-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
170 lines (131 loc) · 5.35 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
168
169
170
// ***********************
//
// Build and deploy different environments with jenkins pipeline
//
// Merge to develop -> triggers development release
// Merge to master without tag -> triggers staging release
// Merge to master with tag -> triggers staging and production release
// Production release requires manual approval on the jenkins job
//
// Configure jenkins pipeline project to pull tags! By default, tags are not pulled!
// -> Check "Advanced clone behaviours" feature of jenkins git plugin
//
// ***********************
def CONTAINER_NAME="rnssolution/antlia-web"
def CONTAINER_TAG="0.0.1"
def DOCKER_HUB_USER="rnssolutions"
pipeline {
agent {label params.node}
tools {nodejs "node"}
environment {
GLOBAL_ENVIRONMENT = 'NO_DEPLOYMENT'
// Need the staging properties anyway to deploy to staging and production simultaneously when doing a prod release
ENVIRONMENT_STAGING = 'staging'
}
options {
// Keep maximum 10 archived artifacts
buildDiscarder(logRotator(numToKeepStr: '10', artifactNumToKeepStr: '10'))
// No simultaneous builds
disableConcurrentBuilds()
}
post {
always {
discordSend description: 'Jenkins Pipeline Build', footer: '' , link: env.BUILD_URL, result: currentBuild.currentResult, unstable: false, title: JOB_NAME, webhookURL: 'https://discordapp.com/api/webhooks/836489945262063626/_SxqgqjzQ-NHYzUhMKYK6dvzj46jV9jbBXVNNPx3wO0CcmMxAAVOT-08rUL8RMBrIW96'
}
}
stages {
stage('Prepare workspace') {
steps {
echo 'Prepare workspace'
// Clean workspace
step([$class: 'WsCleanup'])
// Checkout git
checkout scm
}
}
stage('Setup environment') {
steps {
echo 'Setup environment'
script {
// Determine whether this is a test or a staging / production build
switch (env.BRANCH_NAME) {
case 'development':
GLOBAL_ENVIRONMENT = 'development'
break
case 'testing':
GLOBAL_ENVIRONMENT = 'testing'
break
case 'staging':
GLOBAL_ENVIRONMENT = 'staging'
break
case 'master':
GLOBAL_ENVIRONMENT = 'production'
break
default:
GLOBAL_ENVIRONMENT = 'NO_DEPLOYMENT'
break
}
// Get tag on current branch
TAG = sh(returnStdout: true, script: 'git tag --points-at HEAD')
echo 'Banch To Build'
echo env.BRANCH_NAME
if (TAG && GLOBAL_ENVIRONMENT == 'staging') {
echo 'Build for production'
// Ask user whether master should be builded and deployed to production
try {
timeout(time: 15, unit: 'MINUTES') {
APPROVED = input(
id: 'BuildForProductionInput',
message: 'Build and deploy',
parameters: [
booleanParam(
defaultValue: false,
description: '',
name: 'Build and deploy ' + TAG + ' for production?'
)
]
)
if (APPROVED) {
GLOBAL_ENVIRONMENT = 'production'
} else {
error 'Build for production aborted'
}
}
} catch (err) {
error 'Build for production aborted'
}
}
}
}
}
stage('Build-image') {
steps {
echo 'Build ' + GLOBAL_ENVIRONMENT
buildImage(GLOBAL_ENVIRONMENT)
}
}
stage('Deploy') {
steps {
echo 'Deploy ' + GLOBAL_ENVIRONMENT
script {
if (GLOBAL_ENVIRONMENT == 'NO_DEPLOYMENT') {
echo 'This is not develop nor master branch and should not be deployed'
} else {
deploy(GLOBAL_ENVIRONMENT)
if (GLOBAL_ENVIRONMENT == 'production') {
echo 'Additionally, deploy staging'
}
}
}
}
}
}
}
def buildImage(ENVIRONMENT) {
echo 'started building image...'
sh './jenkins/scripts/docker-build.sh'
}
def deploy(ENVIRONMENT) {
echo 'started deploying'
sh './jenkins/scripts/remote-deploy.sh'
}