Skip to content

Commit 6f3f18e

Browse files
committed
chore: introduce build.yaml
Signed-off-by: Derek Su <derek.su@suse.com>
1 parent 14e81ef commit 6f3f18e

3 files changed

Lines changed: 130 additions & 14 deletions

File tree

.github/workflows/build.yaml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: build
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- main
7+
- v*
8+
tags:
9+
- v*
10+
pull_request:
11+
workflow_dispatch:
12+
jobs:
13+
build:
14+
name: Build binaries
15+
runs-on: ubuntu-latest
16+
outputs:
17+
image_tag: ${{ steps.build_info.outputs.image_tag }}
18+
19+
steps:
20+
- name: Checkout code
21+
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
22+
23+
- id: build_info
24+
name: Declare build info
25+
run: |
26+
image_tag=''
27+
28+
branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}
29+
ref=${{ github.ref }}
30+
if [[ "$ref" =~ 'refs/tags/' ]]; then
31+
image_tag=${{ github.ref_name }}
32+
elif [[ "$ref" =~ 'refs/heads/' ]]; then
33+
image_tag="${branch}-head"
34+
fi
35+
36+
echo "image_tag=${image_tag}" >>$GITHUB_OUTPUT
37+
38+
# Build binaries
39+
- name: Run ci
40+
run: make ci
41+
42+
build_push_image:
43+
name: Build and push image
44+
runs-on: ubuntu-latest
45+
needs: build
46+
if: ${{ startsWith(github.ref, 'refs/heads/') || startsWith(github.ref, 'refs/tags/') }}
47+
steps:
48+
- name: Checkout code
49+
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
50+
51+
- name: Login to Docker Hub
52+
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
53+
with:
54+
username: ${{ secrets.DOCKER_USERNAME }}
55+
password: ${{ secrets.DOCKER_PASSWORD }}
56+
57+
# For multi-platform support
58+
- name: Set up QEMU
59+
uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3
60+
- name: Set up Docker Buildx
61+
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3
62+
63+
# longhornio/kbench image
64+
- name: Build and publish image
65+
env:
66+
REPO: docker.io/longhornio
67+
TAG: ${{ needs.build.outputs.image_tag }}
68+
TARGET_PLATFORMS: linux/amd64,linux/arm64
69+
run: make workflow-image-build-push

Makefile

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
PROJECT := kbench
12
MACHINE := longhorn
3+
DEFAULT_PLATFORMS := linux/amd64,linux/arm64
24

35
.PHONY: build validate ci package clean
46

57
buildx-machine:
6-
@docker buildx create --name=$(MACHINE) 2>/dev/null || true
8+
@docker buildx create --name=$(MACHINE) --platform=$(DEFAULT_PLATFORMS) 2>/dev/null || true
9+
docker buildx inspect $(MACHINE)
710

811
build: buildx-machine
912
docker buildx build --builder=$(MACHINE) --target build-artifacts --output type=local,dest=. -f Dockerfile .
@@ -14,8 +17,14 @@ validate:
1417
ci: buildx-machine
1518
docker buildx build --builder=$(MACHINE) --target ci-artifacts --output type=local,dest=. -f Dockerfile .
1619

17-
package: build
18-
./scripts/package
20+
package:
21+
bash scripts/package
22+
23+
.PHONY: workflow-image-build-push workflow-image-build-push-secure
24+
workflow-image-build-push: buildx-machine
25+
MACHINE=$(MACHINE) PUSH='true' IMAGE_NAME=$(PROJECT) bash scripts/package
26+
workflow-image-build-push-secure: buildx-machine
27+
MACHINE=$(MACHINE) PUSH='true' IMAGE_NAME=$(PROJECT) IS_SECURE=true bash scripts/package
1928

2029
clean:
2130
rm -rf bin dist

scripts/package

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,57 @@ set -e
33

44
source $(dirname $0)/version
55

6-
ARCH=${ARCH:-"amd64"}
7-
SUFFIX=""
8-
[ "${ARCH}" != "amd64" ] && SUFFIX="_${ARCH}"
9-
106
cd $(dirname $0)/..
117

12-
TAG=${TAG:-${VERSION}${SUFFIX}}
13-
REPO=${REPO:-yasker}
8+
command -v buildx >/dev/null && BUILD_CMD=(buildx) || BUILD_CMD=(docker buildx)
9+
10+
# read configurable parameters
11+
REPO=${REPO:-longhornio}
12+
IMAGE_NAME=${IMAGE_NAME:-'kbench'}
13+
TAG=${TAG:-${VERSION}}
14+
PUSH=${PUSH:-'false'}
15+
IS_SECURE=${IS_SECURE:-'false'}
16+
MACHINE=${MACHINE:-''}
17+
TARGET_PLATFORMS=${TARGET_PLATFORMS:-''}
18+
IID_FILE=${IID_FILE:-''}
19+
IID_FILE_FLAG=${IID_FILE_FLAG:-''}
20+
21+
IMAGE="${REPO}/${IMAGE_NAME}:${TAG}"
22+
23+
BUILDER_ARGS=()
24+
[[ ${MACHINE} ]] && BUILDER_ARGS+=('--builder' "${MACHINE}")
25+
26+
IFS=' ' read -r -a IID_FILE_ARGS <<<"${IID_FILE_FLAG}"
27+
[[ -n "${IID_FILE}" && ${#IID_FILE_ARGS} == 0 ]] && IID_FILE_ARGS=('--iidfile' "${IID_FILE}")
1428

15-
if echo $TAG | grep -q dirty; then
16-
TAG=dev
29+
BUILDX_ARGS=()
30+
31+
if [[ "${PUSH}" == 'true' ]]; then
32+
BUILDX_ARGS+=('--push')
33+
else
34+
BUILDX_ARGS+=('--load')
1735
fi
1836

19-
IMAGE=${REPO}/kbench:${TAG}
20-
docker build -t ${IMAGE} -f package/Dockerfile .
21-
echo Built ${IMAGE}
37+
[[ ${IS_SECURE} == 'true' ]] && BUILDX_ARGS+=('--sbom=true' '--attest' 'type=provenance,mode=max')
38+
[[ ${TARGET_PLATFORMS} ]] && BUILDX_ARGS+=('--platform' "${TARGET_PLATFORMS}")
39+
40+
# update base IMAGE to get latest changes
41+
grep 'FROM.*/' package/Dockerfile | awk '{print $2}' | while read -r BASE_IMAGE
42+
do
43+
docker pull "${BASE_IMAGE}"
44+
done
45+
46+
IMAGE_BUILD_CMD_ARGS=(
47+
build --no-cache \
48+
"${BUILDER_ARGS[@]}" \
49+
"${IID_FILE_ARGS[@]}" \
50+
"${BUILDX_ARGS[@]}" \
51+
-t "${IMAGE}" -f package/Dockerfile .
52+
)
53+
echo "${BUILD_CMD[@]}" "${IMAGE_BUILD_CMD_ARGS[@]}"
54+
"${BUILD_CMD[@]}" "${IMAGE_BUILD_CMD_ARGS[@]}"
55+
56+
echo "Built ${IMAGE}"
57+
58+
mkdir -p ./bin
59+
echo "${IMAGE}" > ./bin/latest_image

0 commit comments

Comments
 (0)