Skip to content

Commit ffed947

Browse files
committed
Add GitHub Actions workflow to publish container image
Introduces publish-container.yml workflow to build and push Docker images to GitHub Container Registry on tag push or manual trigger. Ensures tags are on master before publishing, resolves image metadata, and handles Docker authentication and build steps. Skips publishing if tag is not on master.
1 parent 36945fd commit ffed947

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
name: Publish container image
2+
3+
on:
4+
push:
5+
tags:
6+
- "*"
7+
workflow_dispatch:
8+
9+
permissions:
10+
contents: read
11+
packages: write
12+
13+
jobs:
14+
build-and-push:
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0
22+
23+
- name: Ensure tagged commit is on master
24+
id: branch_check
25+
shell: bash
26+
run: |
27+
SHOULD_PUBLISH=true
28+
if [ "${GITHUB_EVENT_NAME}" = "push" ]; then
29+
git fetch origin master --depth=1
30+
if ! git merge-base --is-ancestor "${GITHUB_SHA}" "origin/master"; then
31+
echo "Tag is not on master; image publish skipped."
32+
SHOULD_PUBLISH=false
33+
fi
34+
fi
35+
36+
echo "should_publish=${SHOULD_PUBLISH}" >> "$GITHUB_OUTPUT"
37+
38+
- name: Resolve image name and version tag
39+
id: meta
40+
if: steps.branch_check.outputs.should_publish == 'true'
41+
shell: bash
42+
run: |
43+
IMAGE_NAME="$(echo "ghcr.io/${GITHUB_REPOSITORY}" | tr '[:upper:]' '[:lower:]')"
44+
45+
if [ "${GITHUB_EVENT_NAME}" = "push" ]; then
46+
VERSION_TAG="${GITHUB_REF_NAME}"
47+
else
48+
VERSION_TAG="$(git tag --sort=-v:refname | head -n1)"
49+
fi
50+
51+
if [ -z "${VERSION_TAG}" ]; then
52+
echo "No git tags were found. Create a tag before running this workflow."
53+
exit 1
54+
fi
55+
56+
echo "image_name=${IMAGE_NAME}" >> "$GITHUB_OUTPUT"
57+
echo "version_tag=${VERSION_TAG}" >> "$GITHUB_OUTPUT"
58+
59+
- name: Set up Docker Buildx
60+
if: steps.branch_check.outputs.should_publish == 'true'
61+
uses: docker/setup-buildx-action@v3
62+
63+
- name: Log in to GitHub Container Registry
64+
if: steps.branch_check.outputs.should_publish == 'true'
65+
uses: docker/login-action@v3
66+
with:
67+
registry: ghcr.io
68+
username: ${{ github.actor }}
69+
password: ${{ secrets.GITHUB_TOKEN }}
70+
71+
- name: Build and push image
72+
if: steps.branch_check.outputs.should_publish == 'true'
73+
uses: docker/build-push-action@v6
74+
with:
75+
context: ./h2s
76+
file: ./h2s/Dockerfile
77+
push: true
78+
tags: |
79+
${{ steps.meta.outputs.image_name }}:${{ steps.meta.outputs.version_tag }}
80+
${{ steps.meta.outputs.image_name }}:latest
81+
82+
- name: Report skipped publish
83+
if: steps.branch_check.outputs.should_publish != 'true'
84+
run: echo "Workflow completed without publishing because the tag is not on master."

0 commit comments

Comments
 (0)