forked from mxkrzak/udemy-docker-az
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
93 lines (91 loc) · 2.94 KB
/
Jenkinsfile
File metadata and controls
93 lines (91 loc) · 2.94 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
pipeline {
agent any
options {
buildDiscarder(logRotator(numToKeepStr: '5'))
}
tools {
nodejs 'node-latest'
}
parameters {
string(name: 'IMAGE_REPO_NAME', defaultValue: 'zgchuck/udemy-docker-az', description: '')
string(name: 'LATEST_BUILD_TAG', defaultValue: 'build-latest', description: '')
string(name: 'DOCKER_COMPOSE_FILENAME', defaultValue: 'docker-compose.yml', description: '')
string(name: 'DOCKER_STACK_NAME', defaultValue: 'react_stack', description: '')
booleanParam(name: 'NPM_RUN_TEST', defaultValue: true, description: '')
booleanParam(name: 'PUSH_DOCKER_IMAGES', defaultValue: true, description: '')
booleanParam(name: 'DOCKER_STACK_RM', defaultValue: false, description: 'Remove previous stack. This is required if you have updated any secrets or configs as these cannot be updated. ')
}
stages {
stage('npm install'){
steps{
sh "npm install"
}
}
stage('npm test'){
when{
expression{
return params.NPM_RUN_TEST
}
}
steps{
sh "npm test -- --coverage"
}
}
stage('npm build'){
steps{
sh "npm run build"
}
}
stage('docker build'){
environment {
COMMIT_TAG = sh(returnStdout: true, script: 'git rev-parse HEAD').trim().take(7)
BUILD_IMAGE_REPO_TAG = "${params.IMAGE_REPO_NAME}:${env.BUILD_TAG}"
}
steps{
sh "docker build . -t $BUILD_IMAGE_REPO_TAG"
sh "docker tag $BUILD_IMAGE_REPO_TAG ${params.IMAGE_REPO_NAME}:$COMMIT_TAG"
sh "docker tag $BUILD_IMAGE_REPO_TAG ${params.IMAGE_REPO_NAME}:${readJSON(file: 'package.json').version}"
sh "docker tag $BUILD_IMAGE_REPO_TAG ${params.IMAGE_REPO_NAME}:${params.LATEST_BUILD_TAG}"
sh "docker tag $BUILD_IMAGE_REPO_TAG ${params.IMAGE_REPO_NAME}:$BRANCH_NAME-latest"
}
}
stage('docker push'){
when{
expression {
return params.PUSH_DOCKER_IMAGES
}
}
environment {
COMMIT_TAG = sh(returnStdout: true, script: 'git rev-parse HEAD').trim().take(7)
BUILD_IMAGE_REPO_TAG = "${params.IMAGE_REPO_NAME}:${env.BUILD_TAG}"
}
steps{
sh "docker push $BUILD_IMAGE_REPO_TAG"
sh "docker push ${params.IMAGE_REPO_NAME}:$COMMIT_TAG"
sh "docker push ${params.IMAGE_REPO_NAME}:${readJSON(file: 'package.json').version}"
sh "docker push ${params.IMAGE_REPO_NAME}:${params.LATEST_BUILD_TAG}"
sh "docker push ${params.IMAGE_REPO_NAME}:$BRANCH_NAME-latest"
}
}
stage('Remove Previous Stack'){
when{
expression {
return params.DOCKER_STACK_RM
}
}
steps{
sh "docker stack rm ${params.DOCKER_STACK_NAME}"
}
}
stage('Docker Stack Deploy'){
steps{
sh "docker stack deploy -c ${params.DOCKER_COMPOSE_FILENAME} ${params.DOCKER_STACK_NAME}"
}
}
}
post {
always {
sh 'echo "This will always run"'
}
}
}