Skip to content

Commit d4aecc6

Browse files
dalehamelyonghong-song
authored andcommitted
Refactor docker image publishing
This adds support to push docker images to quay.io, like other projects in the iovisor org. It separates docker image builds into a separate github workflow, and refactors the package building process slightly, to be generic, in order to create builds for both ubuntu 16.04 and ubuntu 18.04. This provides a means to distribute intermediate apt packages between releases, and also enables uploading these as CI artifacts. As recent releases have not annotated their tags, it drops the requirement for tags to be annotated in selecting the version to use.
1 parent ec3747e commit d4aecc6

File tree

9 files changed

+225
-41
lines changed

9 files changed

+225
-41
lines changed

.github/workflows/bcc-test.yml

-35
Original file line numberDiff line numberDiff line change
@@ -92,38 +92,3 @@ jobs:
9292
# https://github.com/marketplace/actions/debugging-with-tmate
9393
# - name: Setup tmate session
9494
# uses: mxschmitt/action-tmate@v1
95-
96-
# Optionally publish container images, guarded by the GitHub secret
97-
# DOCKER_PUBLISH.
98-
# GitHub secrets can be configured as follows:
99-
# - DOCKER_PUBLISH = 1
100-
# - DOCKER_IMAGE = docker.io/myorg/bcc
101-
# - DOCKER_USERNAME = username
102-
# - DOCKER_PASSWORD = password
103-
publish:
104-
name: Publish
105-
runs-on: ubuntu-latest
106-
steps:
107-
108-
- uses: actions/checkout@v1
109-
110-
- name: Initialize workflow variables
111-
id: vars
112-
shell: bash
113-
run: |
114-
echo ::set-output name=DOCKER_PUBLISH::${DOCKER_PUBLISH}
115-
env:
116-
DOCKER_PUBLISH: "${{ secrets.DOCKER_PUBLISH }}"
117-
118-
- name: Build container image and publish to registry
119-
id: publish-registry
120-
uses: elgohr/[email protected]
121-
if: ${{ steps.vars.outputs.DOCKER_PUBLISH }}
122-
with:
123-
name: ${{ secrets.DOCKER_IMAGE }}
124-
username: ${{ secrets.DOCKER_USERNAME }}
125-
password: ${{ secrets.DOCKER_PASSWORD }}
126-
workdir: .
127-
dockerfile: Dockerfile.ubuntu
128-
snapshot: true
129-
cache: ${{ github.event_name != 'schedule' }}

.github/workflows/publish.yml

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
name: Publish Build Artifacts
2+
3+
on: push
4+
5+
jobs:
6+
publish_images:
7+
# Optionally publish container images, guarded by the GitHub secret
8+
# QUAY_PUBLISH.
9+
# To set this up, sign up for quay.io (you can connect it to your github)
10+
# then create a robot user with write access user called "bcc_buildbot",
11+
# and add the secret token for it to GitHub secrets as:
12+
# - QUAY_TOKEN = <token from quay.io>
13+
name: Publish to quay.io
14+
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
env:
18+
- NAME: xenial-release
19+
OS_RELEASE: 16.04
20+
- NAME: bionic-release
21+
OS_RELEASE: 18.04
22+
steps:
23+
24+
- uses: actions/checkout@v1
25+
26+
- name: Initialize workflow variables
27+
id: vars
28+
shell: bash
29+
run: |
30+
if [ -n "${QUAY_TOKEN}" ];then
31+
echo "Quay token is set, will push an image"
32+
echo ::set-output name=QUAY_PUBLISH::true
33+
else
34+
echo "Quay token not set, skipping"
35+
fi
36+
37+
env:
38+
QUAY_TOKEN: ${{ secrets.QUAY_TOKEN }}
39+
40+
- name: Authenticate with quay.io docker registry
41+
if: >
42+
steps.vars.outputs.QUAY_PUBLISH
43+
env:
44+
QUAY_TOKEN: ${{ secrets.QUAY_TOKEN }}
45+
run: ./scripts/docker/auth.sh ${{ github.repository }}
46+
47+
- name: Package docker image and push to quay.io
48+
if: >
49+
steps.vars.outputs.QUAY_PUBLISH
50+
run: >
51+
./scripts/docker/push.sh
52+
${{ github.repository }}
53+
${{ github.ref }}
54+
${{ github.sha }}
55+
${{ matrix.env['NAME'] }}
56+
${{ matrix.env['OS_RELEASE'] }}
57+
58+
# Uploads the packages built in docker to the github build as an artifact for convenience
59+
- uses: actions/upload-artifact@v1
60+
with:
61+
name: ${{ matrix.env['NAME'] }}
62+
path: output
63+
64+
# Optionally publish container images to custom docker repository,
65+
# guarded by presence of all required github secrets.
66+
# GitHub secrets can be configured as follows:
67+
# - DOCKER_IMAGE = docker.io/myorg/bcc
68+
# - DOCKER_USERNAME = username
69+
# - DOCKER_PASSWORD = password
70+
publish_dockerhub:
71+
name: Publish To Dockerhub
72+
runs-on: ubuntu-latest
73+
steps:
74+
75+
- uses: actions/checkout@v1
76+
77+
- name: Initialize workflow variables
78+
id: vars
79+
shell: bash
80+
run: |
81+
if [ -n "${DOCKER_IMAGE}" ] && \
82+
[ -n "${DOCKER_USERNAME}" ] && \
83+
[ -n "${DOCKER_PASSWORD}" ];then
84+
echo "Custom docker credentials set, will push an image"
85+
echo ::set-output name=DOCKER_PUBLISH::true
86+
else
87+
echo "Custom docker credentials not, skipping"
88+
fi
89+
90+
- name: Build container image and publish to registry
91+
id: publish-registry
92+
uses: elgohr/[email protected]
93+
if: ${{ steps.vars.outputs.DOCKER_PUBLISH }}
94+
with:
95+
name: ${{ secrets.DOCKER_IMAGE }}
96+
username: ${{ secrets.DOCKER_USERNAME }}
97+
password: ${{ secrets.DOCKER_PASSWORD }}
98+
workdir: .
99+
dockerfile: Dockerfile.ubuntu
100+
snapshot: true
101+
cache: ${{ github.event_name != 'schedule' }}

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ debian/**/*.log
1212
*critical.log
1313
obj-x86_64-linux-gnu
1414
examples/cgroupid/cgroupid
15+
16+
# Output from docker builds
17+
scripts/docker/output/
18+
/output/

