-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
180 lines (161 loc) · 5.22 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
pipeline {
agent none
/*** PIPELINE ENVIRONMENT ***/
environment {
AWS_ACCOUNT = '904573531492'
AWS_REGION = 'eu-central-1'
ECR_REPO_NAME = 'api'
ECR_REPO_URI = "${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/${ECR_REPO_NAME}"
SLAVES_TEMPLATES_PATH = 'slaves'
}
/*** STAGES ***/
stages {
/** BUILD **/
/* executed in two ways:
* - tag builds, for new software releases
* - push builds, for commits (i.e. merges from feature branches) on master
*/
stage('image-build') {
parallel {
stage('branch-build') {
when {
// only for the master branch
beforeAgent true
branch 'master'
}
agent {
// execute on the 'kaniko slave' pod
kubernetes {
defaultContainer 'kaniko'
yamlFile "${SLAVES_TEMPLATES_PATH}/kaniko-slave.yaml"
}
}
steps {
sh """
/kaniko/executor \
--dockerfile \$(pwd)/Dockerfile \
--context \$(pwd) \
--destination=${env.ECR_REPO_URI}:${env.GIT_COMMIT.take(7)} \
--destination=${env.ECR_REPO_URI}:latest
"""
}
}
stage('tag-build') {
when {
// only for a tag build
beforeAgent true
buildingTag()
}
agent {
// execute on the 'awscli slave' pod
kubernetes {
defaultContainer 'awscli'
yamlFile "${SLAVES_TEMPLATES_PATH}/awscli-slave.yaml"
}
}
steps {
script {
// this step depends on the branch build one; lock can't be used in this case; poll on ECR
timeout(300) {
waitUntil {
sleep(10)
sh(script: """
aws ecr describe-images --repository-name=${env.ECR_REPO_NAME} --image-ids=imageTag=${env.GIT_COMMIT.take(7)} --region ${env.AWS_REGION}
""", returnStatus: true
) == 0
}
}
sh """
manifest=\$(aws ecr batch-get-image --repository-name ${env.ECR_REPO_NAME} --image-ids imageTag=${env.GIT_COMMIT.take(7)} --region ${env.AWS_REGION} --query 'images[].imageManifest' --output text)
aws ecr put-image --repository-name ${env.ECR_REPO_NAME} --image-tag ${env.TAG_NAME} --image-manifest "\${manifest}" --region ${env.AWS_REGION}
"""
}
}
}
}
}
stage('update-manifests') {
options {
lock (resource: 'UPDATE_MANIFESTS_LOCK')
}
when {
beforeAgent true
anyOf {
branch 'master'
buildingTag()
}
}
environment {
GIT_MANIFESTS_REPO_URI = 'github.com/rabe-gitops/manifests.git'
GIT_MANIFESTS_REPO_NAME = 'manifests'
APP_MANIFEST_FILE = 'base/api-deployment.yaml'
GIT_USERNAME = 'rabe-gitops-bot'
}
agent {
// execute on the 'python slave' pod
kubernetes {
defaultContainer 'git'
yamlFile "${SLAVES_TEMPLATES_PATH}/git-slave.yaml"
}
}
steps {
withCredentials([usernamePassword(
credentialsId: 'rabe-gitops-jenkinsci',
usernameVariable: 'GIT_EMAIL',
passwordVariable: 'GIT_TOKEN'
)]) {
script {
def IMAGE_TAG = env.GIT_COMMIT.take(7)
def IMAGE_TAG_PREFIX = 'api'
def NOTIFY = false
if (env.TAG_NAME) {
IMAGE_TAG = env.TAG_NAME
IMAGE_TAG_PREFIX = 'rel'
NOTIFY = true
}
// sh """
// pip install -r scripts/requirements.txt
// python scripts/deploy.py \
// --manifests_repo=${env.GIT_MANIFESTS_REPO_NAME} \
// --manifests_app_file=${env.APP_MANIFEST_FILE} \
// --image_tag_prefix=${IMAGE_TAG_PREFIX} \
// --new_image_tag=${IMAGE_TAG} \
// """
sh """
git clone -b master --single-branch https://${env.GIT_USERNAME}:${GIT_TOKEN}@${env.GIT_MANIFESTS_REPO_URI}
cd ${env.GIT_MANIFESTS_REPO_NAME}
sed -i 's|image: .*|image: ${env.ECR_REPO_URI}:${IMAGE_TAG}|g' ${env.APP_MANIFEST_FILE}
git config user.name ${env.GIT_USERNAME}
git config user.email ${GIT_EMAIL}
git add .
git diff-index --quiet HEAD || git commit -m "Update base image with version '${IMAGE_TAG}'"
git tag ${IMAGE_TAG_PREFIX}-${IMAGE_TAG}
git push origin master --tags
"""
if (NOTIFY) {
slackSend color: "good", message: "API version updated to '${IMAGE_TAG}'"
}
}
}
}
}
}
// /*** POST-EXECUTION ***/
// post {
// success {
// node('ci-jenkins-slave') {
// echo 'SUCCESS'
// }
// }
// failure {
// node('ci-jenkins-slave') {
// echo 'FAILURE'
// }
// }
// always {
// node('ci-jenkins-slave') {
// echo 'ENDED'
// }
// }
// }
}