Skip to content

Commit 1daa105

Browse files
authored
chore(build): add support for multiarch build (#5)
* added support for multiarch image with the following supported platforms (amd64, arm64, armv7, ppc64le) * removed arm64 and ppc64le from travis builds * remove pushing image from travis. Image will be pushed from github actions * refactored build script to add new targets to make use of docker buildx * added the following workflows into github actions - release - ci - build * remove v prefix from image names * push to quay only from travis. Signed-off-by: Akhil Mohan <[email protected]>
1 parent 0ca31bd commit 1daa105

File tree

7 files changed

+236
-33
lines changed

7 files changed

+236
-33
lines changed

.github/workflows/build.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright 2018-2020 The OpenEBS Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
name: build
15+
16+
on: ['push']
17+
18+
jobs:
19+
linux-utils:
20+
runs-on: ubuntu-latest
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v2
24+
25+
- name: Set tag
26+
run: |
27+
BRANCH="${GITHUB_REF##*/}"
28+
CI_TAG=${BRANCH#v}-ci
29+
if [ ${BRANCH} = "master" ]; then
30+
CI_TAG="ci"
31+
fi
32+
echo "::set-env name=TAG::${CI_TAG}"
33+
echo "::set-env name=BRANCH::{BRANCH}"
34+
35+
- name: Set up Docker Buildx
36+
id: buildx
37+
uses: crazy-max/ghaction-docker-buildx@v1
38+
with:
39+
buildx-version: latest
40+
41+
- name: Login to GitHub Docker Registry
42+
run: echo "${DOCKERHUB_PASSWORD}" | docker login -u "${DOCKERHUB_USERNAME}" --password-stdin
43+
env:
44+
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
45+
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }}
46+
47+
- name: Build & Push Image
48+
run: |
49+
make buildx.image
50+
make buildx.push
51+

.github/workflows/pull_request.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2018-2020 The OpenEBS Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
name: ci
16+
17+
on:
18+
pull_request:
19+
branches:
20+
# on pull requests to master and release branches
21+
- master
22+
- 'v*'
23+
24+
jobs:
25+
linux-utils:
26+
runs-on: ubuntu-latest
27+
steps:
28+
- name: Checkout
29+
uses: actions/checkout@v2
30+
31+
- name: Set up Docker Buildx
32+
id: buildx
33+
uses: crazy-max/ghaction-docker-buildx@v1
34+
with:
35+
buildx-version: latest
36+
37+
- name: Build Image
38+
env:
39+
IMG_RESULT: cache
40+
run: make buildx.image

.github/workflows/release.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2018-2020 The OpenEBS Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
name: release
15+
16+
on:
17+
create:
18+
tags:
19+
- 'v*'
20+
21+
jobs:
22+
linux-utils:
23+
runs-on: ubuntu-latest
24+
steps:
25+
- name: Checkout
26+
uses: actions/checkout@v1
27+
28+
- name: Set Tag
29+
run: |
30+
echo "::set-env name=TAG::${GITHUB_REF#refs/*/v}"
31+
echo "::set-env name=RELEASE_TAG::${TAG}"
32+
33+
- name: Set up Docker Buildx
34+
id: buildx
35+
uses: crazy-max/ghaction-docker-buildx@v1
36+
with:
37+
version: latest
38+
39+
- name: Login to GitHub Docker Registry
40+
env:
41+
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
42+
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }}
43+
run: echo "${DOCKERHUB_PASSWORD}" | docker login -u "${DOCKERHUB_USERNAME}" --password-stdin
44+
45+
- name: Build & Push Image
46+
run: |
47+
make buildx.image
48+
make buildx.push

.travis.yml

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,23 @@ jobs:
1313
- IMAGE_REPO="linux-utils"
1414
- TRIVYARCH="64bit"
1515
- RELEASE_TAG_DOWNSTREAM=1
16-
- os: linux
17-
arch: arm64
18-
env:
19-
- IMAGE_REPO="linux-utils-arm64"
20-
- TRIVYARCH="ARM64"
21-
- RELEASE_TAG_DOWNSTREAM=0
22-
- os: linux
23-
arch: ppc64le
24-
env:
25-
- IMAGE_REPO="linux-utils-ppc64le"
26-
- RELEASE_TAG_DOWNSTREAM=0
2716

