This repository has been archived by the owner on Apr 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathJenkinsfile
131 lines (127 loc) · 4.46 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
pipeline {
agent any
options {
// Cleanup build older than 10 previous ones
buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '10')
// Let's measure time
timestamps()
// Fail build if more than 10 minutes
timeout(activity: true, time: 600, unit: 'SECONDS')
}
parameters {
booleanParam(name: 'FORCE_DEPLOY', defaultValue: false, description: 'Force deployment step even if not on master branch?')
}
environment {
PYTHONUNBUFFERED = '1'
ANSIBLE_FORCE_COLOR = 'true'
}
triggers {
cron('@weekly')
}
stages {
stage('Shell Lint') {
agent {
label 'docker && small'
}
steps {
sh '''
docker run --rm -t -v ${WORKSPACE}:${WORKSPACE}:ro koalaman/shellcheck-alpine \
sh -x -c "find ${WORKSPACE}/openio-sds -type f -name *.bats -or -name build.sh -or -name test.sh | grep -v ansible-playbook-openio-deployment \
| xargs -I- shellcheck -"
'''
}
} /* stage('Shell Lint') */
stage('Build a SDS Release Docker Image') {
matrix {
axes {
axis {
name 'DOCKER_IMAGE_DIR'
values 'openio-sds/20.04', 'openio-sds/19.10', 'openio-sds/19.04','openio-sds/18.10'
}
}
agent {
dockerfile {
label 'docker && big'
dir "${DOCKER_IMAGE_DIR}/jenkins/"
filename 'Dockerfile'
args '-v /var/run/docker.sock:/var/run/docker.sock -u root'
}
}
environment {
DOCKER_IMAGE_NAME = DOCKER_IMAGE_DIR.replaceAll("/","-")
DOCKER_BUILD_CONTAINER_NAME = "${GIT_COMMIT.substring(0,6)}-${BUILD_ID}-${DOCKER_IMAGE_NAME}-build"
DOCKER_TEST_CONTAINER_NAME = "${GIT_COMMIT.substring(0,6)}-${BUILD_ID}-${DOCKER_IMAGE_NAME}-test"
}
stages {
stage('build') {
steps {
sh 'echo "Building Docker Image ${DOCKER_IMAGE_NAME} from ${DOCKER_IMAGE_DIR}"'
sh 'bash ${WORKSPACE}/${DOCKER_IMAGE_DIR}/build.sh'
}
post {
always {
script {
// Cleanup with forced removal, and removal of volumes
sh '''
docker kill ${DOCKER_BUILD_CONTAINER_NAME} || true
docker rm -f -v ${DOCKER_BUILD_CONTAINER_NAME} || true
'''
}
}
}
} // stage('build')
stage('test') {
steps {
sh 'echo "Testing Docker Image ${DOCKER_IMAGE_NAME} from ${DOCKER_IMAGE_DIR}"'
sh 'bash ${WORKSPACE}/${DOCKER_IMAGE_DIR}/test.sh'
}
post {
always {
script {
// Cleanup with forced removal, and removal of volumes
sh '''
docker kill ${DOCKER_TEST_CONTAINER_NAME} || true
docker rm -f -v ${DOCKER_TEST_CONTAINER_NAME} || true
'''
}
}
}
} // stage('test')
stage('deploy') {
environment {
DOCKER_HUB = credentials('ID_HUB_DOCKER') // Defines DOCKER_HUB_USR and DOCKER_HUB_PSW env variables
}
steps {
sh 'echo "Deploying Docker Image ${DOCKER_IMAGE_NAME} from ${DOCKER_IMAGE_DIR}"'
// Log in local Docker to the remote registry
sh 'echo ${DOCKER_HUB_PSW} | docker login --password-stdin -u ${DOCKER_HUB_USR}'
sh 'bash ${WORKSPACE}/${DOCKER_IMAGE_DIR}/deploy.sh'
}
when {
anyOf {
branch 'master'
buildingTag()
expression { return params.FORCE_DEPLOY }
}
}
} // stage('deploy')
} // stages
post {
always {
sh 'docker rmi -f ${DOCKER_IMAGE_NAME} || true'
sh 'chmod -R 777 ${WORKSPACE}'
cleanWs()
}
} // post
} // matrix
} // stage('Build a SDS Release Docker Image')
} // stages
post {
failure {
slackMessageUserMention("Build Failed - ${env.JOB_NAME} #${env.BUILD_NUMBER} (<${env.RUN_DISPLAY_URL}|Open>) is ${currentBuild.result}", '#AA0000')
}
success {
slackMessageUserMention("Build Success - ${env.JOB_NAME} #${env.BUILD_NUMBER} (<${env.RUN_DISPLAY_URL}|Open>) is ${currentBuild.result}", '#008800')
}
}
}