Skip to content

Commit 7fe4ea4

Browse files
ref(CI): build container and use it for testing and building (#80)
1 parent a28a40b commit 7fe4ea4

4 files changed

Lines changed: 139 additions & 134 deletions

File tree

Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM quay.io/deis/go-dev:0.17.0
2+
# This Dockerfile is used to bundle the source and all dependencies into an image for testing.
3+
4+
RUN echo "deb http://packages.cloud.google.com/apt cloud-sdk-jessie main" \
5+
| tee /etc/apt/sources.list.d/google-cloud-sdk.list \
6+
&& curl https://packages.cloud.google.com/apt/doc/apt-key.gpg \
7+
| apt-key add - \
8+
&& apt-get update \
9+
&& apt-get install -y google-cloud-sdk \
10+
--no-install-recommends \
11+
&& rm -rf /var/lib/apt/lists/*
12+
13+
COPY glide.yaml /go/src/github.com/deis/controller-sdk-go/
14+
COPY glide.lock /go/src/github.com/deis/controller-sdk-go/
15+
16+
WORKDIR /go/src/github.com/deis/controller-sdk-go
17+
18+
RUN glide install --strip-vcs --strip-vendor
19+
20+
COPY ./_scripts /usr/local/bin
21+
22+
COPY . /go/src/github.com/deis/controller-sdk-go

Jenkinsfile

Lines changed: 103 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,3 @@
1-
def workpath_linux_root = "/src/github.com/deis"
2-
3-
def make = { String target ->
4-
try {
5-
sh "make ${target} fileperms"
6-
} catch(error) {
7-
sh 'make fileperms'
8-
throw error
9-
}
10-
}
11-
12-
def gopath_linux = {
13-
def gopath = pwd() + "/gopath"
14-
env.GOPATH = gopath
15-
gopath
16-
}
17-
18-
def workdir_linux = { String gopath, dest ->
19-
gopath + workpath_linux_root + "/" + dest
20-
}
21-
221
def sh = { String cmd ->
232
wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'XTerm']) {
243
sh cmd
@@ -44,129 +23,130 @@ node('windows') {
4423
}
4524
}
4625

47-
node('linux') {
48-
def gopath = gopath_linux()
49-
def workdir = workdir_linux(gopath, "controller-sdk-go")
50-
51-
dir(workdir) {
52-
stage 'Checkout Linux'
53-
checkout scm
54-
stage 'Install Linux'
55-
make 'bootstrap'
56-
stage 'Test Linux'
57-
make 'test-style'
58-
make 'test-cover'
59-
stage 'Upload to Codecov'
60-
withCredentials([[$class: 'StringBinding', credentialsId: '2da033eb-2e34-4efd-b090-ad892f348065', variable: 'CODECOV_TOKEN']]) {
61-
sh 'curl -s https://codecov.io/bash | bash'
62-
}
63-
}
64-
}
65-
6626
def git_commit = ''
6727
def git_branch = ''
68-
def go_repo = ''
6928

7029
stage 'Go & Git Info'
7130
node('linux') {
72-
73-
def gopath = gopath_linux()
74-
def workdir = workdir_linux(gopath, "controller-sdk-go")
75-
76-
dir(workdir) {
77-
checkout scm
78-
79-
// HACK: Recommended approach for getting command output is writing to and then reading a file.
80-
sh 'mkdir -p tmp'
81-
sh 'git describe --all > tmp/GIT_BRANCH'
82-
sh 'git rev-parse HEAD > tmp/GIT_COMMIT'
83-
sh 'go list . > tmp/GO_LIST'
84-
git_branch = readFile('tmp/GIT_BRANCH').trim()
85-
git_commit = readFile('tmp/GIT_COMMIT').trim()
86-
go_repo = readFile('tmp/GO_LIST').trim()
87-
88-
if (git_branch != "remotes/origin/master") {
89-
// Determine actual PR commit, if necessary
90-
sh 'git rev-parse HEAD | git log --pretty=%P -n 1 --date-order > tmp/MERGE_COMMIT_PARENTS'
91-
sh 'cat tmp/MERGE_COMMIT_PARENTS'
92-
merge_commit_parents = readFile('tmp/MERGE_COMMIT_PARENTS').trim()
93-
if (merge_commit_parents.length() > 40) {
94-
echo 'More than one merge commit parent signifies that the merge commit is not the PR commit'
95-
echo "Changing git_commit from '${git_commit}' to '${merge_commit_parents.take(40)}'"
96-
git_commit = merge_commit_parents.take(40)
97-
} else {
98-
echo 'Only one merge commit parent signifies that the merge commit is also the PR commit'
99-
echo "Keeping git_commit as '${git_commit}'"
100-
}
101-
// convert 'github.com/deis/controller-sdk-go' to 'github.com/${env.CHANGE_AUTHOR}/controller-sdk-go'
102-
go_repo = go_repo.replace('deis', env.CHANGE_AUTHOR)
31+
checkout scm
32+
// HACK: Recommended approach for getting command output is writing to and then reading a file.
33+
sh 'mkdir -p tmp'
34+
sh 'git describe --all > tmp/GIT_BRANCH'
35+
sh 'git rev-parse HEAD > tmp/GIT_COMMIT'
36+
git_branch = readFile('tmp/GIT_BRANCH').trim()
37+
git_commit = readFile('tmp/GIT_COMMIT').trim()
38+
39+
if (git_branch != "remotes/origin/master") {
40+
// Determine actual PR commit, if necessary
41+
sh 'git rev-parse HEAD | git log --pretty=%P -n 1 --date-order > tmp/MERGE_COMMIT_PARENTS'
42+
sh 'cat tmp/MERGE_COMMIT_PARENTS'
43+
merge_commit_parents = readFile('tmp/MERGE_COMMIT_PARENTS').trim()
44+
if (merge_commit_parents.length() > 40) {
45+
echo 'More than one merge commit parent signifies that the merge commit is not the PR commit'
46+
echo "Changing git_commit from '${git_commit}' to '${merge_commit_parents.take(40)}'"
47+
git_commit = merge_commit_parents.take(40)
48+
} else {
49+
echo 'Only one merge commit parent signifies that the merge commit is also the PR commit'
50+
echo "Keeping git_commit as '${git_commit}'"
10351
}
10452
}
10553
}
10654

107-
stage 'Checkout workflow-cli repo and build/deploy with appropriate updates'
108-
node('linux') {
109-
def repo = "workflow-cli"
110-
def gopath = gopath_linux()
111-
def workdir = workdir_linux(gopath, repo)
55+
def test_image = "quay.io/deisci/controller-sdk-go-dev:${git_commit.take(7)}"
11256

113-
def getBasePath = { String filepath ->
114-
def filename = filepath.lastIndexOf(File.separator)
115-
return filepath.substring(0, filename)
116-
}
117-
118-
def gcs_cleanup_cmd = "sh -c 'rm -rf /.config/*'"
119-
def gcs_bucket = "gs://workflow-cli"
120-
def gcs_key = "tmp/key.json"
57+
node('linux') {
58+
stage 'Build and push test container'
59+
checkout scm
60+
def quayUsername = "deisci+jenkins"
61+
def quayEmail = "deis+jenkins@deis.com"
62+
withCredentials([[$class: 'StringBinding',
63+
credentialsId: 'c67dc0a1-c8c4-4568-a73d-53ad8530ceeb',
64+
variable: 'QUAY_PASSWORD']]) {
65+
66+
sh """
67+
docker login -e="${quayEmail}" -u="${quayUsername}" -p="\${QUAY_PASSWORD}" quay.io
68+
docker build -t ${test_image} .
69+
docker push ${test_image}
70+
"""
71+
}
72+
}
12173

122-
def gcs_cmd = { String cmd ->
123-
gcs_cmd = "docker run --rm -v ${pwd()}/tmp:/.config -v ${pwd()}/_dist:/upload google/cloud-sdk:latest "
124-
try {
125-
sh(gcs_cmd + cmd)
126-
} catch(error) {
127-
sh(gcs_cmd + gcs_cleanup_cmd)
128-
throw error
74+
stage 'Lint and test container'
75+
parallel(
76+
lint: {
77+
node('linux') {
78+
sh "docker run --rm ${test_image} lint"
12979
}
130-
}
131-
132-
def upload_artifacts = {
133-
withCredentials([[$class: 'FileBinding', credentialsId: 'e80fd033-dd76-4d96-be79-6c272726fb82', variable: 'GCSKEY']]) {
134-
sh "mkdir -p ${getBasePath(gcs_key)}"
135-
sh "cat \"\${GCSKEY}\" > ${gcs_key}"
136-
gcs_cmd 'gcloud auth activate-service-account -q --key-file /.config/key.json'
137-
gcs_cmd "gsutil -mq cp -a public-read -r /upload/* ${gcs_bucket}"
138-
gcs_cmd gcs_cleanup_cmd
80+
},
81+
test: {
82+
node('linux') {
83+
withCredentials([[$class: 'StringBinding',
84+
credentialsId: '2da033eb-2e34-4efd-b090-ad892f348065',
85+
variable: 'CODECOV_TOKEN']]) {
86+
sh "docker run -e CODECOV_TOKEN=\${CODECOV_TOKEN} --rm ${test_image} sh -c 'test-cover.sh && curl -s https://codecov.io/bash | bash'"
87+
}
13988
}
14089
}
90+
)
14191

142-
dir(workdir) {
143-
stage "Checkout ${repo}"
144-
git url: "https://github.com/deis/${repo}.git", branch: "master"
92+
stage 'Build and Upload CLI built with SDK'
93+
94+
def gcs_bucket = "gs://workflow-cli"
95+
def wcli_image = 'quay.io/deisci/workflow-cli-dev:latest'
14596

146-
stage "Build ${repo}"
147-
if (git_branch != "remotes/origin/master") {
148-
echo "Skipping build of 386 binaries to shorten CI for Pull Requests"
149-
env.BUILD_ARCH = "amd64"
150-
}
151-
make 'bootstrap'
15297

153-
stage "Update local glide.yaml with controller-sdk-go repo '${go_repo}' and version '${git_commit}'"
98+
def upload_artifacts = { String dist_dir ->
99+
headers = "-h 'x-goog-meta-git-branch:${git_branch}' "
100+
headers += "-h 'x-goog-meta-git-sha:${git_commit}' "
101+
headers += "-h 'x-goog-meta-ci-job:${env.JOB_NAME}' "
102+
headers += "-h 'x-goog-meta-ci-number:${env.BUILD_NUMBER}' "
103+
headers += "-h 'x-goog-meta-ci-url:${env.BUILD_URL}'"
154104

155-
def pattern = "github\\.com\\/deis\\/controller-sdk-go\\n\\s+version:\\s+[a-f0-9]+"
156-
def replacement = "${go_repo.replace("/", "\\/")}\\n version: ${git_commit}"
157-
sh "perl -i -0pe 's/${pattern}/${replacement}/' glide.yaml"
105+
script = "sh -c 'echo \${GCS_KEY_JSON} | base64 -d - > /tmp/key.json "
106+
script += "&& gcloud auth activate-service-account -q --key-file /tmp/key.json "
107+
script += "&& gsutil -mq ${headers} cp -a public-read -r /upload/* ${gcs_bucket} "
108+
script += "&& rm -rf /upload/*'"
158109

159-
def glideYaml = readFile('glide.yaml')
160-
echo "Updated glide.yaml:\n${glideYaml}"
110+
withCredentials([[$class: 'StringBinding',
111+
credentialsId: '6561701c-b7b4-4796-83c4-9d87946799e4',
112+
variable: 'GCSKEY']]) {
113+
sh "docker run ${dist_dir} -e GCS_KEY_JSON=\"\${GCSKEY}\" --rm ${wcli_image} ${script}"
114+
}
115+
}
161116

162-
make 'glideup'
163-
env.REVISION = git_commit.take(7)
164-
env.VERSION = "csdk-ci-" + git_commit.take(7)
165-
make 'build-revision'
117+
def mktmp = {
118+
// Create tmp directory to store files
119+
sh 'mktemp -d > tmp_dir'
120+
tmp = readFile('tmp_dir').trim()
121+
echo "Storing binaries in ${tmp}"
122+
sh 'rm tmp_dir'
123+
return tmp
124+
}
166125

167-
stage "Deploy ${repo}"
168-
upload_artifacts()
126+
node('linux') {
127+
flags = ""
128+
if (git_branch != "remotes/origin/master") {
129+
echo "Skipping build of 386 binaries to shorten CI for Pull Requests"
130+
flags += "-e BUILD_ARCH=amd64"
169131
}
132+
133+
tmp_dir = mktmp()
134+
dist_dir = "-e DIST_DIR=/upload -v ${tmp_dir}:/upload"
135+
136+
def pattern = "github\\.com\\/deis\\/controller-sdk-go\\n\\s+version:\\s+[a-f0-9]+"
137+
replacement = "github\\.com\\/deis\\/controller-sdk-go\\n"
138+
replacement += " repo: https:\\/\\/github\\.com\\/${env.CHANGE_AUTHOR}\\/controller-sdk-go\\.git\\n"
139+
replacement += " vcs: git\\n"
140+
replacement += " version: ${git_commit}"
141+
142+
build_script = "sh -c 'perl -i -0pe \"s/${pattern}/${replacement}/\" glide.yaml "
143+
build_script += "&& rm -rf glide.lock vendor/github.com/deis/controller-sdk-go "
144+
build_script += "&& glide install "
145+
build_script += "&& make build-revision'"
146+
sh "docker run ${flags} -e REVISION=${git_commit.take(7)} ${dist_dir} --rm ${wcli_image} ${build_script}"
147+
148+
upload_artifacts(dist_dir)
149+
sh "rm -rf ${tmp_dir}"
170150
}
171151

172152
stage 'Trigger e2e tests'
@@ -193,7 +173,7 @@ waitUntil {
193173
<div>Author: ${env.CHANGE_AUTHOR}<br/>
194174
Branch: ${env.BRANCH_NAME}<br/>
195175
Commit: ${env.CHANGE_TITLE}<br/>
196-
<p><a href="${env.BUILD_URL}console/">Click here</a> to view build logs.</p>
176+
<p><a href="${env.BUILD_URL}console">Click here</a> to view build logs.</p>
197177
<p><a href="${env.BUILD_URL}input/">Click here</a> to restart e2e.</p>
198178
</div>
199179
</html>

Makefile

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# the filepath to this repository, relative to $GOPATH/src
22
repo_path = github.com/deis/controller-sdk-go
33

4+
REVISION ?= $(shell git rev-parse --short HEAD)
5+
REGISTRY ?= quay.io/
6+
IMAGE_PREFIX ?= deisci
7+
IMAGE := ${REGISTRY}${IMAGE_PREFIX}/controller-sdk-go-dev:${REVISION}
8+
49
DEV_ENV_IMAGE := quay.io/deis/go-dev:0.16.0
510
DEV_ENV_WORK_DIR := /go/src/${repo_path}
611
DEV_ENV_PREFIX := docker run --rm -e CGO_ENABLED=0 -v ${CURDIR}:${DEV_ENV_WORK_DIR} -w ${DEV_ENV_WORK_DIR}
@@ -19,17 +24,13 @@ bootstrap:
1924
glideup:
2025
${DEV_ENV_CMD} glide up
2126

22-
test: test-style test-unit
23-
24-
test-style:
25-
${DEV_ENV_CMD} lint
27+
test-style: build-test-image
28+
docker run --rm ${IMAGE} lint
2629

27-
test-unit:
28-
${DEV_ENV_PREFIX_CGO_ENABLED} ${DEV_ENV_IMAGE} sh -c '${GOTEST} $$(glide nv)'
30+
test: build-test-image
31+
docker run --rm ${IMAGE} test
2932

30-
test-cover: test-style
31-
${DEV_ENV_PREFIX_CGO_ENABLED} ${DEV_ENV_IMAGE} test-cover.sh
33+
build-test-image:
34+
docker build -t ${IMAGE} .
3235

33-
# Set local user as owner for files
34-
fileperms:
35-
${DEV_ENV_PREFIX_CGO_ENABLED} ${DEV_ENV_IMAGE} chown -R ${UID}:${GID} .
36+
push-test-image: build-test-image

_scripts/test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
CGO_ENABLED=1 go test --race $(glide novendor)

0 commit comments

Comments
 (0)