2817
before_install:
2918
- if [ -z $IMAGE_ORG ]; then
3019
IMAGE_ORG="openebs";
3120
export IMAGE_ORG;
3221
fi
3322
- export DIMAGE="${IMAGE_ORG}/${IMAGE_REPO}"
34-
- if [ "$TRAVIS_CPU_ARCH" != "ppc64le" ]; then
35-
export VERSION=$(curl --silent "https://api.github.com/repos/aquasecurity/trivy/releases/latest" | grep '"tag_name":' | sed -E 's/.*"v([^"]+)".*/\1/');
36-
wget https://github.com/aquasecurity/trivy/releases/download/v${VERSION}/trivy_${VERSION}_Linux-${TRIVYARCH}.tar.gz;
37-
tar zxvf trivy_${VERSION}_Linux-${TRIVYARCH}.tar.gz;
38-
fi
23+
- export VERSION=$(curl --silent "https://api.github.com/repos/aquasecurity/trivy/releases/latest" | grep '"tag_name":' | sed -E 's/.*"v([^"]+)".*/\1/')
24+
- wget https://github.com/aquasecurity/trivy/releases/download/v${VERSION}/trivy_${VERSION}_Linux-${TRIVYARCH}.tar.gz
25+
- tar zxvf trivy_${VERSION}_Linux-${TRIVYARCH}.tar.gz
26+
3927

4028
script:
4129
- make image
4230
- make test
43-
- if [ "$TRAVIS_CPU_ARCH" != "ppc64le" ]; then
44-
./trivy --exit-code 0 --severity HIGH --no-progress ${DIMAGE}:ci;
45-
./trivy --exit-code 1 --severity CRITICAL --no-progress ${DIMAGE}:ci;
46-
fi
31+
- ./trivy --exit-code 0 --severity HIGH --no-progress ${DIMAGE}:ci
32+
- ./trivy --exit-code 1 --severity CRITICAL --no-progress ${DIMAGE}:ci
4733
- make push
4834
# If this build is running due to travis release tag, and
4935
# this job indicates to push the release downstream, then

Makefile

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@
1818
# set the shell to bash in case some environments use sh
1919
SHELL:=/bin/bash
2020

21+
# Default behaviour is to use buildx only for github actions, until the Travis workflow is deprecated.
22+
BUILDX:=false
23+
24+
ifeq (${IMAGE_ORG}, )
25+
IMAGE_ORG="openebs"
26+
export IMAGE_ORG
27+
endif
2128

2229
# Determine the DIMAGE associated with given arch/os
2330
ifeq (${DIMAGE}, )
2431
#Default image name
2532
DIMAGE:=openebs/linux-utils
26-
XC_ARCH:=$(shell uname -m)
27-
ifeq (${XC_ARCH},aarch64)
28-
DIMAGE="openebs/linux-utils-arm64"
29-
else ifeq (${XC_ARCH},ppc64le)
30-
DIMAGE="openebs/linux-utils-ppc64le"
31-
endif
3233
export DIMAGE
3334
endif
3435

@@ -81,3 +82,5 @@ test:
8182
.PHONY: push
8283
push:
8384
./buildscripts/push;
85+
86+
include Makefile.buildx.mk

