generated from 0xb4lamx/nestjs-boilerplate-microservice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
172 lines (168 loc) · 7.02 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
171
172
#!groovy
def lastCommitInfo = ""
def skippingText = ""
def commitContainsSkip = 0
def shouldBuild = false
def tag = ""
String[] releaseBranches = ["master","pre/rc","develop"]
// ---- infrastructure repository params ----
def infrastructure_environment_folder = ""
pipeline {
agent any
environment {
// ---- microservice repository params ----
microservice_repository = "nestjs-boilerplate-microservice"
microservice_organization = "0xb4lamx"
// ---- infrastructure repository params ----
infrastructure_organization = "itninja-hue"
infrastructure_repository = "nestjs-k8s-boilerplate-microservice"
// ---- docker hub params ----
dockerhub_organization = 'itninja-hue'
dockerhub_repository = 'nestjs-boilerplate-microservice'
}
stages{
stage('init') {
steps{
script{
lastCommitInfo = sh(script: "git log -1 --pretty=medium", returnStdout: true).trim()
commitContainsSkip = sh(script: "git log -1 | grep '.*\\[skip ci\\].*'", returnStatus: true)
if(commitContainsSkip == 0) {
skippingText = "Skipping commit."
env.shouldBuild = false
currentBuild.result = "NOT_BUILT"
}
}
}
}
stage ('send info to slack'){
when {
expression{
return env.shouldBuild != "false"
}
}
steps{
script{
if(releaseBranches.contains(env.BRANCH_NAME)){
slackSend color: "#2222FF", message:"*${env.JOB_NAME}* received a new commit. :hourglass_flowing_sand:"
}
}
}
}
stage ('sending commit info'){
when {
expression{
return env.shouldBuild != "false"
}
}
steps{
script{
if(releaseBranches.contains(env.BRANCH_NAME)){
slackSend color: "#2222FF", message:"${skippingText}\n *commmit info:* \n ${lastCommitInfo}"
slackSend color: "#2222FF", message:"Releasing on GIT"
}
}
}
}
stage('Git release'){
agent{
docker {image 'timbru31/node-alpine-git'}
}
environment {
HOME = '.'
}
when{
expression{
return env.shouldBuild != "false"
}
}
steps{
script{
if(releaseBranches.contains(env.BRANCH_NAME)){
withCredentials([string(credentialsId: 'GH_TOKEN', variable: 'GH_TOKEN')]){
sh 'npm run release'
}
}
}
}
}
stage ('Releasing Docker Image'){
when{
expression{
return env.shouldBuild != "false"
}
}
steps{
script{
if(releaseBranches.contains(env.BRANCH_NAME)){
slackSend color: "#2222FF", message: "Releasing Image to DockerHub :whale:"
withCredentials([string(credentialsId: 'GH_TOKEN', variable: 'GH_TOKEN')]){
env.tag = sh (returnStdout: true, script: "make retrivetag organization=${microservice_organization} repository=${microservice_repository}")
sh 'make makefile deliver_image_to_dockerhub NAME="${dockerhub_organization}/${dockerhub_repository}" organization=${microservice_organization} repository=${microservice_repository}'
}
slackSend color: "good", message: "Image released \n *Tag :* ${env.tag}"
}
}
}
}
stage ('infrastructure version update'){
when{
expression{
return env.shouldBuild != "false"
}
}
steps{
script{
if(releaseBranches.contains(env.BRANCH_NAME)){
slackSend color: "#2222FF", message: "Updating tag in infrastructure repository"
switch(env.BRANCH_NAME) {
case "develop":
env.infrastructure_environment_folder = "dev"
break
case "pre/rc":
env.infrastructure_environment_folder = "preprod"
break
case "master":
env.infrastructure_environment_folder = "prod"
break
}
withCredentials([usernamePassword(credentialsId: 'sysadmin', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]){
sh 'git clone http://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/${infrastructure_organization}/${infrastructure_repository}.git'
sh 'cd ${infrastructure_repository}; sed -i "s/v[0-9]*\\.[0-9]*\\.[0-9]*.*/${tag}/g" ${infrastructure_environment_folder}/values.yaml'
sh 'cd ${infrastructure_repository}; git add ${infrastructure_environment_folder}/values.yaml'
sh 'cd ${infrastructure_repository}; git commit -m "update tag" && git push http://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/${infrastructure_organization}/${infrastructure_repository}.git || :'
sh 'rm -rf ${infrastructure_repository}'
}
slackSend color: "good", message: "Tag updated succesfully trigering infratsructure pipeline"
}
}
}
}
}
post {
always {
/* clean up workspace */
deleteDir();
}
success {
script{
if(releaseBranches.contains(env.BRANCH_NAME)){
slackSend color: "good", message: "*${env.JOB_NAME}* Build #${env.BUILD_NUMBER} job is completed successfully \n <${env.BUILD_URL}|Job Link>"
}
}
}
unstable{
script{
if(releaseBranches.contains(env.BRANCH_NAME)){
slackSend color: "danger", message: "*${env.JOB_NAME}* Build #${env.BUILD_NUMBER} job is unstable :ghost: \n <${env.BUILD_URL}|Job Link>"
}
}
}
failure{
script{
if(releaseBranches.contains(env.BRANCH_NAME)){
slackSend color: "danger", message: "*${env.JOB_NAME}* Build #${env.BUILD_NUMBER} job failed :scream_cat: \n <${env.BUILD_URL}|Job Link>"
}
}
}
}
}