Dockerfile.ubuntu

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
FROM ubuntu:bionic as builder
1+
ARG OS_TAG=18.04
2+
FROM ubuntu:${OS_TAG} as builder
3+
4+
ARG OS_TAG
5+
ARG BUILD_TYPE=release
6+
ARG DEBIAN_FRONTEND=noninteractive
27

38
MAINTAINER Brenden Blanco <[email protected]>
49

@@ -10,10 +15,9 @@ COPY ./ /root/bcc
1015
WORKDIR /root/bcc
1116

1217
RUN /usr/lib/pbuilder/pbuilder-satisfydepends && \
13-
./scripts/build-deb.sh
14-
18+
./scripts/build-deb.sh ${BUILD_TYPE}
1519

16-
FROM ubuntu:bionic
20+
FROM ubuntu:${OS_TAG}
1721

1822
COPY --from=builder /root/bcc/*.deb /root/bcc/
1923

scripts/build-deb.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
# helper script to be invoked by jenkins/buildbot
3+
# helper script to be invoked by jenkins/buildbot or github actions
44

55
# $1 [optional]: the build type - release | nightly | test
66
buildtype=${1:-test}

scripts/docker/auth.sh

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# For now only quay.io is supported, but this could be portable to dockerhub
5+
# and other image repositories.
6+
7+
# Forks can push using this approach if they create a quay.io bot user
8+
# with name matching of ORGNAME+bcc_buildbot, or by setting QUAY_BOT_NAME
9+
10+
git_repo=$1 # github.repository format: ORGNAME/REPONAME
11+
12+
# Set this value as QUAY_TOKEN in the github repository settings "Secrets" tab
13+
[[ -z "${QUAY_TOKEN}" ]] && echo "QUAY_TOKEN not set" && exit 0
14+
15+
# Set this to match the name of the bot user on quay.io
16+
[[ -z "${QUAY_BOT_NAME}" ]] && QUAY_BOT_NAME="bcc_buildbot"
17+
18+
quay_user="$(dirname ${git_repo})+${QUAY_BOT_NAME}"
19+
echo "${QUAY_TOKEN}" | docker login -u="${quay_user}" --password-stdin quay.io

scripts/docker/build.sh

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
# Builds debian packages using docker wrapper
4+
5+
function help() {
6+
message=$1
7+
echo "USAGE: build.sh DOCKER_REPO DOCKER_TAG OS_TAG [DISTRO]"
8+
echo "hint: ${message}"
9+
}
10+
11+
docker_repo=$1
12+
docker_tag=$2
13+
os_tag=$3
14+
distro=${4:-ubuntu}
15+
16+
[ -z "${docker_repo}" ] && help "You must specify repo, eg: quay.io/iovisoc/bcc" && exit 1
17+
[ -z "${docker_tag}" ] && help "You must specify tag, eg: bionic-release-master, latest, SHA, git tag, etc " && exit 1
18+
[ -z "${os_tag}" ] && help "You must specify os tag, eg: 18.04, bionic, etc " && exit 1
19+
20+
21+
# The main docker image build,
22+
echo "Building ${distro} ${os_tag} release docker image for ${docker_repo}:${docker_tag}"
23+
docker build -t ${docker_repo}:${docker_tag} --build-arg OS_TAG=${os_tag} -f Dockerfile.${distro} .
24+
25+
echo "Copying build artifacts to $(pwd)/output"
26+
mkdir output
27+
docker run -v $(pwd)/output:/output ${docker_repo}:${docker_tag} /bin/bash -c "cp /root/bcc/* /output"

scripts/docker/push.sh

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Push docker tags to a configured docker repo, defaulting to quay.io
5+
# You must run login.sh before running this script.
6+
7+
DEFAULT_DOCKER_REPO="quay.io"
8+
DEFAULT_RELEASE_TARGET="bionic-release" # will allow unprefixed tags
9+
10+
# Currently only support pushing to quay.io
11+
DOCKER_REPO=${DEFAULT_DOCKER_REPO}
12+
13+
git_repo=$1 # github.repository format: ORGNAME/REPONAME
14+
git_ref=$2 # github.ref format: refs/REMOTE/REF
15+
# eg, refs/heads/BRANCH
16+
# refs/tags/v0.9.6-pre
17+
git_sha=$3 # github.sha GIT_SHA
18+
type_name=$4 # build name, s/+/_/g eg, bionic-release
19+
os_tag=${5:-18.04} # numeric docker tag eg, 18.04
20+
21+
# refname will be either a branch like "master" or "some-branch",
22+
# or a tag, like "v1.17.0-pre".
23+
# When a tag is pushed, a build is done for both the branch and the tag, as
24+
# separate builds.
25+
# This is a feature specific to github actions based on the `github.ref` object
26+
refname=$(basename ${git_ref})
27+
28+
# The build type needs to be sanitized into a valid tag, replacing + with _
29+
type_tag="$(echo ${type_name} | sed 's/+/_/g')"
30+
31+
32+
echo "Triggering image build"
33+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
34+
${SCRIPT_DIR}/build.sh ${DOCKER_REPO}/${git_repo} ${git_sha}-${type_tag} ${os_tag}
35+
36+
echo "Upload image for git sha ${git_sha} to ${DOCKER_REPO}/${git_repo}"
37+
docker push ${DOCKER_REPO}/${git_repo}:${git_sha}-${type_tag}
38+
39+
echo "Push tags to branch or git tag HEAD refs"
40+
docker tag ${DOCKER_REPO}/${git_repo}:${git_sha}-${type_tag} ${DOCKER_REPO}/${git_repo}:${refname}-${type_tag}
41+
docker push ${DOCKER_REPO}/${git_repo}:${refname}-${type_tag}
42+
43+
# Only push to un-suffixed tags for the default release target build type
44+
if [[ "${type_name}" == "${DEFAULT_RELEASE_TARGET}"* ]];then
45+
46+
# Update branch / git tag ref
47+
echo "Pushing tags for ${DOCKER_REPO}/${git_repo}:${refname}"
48+
docker tag ${DOCKER_REPO}/${git_repo}:${git_sha}-${type_tag} ${DOCKER_REPO}/${git_repo}:${refname}
49+
docker push ${DOCKER_REPO}/${git_repo}:${refname}
50+
51+
if [[ "${refname}" == "master" ]];then
52+
if [[ "${edge}" == "ON" ]];then
53+
echo "This is an edge build on master, pushing ${DOCKER_REPO}/${git_repo}:edge"
54+
docker tag ${DOCKER_REPO}/${git_repo}:${git_sha}-${type_tag} ${DOCKER_REPO}/${git_repo}:edge
55+
docker push ${DOCKER_REPO}/${git_repo}:edge
56+
else
57+
echo "This is a build on master, pushing ${DOCKER_REPO}/${git_repo}:latest :SHA as well"
58+
docker tag ${DOCKER_REPO}/${git_repo}:${git_sha}-${type_tag} ${DOCKER_REPO}/${git_repo}:latest
59+
docker tag ${DOCKER_REPO}/${git_repo}:${git_sha}-${type_tag} ${DOCKER_REPO}/${git_repo}:${git_sha}
60+
docker push ${DOCKER_REPO}/${git_repo}:latest
61+
docker push ${DOCKER_REPO}/${git_repo}:${git_sha}
62+
fi
63+
fi
64+
fi

scripts/git-tag.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
git_tag_latest=$(git describe --abbrev=0)
1+
git_tag_latest=$(git describe --tags --abbrev=0)
22
git_rev_count=$(git rev-list $git_tag_latest.. --count)
33
git_rev_count=$[$git_rev_count+1]
44
git_subject=$(git log --pretty="%s" -n 1)

0 commit comments

Comments
 (0)