This repository was archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJenkinsfile
More file actions
161 lines (139 loc) · 4.47 KB
/
Copy pathJenkinsfile
File metadata and controls
161 lines (139 loc) · 4.47 KB
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
// The name of the service
def service = 'bcf-converter'
// The name of the current branch
def branch = env.BRANCH_NAME.replaceAll("[^a-zA-Z0-9 ]+", "-")
// The name of the docker image to be built and pushed
def image = "820709727174.dkr.ecr.eu-central-1.amazonaws.com/${service}"
// The name consists of the job's name and the build number.
// It is used to name the docker container and should be unique.
def containerName = "${env.JOB_NAME}-${BUILD_NUMBER}".replaceAll("[^a-zA-Z0-9 ]+", "-")
// The git hash is added as to the docker image name to be able to distinguish
// between separate builds and deploy them individually.
def gitHash = "";
pipeline {
agent any
stages {
// Using the credentials in Jenkins' credentials store, git checkout is
// performed
stage('Checkout') {
steps {
git branch: "${env.BRANCH_NAME}",
credentialsId: '2e500ae7-ce03-47ca-901c-e55188de6d78',
url: "git@gitlab.com:bimspot/${service}.git"
}
}
// Building the docker image and starting up a container with the
// newly built image.
// At first, only a partial build of the 'build' stage is done
// so the tests can ber run
stage('Build: testing') {
steps {
sh "docker build --target builder -t ${image} ."
sh "docker run --name ${containerName} --detach --tty ${image} /bin/sh"
}
}
// Performing different tests, for instance:
// - Running JEST for testing
// - Postman API integration tests
stage('Test') {
steps {
sh "docker exec ${containerName} jest --ci --reporters=default --reporters=jest-junit"
// sh """
// docker exec ${containerName} \
// newman run \
// {{collection file}} -e {{environment file}}
// """
}
}
// Security check of the dependencies using snyk.io monitor
// // Only runs if package.json, or oom.xml was changed
stage('Security: snyk') {
when {
changeset "src/package.json"
}
steps {
sh "docker exec ${containerName} snyk monitor --org=bimspot"
}
}
// // Generating documentation and making it available on docs.bimspot.io
// // Only run for development branch
// stage('Documentation') {
// when {
// expression {
// env.BRANCH_NAME == 'develop'
// }
// }
// steps {
// sh "docker exec ${containerName} jsdoc --configure .jsdoc.json"
// // TODO:
// sh "docker cp ${containerName}:docs ."
// }
// }
// The production image is built here.
stage('Build: deploy') {
steps {
script {
gitHash = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim().take(7)
}
sh "docker build -t ${image}:${branch} -t ${image}:${branch}-${gitHash} ."
}
}
// Publishes the docker image to ERC after tagging
// it with the name of the current branch.
stage('Publish') {
steps {
script {
docker.withRegistry("https://820709727174.dkr.ecr.eu-central-1.amazonaws.com", "ecr:eu-central-1:iam_jenkins") {
sh "docker push ${image}:${branch}"
sh "docker push ${image}:${branch}-${gitHash}"
}
}
}
}
}
// These steps run after the pipeline completes.
post {
always {
// Collecting unit test results.
sh "docker cp ${containerName}:artifacts ."
junit 'artifacts/*.xml'
// Stopping and removing docker container
sh "docker stop ${containerName} && docker rm ${containerName}"
// Cleanup
sh "rm -rf docs artifacts"
}
success {
notifySlack('SUCCESS')
}
failure {
notifySlack('ERROR')
}
}
}
// Posts a Slack notification with the job status status and
// code changes from git
def notifySlack(String buildStatus) {
buildStatus = buildStatus
def colorName = 'RED'
def colorCode = '#FF0000'
def subject = "${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
def changeSet = getChangeSet()
def message = "${subject} \n ${changeSet}"
if (buildStatus == 'SUCCESS') {
color = 'GREEN'
colorCode = '#00FF00'
} else {
color = 'RED'
colorCode = '#FF0000'
}
slackSend(channel: '#bimspot-builds', color: colorCode, message: message)
}
@NonCPS
// Fetching change set from Git
def getChangeSet() {
return currentBuild.changeSets.collect { cs ->
cs.collect { entry ->
"* ${entry.author.fullName}: ${entry.msg}"
}.join("\n")
}.join("\n")
}