This repository has been archived by the owner on Aug 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJenkinsfile
83 lines (77 loc) · 2.72 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
#!/usr/bin/env groovy
final IMAGE_BASE_NAME = 'swisstopo/service-print'
final IMAGE_BASE_NAME_NGINX = 'swisstopo/service-print-nginx'
final IMAGE_BASE_NAME_TOMCAT = 'swisstopo/service-print-tomcat'
final DOCKER_REGISTRY_URL = 'https://registry.hub.docker.com'
node(label: "jenkins-slave") {
final deployGitBranch = env.BRANCH_NAME
env.COMPOSE_PROJECT_NAME = "${env.JOB_NAME}-${env.BUILD_ID}"
def IMAGE_TAG = "staging"
env.IMAGE_TAG = IMAGE_TAG
try {
withEnv(["JENKINS_DOCKERHUB_USER=${JENKINS_DOCKERHUB_USER}", "JENKINS_DOCKERHUB_PASSWORD=${JENKINS_DOCKERHUB_PASSWORD}"]){
stage("Checkout") {
sh 'echo Checking out code from github'
final scmVars = checkout scm
sh 'docker --version'
sh 'docker-compose --version'
if (deployGitBranch == 'master') {
IMAGE_TAG = 'staging'
} else {
IMAGE_TAG = scmVars.GIT_COMMIT
}
sh "echo git branch=$BRANCH_NAME"
sh "echo Setting IMAGE_TAG to ${IMAGE_TAG}"
env.IMAGE_TAG = "${IMAGE_TAG}"
}
stage("Build") {
sh 'echo Starting the build...'
sh 'echo "export IMAGE_TAG=${IMAGE_TAG}" >> rc_user'
sh 'GIT_BRANCH=$GIT_BRANCH GIT_COMMIT_HASH=$GIT_COMMIT GIT_COMMIT_DATE=1970-01-01 make dockerbuild'
}
stage("Run") {
sh '''
echo Starting the containers...
make dockerrun
'''
}
stage("Test") {
sh '''
echo Starting the tests...
DOCKER_CONTAINER_ID="$(docker ps | grep "python3 print3" | awk '{ print $1 }')"
docker exec -i "$DOCKER_CONTAINER_ID" coverage run --source=print3 --omit=print3/wsgi.py setup.py test
echo All tests are successful
'''
}
if (deployGitBranch == 'master') {
stage("Publish") {
sh 'echo Publishing images to Dockerhub'
sh 'docker login -u "${JENKINS_DOCKERHUB_USER}" -p "${JENKINS_DOCKERHUB_PASSWORD}"'
docker.image("${IMAGE_BASE_NAME}:${IMAGE_TAG}").push()
docker.image("${IMAGE_BASE_NAME_NGINX}:${IMAGE_TAG}").push()
docker.image("${IMAGE_BASE_NAME_TOMCAT}:${IMAGE_TAG}").push()
}
}
if (deployGitBranch == 'master') {
stage("Deploy") {
sh 'echo Deploying to dev'
sh 'make rancherdeploydev'
sh 'echo Deployed to dev'
}
}
}
}
catch (e) {
throw e
}
finally {
sh 'docker-compose down -v || echo Skipping'
sh "docker rmi ${IMAGE_BASE_NAME}:${IMAGE_TAG} || echo Skipping"
sh "docker rmi ${IMAGE_BASE_NAME_NGINX}:${IMAGE_TAG} || echo Skipping"
sh "docker rmi ${IMAGE_BASE_NAME_TOMCAT}:${IMAGE_TAG} || echo Skipping"
sh 'git clean -dx --force'
sh 'docker ps'
sh 'docker ps --all --filter status=exited'
sh 'echo All dockers have been purged'
}
}