Skip to content

Commit 73d839a

Browse files
refactor(build): simplify container build process and derive image coordinates
1 parent 3d8152a commit 73d839a

File tree

1 file changed

+29
-34
lines changed

1 file changed

+29
-34
lines changed

.github/workflows/container-build.yaml

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,36 @@ jobs:
1616
build:
1717
runs-on: ubuntu-latest
1818
timeout-minutes: 60
19-
container:
20-
image: moby/buildkit:latest
21-
options: --privileged
2219
steps:
2320
- name: Checkout code
2421
uses: actions/checkout@v5
25-
- name: Build container
22+
23+
- name: Derive image coordinates
24+
id: meta
2625
run: |
27-
# ghcr requires lowercase repository names; normalize `MagicMirrorOrg/MagicMirror-3rd-Party-Modules`
28-
REPO="$(echo $GITHUB_REPOSITORY | tr '[:upper:]' '[:lower:]')"
29-
case "${{ github.event_name }}" in
30-
workflow_dispatch)
31-
# manual runs should publish a refreshed image tag
32-
PARAMS="--output type=image,\"name=ghcr.io/${REPO}:${{ github.ref_name }}\",push=true"
33-
;;
34-
schedule)
35-
# nightly cron refresh publishes the image used by the website
36-
PARAMS="--output type=image,\"name=ghcr.io/${REPO}:${{ github.ref_name }}\",push=true"
37-
;;
38-
push)
39-
# changes on main publish the branch tag
40-
PARAMS="--output type=image,\"name=ghcr.io/${REPO}:${{ github.ref_name }}\",push=true"
41-
;;
42-
*)
43-
# fallback: build without pushing, but still run full build pipeline
44-
PARAMS="--output type=image,push=false"
45-
;;
46-
esac
47-
# registry credentials
48-
export DOCKER_CONFIG="$(pwd)/container"
49-
echo "{\"auths\":{\"ghcr.io\":{\"auth\":\"$(echo -n ${{ github.actor }}:${{ secrets.GITHUB_TOKEN }} | base64 -w 0)\"}}}" > $DOCKER_CONFIG/config.json
50-
# build
51-
buildctl-daemonless.sh build \
52-
--progress plain \
53-
--frontend=dockerfile.v0 \
54-
--local context=. \
55-
--local dockerfile=container \
56-
$PARAMS
26+
set -euo pipefail
27+
repo="${GITHUB_REPOSITORY,,}"
28+
ref="${GITHUB_REF_NAME}"
29+
sanitized_ref=$(echo "${ref}" | tr '[:upper:]' '[:lower:]' | sed 's#[^a-z0-9_.-]#-#g')
30+
echo "image=ghcr.io/${repo}:${sanitized_ref}" >> "$GITHUB_OUTPUT"
31+
32+
- name: Set up Docker Buildx
33+
uses: docker/setup-buildx-action@v3
34+
35+
- name: Log in to GitHub Container Registry
36+
uses: docker/login-action@v3
37+
with:
38+
registry: ghcr.io
39+
username: ${{ github.actor }}
40+
password: ${{ secrets.GITHUB_TOKEN }}
41+
42+
- name: Build and push image
43+
uses: docker/build-push-action@v6
44+
with:
45+
context: .
46+
file: container/Dockerfile
47+
push: true
48+
platforms: linux/amd64
49+
build-args: |
50+
GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
51+
tags: ${{ steps.meta.outputs.image }}

0 commit comments

Comments
 (0)