-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
144 lines (120 loc) · 4.26 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
@Library('campudus-jenkins-shared-lib') _
final String BRANCH = params.BRANCH
final boolean NOTIFY_SLACK_ON_FAILURE = params.NOTIFY_SLACK_ON_FAILURE
final boolean NOTIFY_SLACK_ON_SUCCESS = params.NOTIFY_SLACK_ON_SUCCESS
final String CLEAN_BRANCH_NAME = BRANCH ? BRANCH.replaceAll("[\\.\\_\\#]", "-").tokenize('/').last() : ""
final boolean isBranch = CLEAN_BRANCH_NAME != "master"
final String DEPLOY_DIR = 'build/deploy'
final String TEST_COVERAGE_FILE = 'output/coverage/junit.xml'
final String IMAGE_NAME = "campudus/grud-frontend"
final String IMAGE_TAG = CLEAN_BRANCH_NAME && isBranch ? CLEAN_BRANCH_NAME : "latest"
final String ARCHIVE_FILENAME_DIST = "grud-frontend-dist.tar.gz"
final String SLACK_CHANNEL = "#grud"
pipeline {
agent { label 'agent1' }
options {
timestamps()
buildDiscarder(logRotator(numToKeepStr: '10'))
timeout(time: 5, unit: 'MINUTES')
}
triggers {
githubPush()
}
environment {
COMMIT_INFO = sh(returnStdout: true, script: './getCommitHash.sh').trim()
GIT_HASH = sh(returnStdout: true, script: 'git log -1 --pretty=%h').trim()
BUILD_DATE = sh(returnStdout: true, script: 'date \"+%Y-%m-%d %H:%M:%S\"').trim()
GIT_COMMIT_DATE = sh(returnStdout: true, script: "git show -s --format=%ci").trim()
}
parameters {
booleanParam(name: 'NOTIFY_SLACK_ON_FAILURE', defaultValue: true, description: '')
booleanParam(name: 'NOTIFY_SLACK_ON_SUCCESS', defaultValue: false, description: '')
}
stages {
stage('Init Build') {
steps {
buildName "#${env.BUILD_NUMBER} ${isBranch ? CLEAN_BRANCH_NAME : ''}".trim()
echo "Build with BUILD_ID: $COMMIT_INFO"
sh "rm -rf build"
sh "mkdir -p ${DEPLOY_DIR}"
sh "mkdir -p output/coverage"
// cleanup docker
sh 'docker rmi $(docker images -f "dangling=true" -q) || true'
sh "docker rmi -f \$(docker images -qa --filter=reference='${IMAGE_NAME}') || true"
}
}
stage('Build dist') {
steps {
script {
def image = docker.build("${IMAGE_NAME}builder", "--target build -f Dockerfile . --build-arg BUILD_ID=${COMMIT_INFO}")
image.inside {
/*
* Jenkins Docker Plugin automatically mounts WORKSPACE on host to the same directory within the container.
* Also, we need to explicitly use the defined BUILDER_WORKING_DIRECTORY
* because Jenkins runs the docker container automatically within the WORKSPACE directory.
*/
sh "cd /usr/app && ls -la && tar -czf ${WORKSPACE}/${DEPLOY_DIR}/${ARCHIVE_FILENAME_DIST} node_modules out package.json"
sh "cd /usr/app && ls -la && cp ${TEST_COVERAGE_FILE} ${WORKSPACE}/${TEST_COVERAGE_FILE}"
}
}
}
post {
always {
junit TEST_COVERAGE_FILE
}
}
}
stage('Build docker image') {
steps {
sh """
docker build \
--build-arg BUILD_ID=${COMMIT_INFO} \
--label "GIT_COMMIT=${GIT_COMMIT}" \
--label "GIT_COMMIT_DATE=${GIT_COMMIT_DATE}" \
--label "BUILD_DATE=${BUILD_DATE}" \
-t ${IMAGE_NAME}:${IMAGE_TAG} \
-f Dockerfile --rm .
"""
}
}
stage('Artifacts') {
steps {
archiveArtifacts artifacts: "${DEPLOY_DIR}/${ARCHIVE_FILENAME_DIST}", fingerprint: true
}
}
stage('Push to docker registry') {
steps {
withDockerRegistry([credentialsId: "dockerhub", url: ""]) {
sh "docker push ${IMAGE_NAME}:${IMAGE_TAG}"
}
}
}
}
post {
success {
wrap([$class: 'BuildUser']) {
script {
if (NOTIFY_SLACK_ON_SUCCESS) {
final String logParams = [
BRANCH ? "BRANCH=${BRANCH}" : null,
"image: ${IMAGE_NAME}:${IMAGE_TAG}",
].minus(null).join(' ')
slackOk(channel: SLACK_CHANNEL, message: "${logParams}")
}
}
}
}
failure {
wrap([$class: 'BuildUser']) {
script {
if (NOTIFY_SLACK_ON_FAILURE) {
final String logParams = [
BRANCH ? "BRANCH=${BRANCH}" : null,
].minus(null).join(' ')
slackError(channel: SLACK_CHANNEL, message: "${logParams}")
}
}
}
}
}
}