Makefile.buildx.mk

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright 2018-2020 The OpenEBS Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
ifeq (${TAG}, )
16+
export TAG=ci
17+
endif
18+
19+
# default list of platforms for which multiarch image is built
20+
ifeq (${PLATFORMS}, )
21+
export PLATFORMS="linux/amd64,linux/arm64,linux/arm/v7,linux/ppc64le"
22+
endif
23+
24+
# if IMG_RESULT is unspecified, by default the image will be pushed to registry
25+
ifeq (${IMG_RESULT}, load)
26+
export PUSH_ARG="--load"
27+
# if load is specified, image will be built only for the build machine architecture.
28+
export PLATFORMS="local"
29+
else ifeq (${IMG_RESULT}, cache)
30+
# if cache is specified, image will only be available in the build cache, it won't be pushed or loaded
31+
# therefore no PUSH_ARG will be specified
32+
else
33+
export PUSH_ARG="--push"
34+
endif
35+
36+
DOCKERX_IMAGE=${IMAGE_ORG}/linux-utils:${TAG}
37+
38+
.PHONY: buildx.image
39+
buildx.image:
40+
@if ! docker buildx ls | grep -q container-builder; then\
41+
docker buildx create --platform ${PLATFORMS} --name container-builder --use;\
42+
fi
43+
@docker buildx build --platform ${PLATFORMS} \
44+
-t "$(DOCKERX_IMAGE)" ${DBUILD_ARGS} -f Dockerfile \
45+
. ${PUSH_ARG}
46+
@echo "--> Build docker image: $(DOCKERX_IMAGE)"
47+
@echo
48+
49+
.PHONY: buildx.push
50+
buildx.push:
51+
BUILDX=true DIMAGE=${IMAGE_ORG}/linux-utils ./buildscripts/push

buildscripts/push

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,30 @@ then
2222
exit 1
2323
fi
2424

25+
function pushBuildx() {
26+
BUILD_TAG="latest"
27+
TARGET_IMG=${DIMAGE}
28+
29+
# TODO Currently ci builds with commit tag will not be generated,
30+
# since buildx does not support multiple repo
31+
# if not a release build set the tag and ci image
32+
if [ -z "${RELEASE_TAG}" ]; then
33+
return
34+
# BUILD_ID=$(git describe --tags --always)
35+
# BUILD_TAG="${BRANCH}-${BUILD_ID}"
36+
# TARGET_IMG="${DIMAGE}-ci"
37+
fi
38+
39+
echo "Tagging and pushing ${DIMAGE}:${TAG} as ${TARGET_IMG}:${BUILD_TAG}"
40+
docker buildx imagetools create "${DIMAGE}:${TAG}" -t "${TARGET_IMG}:${BUILD_TAG}"
41+
}
42+
43+
# if the push is for a buildx build
44+
if [[ ${BUILDX} ]]; then
45+
pushBuildx
46+
exit 0
47+
fi
48+
2549
IMAGEID=$( sudo docker images -q ${DIMAGE}:ci )
2650
echo "${DIMAGE}:ci -> $IMAGEID"
2751
if [ -z ${IMAGEID} ];
@@ -55,7 +79,11 @@ echo "Set the build/unique image tag as: ${BUILD_TAG}"
5579

5680
function TagAndPushImage() {
5781
REPO="$1"
58-
TAG="$2"
82+
# Trim the `v` from the TAG if it exists
83+
# Example: v1.10.0 maps to 1.10.0
84+
# Example: 1.10.0 maps to 1.10.0
85+
# Example: v1.10.0-custom maps to 1.10.0-custom
86+
TAG="${2#v}"
5987

6088
#Add an option to specify a custom TAG_SUFFIX
6189
#via environment variable. Default is no tag.
@@ -83,11 +111,7 @@ then
83111
# Push with different tags if tagged as a release
84112
# When github is tagged with a release, then Travis will
85113
# set the release tag in env TRAVIS_TAG
86-
# Trim the `v` from the TRAVIS_TAG if it exists
87-
# Example: v1.10.0 maps to 1.10.0
88-
# Example: 1.10.0 maps to 1.10.0
89-
# Example: v1.10.0-custom maps to 1.10.0-custom
90-
TagAndPushImage "${DIMAGE}" "${TRAVIS_TAG#v}"
114+
TagAndPushImage "${DIMAGE}" "${TRAVIS_TAG}"
91115
TagAndPushImage "${DIMAGE}" "latest"
92116
fi;
93117
else
@@ -108,7 +132,7 @@ then
108132
# When github is tagged with a release, then Travis will
109133
# set the release tag in env TRAVIS_TAG
110134
# Trim the `v` from the TRAVIS_TAG if it exists
111-
TagAndPushImage "quay.io/${DIMAGE}" "${TRAVIS_TAG#v}"
135+
TagAndPushImage "quay.io/${DIMAGE}" "${TRAVIS_TAG}"
112136
TagAndPushImage "quay.io/${DIMAGE}" "latest"
113137
fi;
114138
else

0 commit comments

Comments
 (0)