-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJenkinsfile
167 lines (153 loc) Β· 6.14 KB
/
Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
pipeline {
agent any
environment {
DISCORD_WEBHOOK = credentials('discord-webhook')
}
stages {
stage('Determine Environment') {
steps {
script {
def branchName = env.BRANCH_NAME ?: 'unknown'
branchName = branchName.replaceFirst('origin/', '')
switch (branchName) {
case 'develop':
env.ENVIRONMENT = 'development'
env.ENV_FILE_CREDENTIAL = 'blog-dev-env-file'
env.DOCKER_COMPOSE_FILE = 'docker-compose.develop.yml'
break
case ~/^release\/.*/:
env.ENVIRONMENT = 'staging'
env.ENV_FILE_CREDENTIAL = 'blog-staging-env-file'
env.DOCKER_COMPOSE_FILE = 'docker-compose.pre.yml'
break
case 'main':
env.ENVIRONMENT = 'production'
env.ENV_FILE_CREDENTIAL = 'blog-prod-env-file'
env.DOCKER_COMPOSE_FILE = 'docker-compose.prod.yml'
break
default:
env.ENVIRONMENT = 'other'
env.DOCKER_COMPOSE_FILE = ''
env.ENV_FILE_CREDENTIAL = 'blog-dev-env-file'
}
echo "Environment: ${env.ENVIRONMENT}"
echo "DOCKER_COMPOSE_FILE: ${env.DOCKER_COMPOSE_FILE}"
}
}
}
stage('Setup .env') {
steps {
script {
withCredentials([file(credentialsId: env.ENV_FILE_CREDENTIAL, variable: 'SECRET_ENV_FILE')]) {
sh "cp $SECRET_ENV_FILE .env"
echo "Loaded environment file for ${env.ENVIRONMENT}."
}
}
}
}
stage('Checkout & Pull') {
steps {
script {
checkout scm
env.LAST_COMMIT_AUTHOR = sh(script: "git log -1 --pretty=format:'%an'", returnStdout: true).trim()
env.LAST_COMMIT_MESSAGE = sh(script: "git log -1 --pretty=format:'%s'", returnStdout: true).trim()
echo "Last Commit Author: ${env.LAST_COMMIT_AUTHOR}"
echo "Last Commit Message: ${env.LAST_COMMIT_MESSAGE}"
}
}
}
stage('Install Dependencies') {
steps {
script {
sh 'npm install'
}
}
}
stage('Maual Build') {
steps {
script {
sh 'npm run build'
}
}
}
stage('Run Tests') {
steps {
script {
sh 'npm test'
}
}
}
stage('Build & Deploy Docker') {
when {
expression { env.ENVIRONMENT != 'other' && env.DOCKER_COMPOSE_FILE?.trim() }
}
steps {
script {
echo "Using DOCKER_COMPOSE_FILE: ${env.DOCKER_COMPOSE_FILE}"
sh """
docker compose -f ${env.DOCKER_COMPOSE_FILE} build
docker compose -f ${env.DOCKER_COMPOSE_FILE} up -d
"""
}
}
}
}
post {
always {
script {
def color = (currentBuild.result == 'SUCCESS') ? 3066993 : 15158332
def status = (currentBuild.result == 'SUCCESS') ? 'β
Success' : 'β Failure'
def timestamp = new Date().format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone('UTC'))
def payload = [
content: null,
embeds: [[
title: "π Pipeline Execution Report For BSO Blog Front-end",
description: "Pipeline execution details below:",
color: color,
thumbnail: [
url: "https://raw.githubusercontent.com/bsospace/assets/refs/heads/main/LOGO/LOGO%20WITH%20CIRCLE.ico"
],
fields: [
[
name: "Job",
value: "${env.JOB_NAME} [#${env.BUILD_NUMBER}]",
inline: true
],
[
name: "Status",
value: status,
inline: true
],
[
name: "Branch",
value: "${env.BRANCH_NAME ?: 'unknown'}",
inline: true
],
[
name: "Author",
value: "${env.LAST_COMMIT_AUTHOR ?: 'unknown'}",
inline: true
],
[
name: "Commit Message",
value: "${env.LAST_COMMIT_MESSAGE ?: 'unknown'}",
inline: false
]
],
footer: [
text: "Pipeline executed at",
icon_url: "https://raw.githubusercontent.com/bsospace/assets/refs/heads/main/LOGO/LOGO%20WITH%20CIRCLE.ico"
],
timestamp: timestamp
]]
]
httpRequest(
url: env.DISCORD_WEBHOOK,
httpMode: 'POST',
contentType: 'APPLICATION_JSON',
requestBody: groovy.json.JsonOutput.toJson(payload)
)
}
}
}
}