-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
107 lines (105 loc) · 3.07 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
pipeline {
agent any
options {
disableConcurrentBuilds()
}
environment {
registry = "rma-tools-docker-local.repo.vito.be"
registryCredential = "svc_git_rma"
repository = "guppy_server"
version = get_version()
datetime = get_datetime()
deploy_project = "guppy_deploy"
deploy_test_branch = "test"
deploy_prod_branch = "prod"
}
stages {
stage("Build conda-env docker image") {
// build and name the conda-env stage of the Dockerfile to prevent dangling images
steps {
script {
docker.build(repository + "_conda-env" + ":latest-${env.BRANCH_NAME}", "--target conda-env server")
}
}
}
stage("Build docker image") {
steps {
script {
dockerImage = docker.build(repository + ":latest-${env.BRANCH_NAME}", "server")
}
}
}
stage("Run unittests") {
steps {
script {
dockerImage.inside() {
sh '/opt/guppy/server/conda-env/bin/python -m unittest discover -s server -v'
}
}
}
}
stage("Push docker image to registry") {
steps {
script {
docker.withRegistry("https://" + registry, registryCredential ) {
dockerImage.push("$version")
if (env.BRANCH_NAME =~ /^(main|master)$/ ) {
dockerImage.push("latest")
dockerImage.push("$datetime")
}
if (env.TAG_NAME) {
dockerImage.push("${env.TAG_NAME}")
} else {
dockerImage.push("latest-${env.BRANCH_NAME}")
}
}
// Cleanup
sh(script: "docker image rm $registry/$repository:$version")
if (env.BRANCH_NAME =~ /^(main|master)$/ ) {
sh(script: "docker image rm $registry/$repository:latest")
sh(script: "docker image rm $registry/$repository:$datetime")
}
if (env.TAG_NAME) {
sh(script: "docker image rm $registry/$repository:${env.TAG_NAME}")
} else {
sh(script: "docker image rm $registry/$repository:latest-${env.BRANCH_NAME}")
}
}
}
}
stage("Update services") {
when {
expression { BRANCH_NAME =~ /^(develop|main|master)$/ }
}
steps {
script {
if (env.BRANCH_NAME == 'develop') {
env.deploy_branch = deploy_test_branch
}
if (env.BRANCH_NAME =~ /^(main|master)$/ ) {
env.deploy_branch = deploy_prod_branch
}
try {
build(job: "${deploy_project}/${deploy_branch}", wait: true)
} catch (hudson.AbortException e) {
println("Skipping service update: ${deploy_project}/${deploy_branch} does not exist yet!")
}
}
}
}
}
post {
always {
deleteDir()
}
}
}
def get_version() {
if (env.TAG_NAME) {
return get_datetime() + "-${env.TAG_NAME}"
}
return get_datetime() + "-${env.BRANCH_NAME}"
}
def get_datetime() {
return sh(script: "date -d @`git log -1 --format=%at` +%Y%m%d%Z%H%M", returnStdout: true).trim()
}