-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
188 lines (166 loc) · 6.92 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
181
182
183
184
185
186
187
188
#!/usr/bin/env groovy
// Learn groovy: https://learnxinyminutes.com/docs/groovy/
def docker_credentials = 'ecr:us-east-1:tailor_aws'
def recipes_yaml = 'rosdistro/config/recipes.yaml'
def images_yaml = 'rosdistro/config/images.yaml'
def parentImage = { release, docker_registry -> docker_registry - "https://" + ':tailor-image-' + release + '-parent-' + env.BRANCH_NAME }
def distributions = []
def images = null
def organization = null
def testing_flavour = null
pipeline {
agent none
parameters {
string(name: 'rosdistro_job', defaultValue: '/ci/toydistro/master')
string(name: 'release_track', defaultValue: 'hotdog')
string(name: 'release_label', defaultValue: 'hotdog')
string(name: 'num_to_keep', defaultValue: '10')
string(name: 'days_to_keep', defaultValue: '10')
string(name: 'retries', defaultValue: '3')
string(name: 'timestamp')
string(name: 'docker_registry')
string(name: 'tailor_meta')
string(name: 'apt_repo')
string(name: 'apt_region', defaultValue: 'us-east-1')
booleanParam(name: 'deploy', defaultValue: false)
}
options {
timestamps()
}
stages {
stage("Configure build parameters") {
agent { label('master') }
steps {
script {
sh('env')
properties([
buildDiscarder(logRotator(
daysToKeepStr: params.days_to_keep, numToKeepStr: params.num_to_keep,
artifactDaysToKeepStr: params.days_to_keep, artifactNumToKeepStr: params.num_to_keep
))
])
copyArtifacts(
projectName: params.rosdistro_job,
selector: upstream(fallbackToLastSuccessful: true),
)
// (pbovbel) Read configuration from rosdistro. This should probably happen in some kind of Python
def recipes_config = readYaml(file: recipes_yaml)
organization = recipes_config['common']['organization']
testing_flavour = recipes_config['common']['testing_flavour']
distributions = recipes_config['os'].collect {
os, distribution -> distribution }.flatten()
images_config = readYaml(file: images_yaml).images
stash(name: 'rosdistro', includes: 'rosdistro/**')
}
}
post {
cleanup {
deleteDir()
}
}
}
stage("Build tailor-image") {
agent any
steps {
script {
dir('tailor-image') {
checkout(scm)
}
stash(name: 'source', includes: 'tailor-image/**')
def parent_image_label = parentImage(params.release_label, params.docker_registry)
def parent_image = docker.image(parent_image_label)
try {
docker.withRegistry(params.docker_registry, docker_credentials) { parent_image.pull() }
} catch (all) {
echo("Unable to pull ${parent_image_label} as a build cache")
}
unstash(name: 'rosdistro')
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'tailor_aws'],
string(credentialsId: 'ansible_vault_password', variable: 'ANSIBLE_VAULT_PASS')]) {
parent_image = docker.build(parent_image_label,
"-f tailor-image/environment/Dockerfile --cache-from ${parent_image_label} " +
"--build-arg APT_REPO=${params.apt_repo} " +
"--build-arg APT_REGION=${params.apt_region} " +
"--build-arg RELEASE_LABEL=${params.release_label} " +
"--build-arg RELEASE_TRACK=${params.release_track} " +
"--build-arg FLAVOUR=${testing_flavour} " +
"--build-arg ORGANIZATION=${organization} " +
"--build-arg AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID " +
"--build-arg AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY " +
"--build-arg ANSIBLE_VAULT_PASS=$ANSIBLE_VAULT_PASS .")
}
parent_image.inside() {
sh('pip3 install -e tailor-image')
}
docker.withRegistry(params.docker_registry, docker_credentials) {
parent_image.push()
}
}
}
post {
cleanup {
library("tailor-meta@${params.tailor_meta}")
cleanDocker()
deleteDir()
}
}
}
stage("Create images") {
agent none
steps {
script {
def jobs = [:]
images_config.each { image, config ->
def tmp_distributions = distributions.clone()
// If `os_versions` is not configured, default to build for all distros
if (config.containsKey('os_versions')) {
tmp_distributions = config['os_versions'].findAll { it in distributions }
}
jobs << tmp_distributions.collectEntries { distribution ->
["${image}-${distribution}", { node {
try {
retry(params.retries as Integer) {
dir('tailor-image') {
checkout(scm)
}
unstash(name: 'rosdistro')
def parent_image = docker.image(parentImage(params.release_label, params.docker_registry))
docker.withRegistry(params.docker_registry, docker_credentials) {
parent_image.pull()
}
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'tailor_aws']]) {
parent_image.inside("-v /var/run/docker.sock:/var/run/docker.sock -v /lib/modules:/lib/modules " +
"-v /dev:/dev -v /boot:/boot --cap-add=ALL --privileged " +
"--env AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID " +
"--env AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY") {
sh("""#!/bin/bash
sudo -E create_image \
--name ${image} \
--distribution ${distribution} \
--apt-repo ${params.apt_repo - 's3://'} \
--release-track ${params.release_track} \
--release-label ${params.release_label} \
--flavour ${testing_flavour} \
--organization ${organization} \
--docker-registry ${params.docker_registry} \
--rosdistro-path /rosdistro \
--timestamp ${params.timestamp} \
${params.deploy ? '--publish' : ''}
""")
}
}
}
} finally {
library("tailor-meta@${params.tailor_meta}")
cleanDocker()
deleteDir()
}
}}]
}
}
parallel(jobs)
}
}
}
}
}