-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathJenkinsfile
110 lines (96 loc) · 3.15 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
#!groovy
def tag_image_as(docker_image_url, tag) {
// Tag image, push to repo, remove local tagged image
script {
docker.image("${docker_image_url}:${env.COMMIT_HASH}").push(tag)
sh "docker rmi ${docker_image_url}:${tag} || true"
}
}
def deploy(app_name, environment) {
// Deploys the app to the given environment
build job: 'Subtask_Openstack_Playbook',
parameters: [
[$class: 'StringParameterValue', name: 'INVENTORY', value: environment],
[$class: 'StringParameterValue', name: 'PLAYBOOK', value: 'deploy.yml'],
[$class: 'StringParameterValue', name: 'PLAYBOOKPARAMS', value: "-e cmdb_id=app_${app_name}"],
]
}
def tag_and_deploy(docker_image_url, app_name, environment) {
// Tags the Docker with the environment, en deploys to the same environment
script {
tag_image_as(docker_image_url, environment)
deploy(app_name, environment)
}
}
def build_image(docker_image_url, source) {
// Builds the image given the source, and pushes it to the Amsterdam Docker registry
script {
def image = docker.build("${docker_image_url}:${env.COMMIT_HASH}",
"--no-cache " +
"--shm-size 1G " +
"--build-arg COMMIT_HASH=${env.COMMIT_HASH} " +
"--build-arg BRANCH_NAME=${env.BRANCH_NAME} " +
" ${source}")
image.push()
tag_image_as(docker_image_url, "latest")
}
}
def remove_image(docker_image_url) {
// delete original image built on the build server
script {
sh "docker rmi ${docker_image_url}:${env.COMMIT_HASH} || true"
}
}
pipeline {
agent any
environment {
PRODUCTION = "production"
ACCEPTANCE = "acceptance"
OPEN_ZAAK_IMAGE_URL = "${DOCKER_REGISTRY_NO_PROTOCOL}/open-zaak"
OPEN_ZAAK_SOURCE = "./open-zaak"
OPEN_ZAAK_NAME = "open-zaak"
OPEN_NOTIFICATIES_IMAGE_URL = "${DOCKER_REGISTRY_NO_PROTOCOL}/open-notificaties"
OPEN_NOTIFICATIES_SOURCE = "./open-notificaties"
OPEN_NOTIFICATIES_NAME = "open-notificaties"
}
stages {
stage("Checkout") {
steps {
checkout scm
script {
env.COMMIT_HASH = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()
}
}
}
stage("Build docker images") {
steps {
build_image(env.OPEN_ZAAK_IMAGE_URL, env.OPEN_ZAAK_SOURCE)
build_image(env.OPEN_NOTIFICATIES_IMAGE_URL, env.OPEN_NOTIFICATIES_SOURCE)
}
}
stage("Push and deploy acceptance images") {
when {
not { buildingTag() }
branch 'main'
}
steps {
tag_and_deploy(env.OPEN_ZAAK_IMAGE_URL, env.OPEN_ZAAK_NAME, env.ACCEPTANCE)
tag_and_deploy(env.OPEN_NOTIFICATIES_IMAGE_URL, env.OPEN_NOTIFICATIES_NAME, env.ACCEPTANCE)
}
}
stage("Push and deploy production images") {
// Only deploy to production if there is a tag
when { buildingTag() }
steps {
tag_and_deploy(env.OPEN_ZAAK_IMAGE_URL, env.OPEN_ZAAK_NAME, env.PRODUCTION)
tag_and_deploy(env.OPEN_NOTIFICATIES_IMAGE_URL, env.OPEN_NOTIFICATIES_NAME, env.PRODUCTION)
}
}
}
post {
always {
remove_image(env.OPEN_ZAAK_IMAGE_URL)
remove_image(env.OPEN_NOTIFICATIES_IMAGE_URL)
}
}
}