@@ -17,16 +17,23 @@ name: "Docker image - release tag"
17
17
18
18
# ----------------------------------------------------------------------------
19
19
# Trigger: When a release is published (NOT draft or prerelease)
20
+ # OR manually via workflow_dispatch
20
21
# ----------------------------------------------------------------------------
21
22
on :
22
23
release :
23
24
types : [published]
25
+ workflow_dispatch :
26
+ inputs :
27
+ tag :
28
+ description : ' Release tag (e.g., v0.1.0)'
29
+ required : true
30
+ type : string
24
31
25
32
jobs :
26
33
tag-and-push :
27
- # ------------------------------------------------------------------------
28
- # Only run if the release tag starts with 'v', and is not a draft/prerelease
29
- # ------------------------------------------------------------------------
34
+ # ------------------------------------------------------------------
35
+ # Only run if the release tag starts with 'v', and is not draft/prerelease
36
+ # ------------------------------------------------------------------
30
37
if : |
31
38
startsWith(github.event.release.tag_name, 'v') &&
32
39
github.event.release.draft == false &&
@@ -35,60 +42,42 @@ jobs:
35
42
runs-on : ubuntu-latest
36
43
37
44
permissions :
38
- contents : read # read repository info
45
+ contents : read # read repo info
39
46
packages : write # push Docker image
40
- statuses : read # check status API to ensure commit checks passed
47
+ statuses : read # check commit status API
41
48
42
49
steps :
43
- # ----------------------------------------------------------------------
44
- # Step 1: Capture release tag and resolve the commit SHA it points to
45
- # ----------------------------------------------------------------------
50
+ # ----------------------------------------------------------------
51
+ # Step 1 Capture release tag and resolve the commit SHA it points to
52
+ # ----------------------------------------------------------------
46
53
- name : 🏷️ Extract tag & commit SHA
47
54
id : meta
55
+ shell : bash
48
56
run : |
57
+ set -euo pipefail
49
58
TAG="${{ github.event.release.tag_name }}"
50
- echo "tag=$TAG" >> "$GITHUB_OUTPUT"
51
-
52
- # Method 1: Use the target_commitish from the release event if available
53
- if [ -n "${{ github.event.release.target_commitish }}" ]; then
54
- SHA="${{ github.event.release.target_commitish }}"
55
- echo "Using release target_commitish: $SHA"
56
- else
57
- # Method 2: Use GitHub API to get the commit SHA for the tag
58
- SHA=$(curl -sSL \
59
- -H "Accept: application/vnd.github+json" \
60
- -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
61
- "https://api.github.com/repos/${{ github.repository }}/git/refs/tags/$TAG" \
62
- | jq -r '.object.sha')
59
+ echo "tag=$TAG" >>"$GITHUB_OUTPUT"
63
60
64
- # If it's an annotated tag, we need to get the commit it points to
65
- if [ -z "$SHA" ] || [ "$SHA" = "null" ]; then
66
- # Try getting the tag object
67
- TAG_SHA=$(curl -sSL \
68
- -H "Accept: application/vnd.github+json" \
69
- -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
70
- "https://api.github.com/repos/${{ github.repository }}/git/refs/tags/$TAG" \
71
- | jq -r '.object.sha')
61
+ # Ask the remote repo which commit the tag points to
62
+ SHA=$(git ls-remote --quiet --refs \
63
+ "https://github.com/${{ github.repository }}.git" \
64
+ "refs/tags/$TAG" | cut -f1)
72
65
73
- # Get the commit SHA from the tag object
74
- SHA=$(curl -sSL \
75
- -H "Accept: application/vnd.github+json" \
76
- -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
77
- "https://api.github.com/repos/${{ github.repository }}/git/tags/$TAG_SHA" \
78
- | jq -r '.object.sha')
79
- fi
66
+ # Fallback to the release's target_commitish (covers annotated tags/branch releases)
67
+ if [ -z "$SHA" ] || [ "$SHA" = "null" ]; then
68
+ SHA="${{ github.event.release.target_commitish }}"
80
69
fi
81
70
82
71
echo "Resolved commit SHA: $SHA"
83
- echo "sha=$SHA" >> "$GITHUB_OUTPUT"
72
+ echo "sha=$SHA" >>"$GITHUB_OUTPUT"
84
73
85
- # ----------------------------------------------------------------------
86
- # Step 2: Confirm all checks on that commit were successful
87
- # ----------------------------------------------------------------------
74
+ # ----------------------------------------------------------------
75
+ # Step 2 Confirm all checks on that commit were successful
76
+ # ----------------------------------------------------------------
88
77
- name : ✅ Verify commit checks passed
89
78
env :
90
- SHA : ${{ steps.meta.outputs.sha }}
91
- REPO : ${{ github.repository }}
79
+ SHA : ${{ steps.meta.outputs.sha }}
80
+ REPO : ${{ github.repository }}
92
81
run : |
93
82
set -euo pipefail
94
83
STATUS=$(curl -sSL \
@@ -98,39 +87,40 @@ jobs:
98
87
| jq -r '.state')
99
88
echo "Combined status: $STATUS"
100
89
if [ "$STATUS" != "success" ]; then
101
- echo "Required workflows have not all succeeded - aborting." >&2
90
+ echo "Required workflows have not all succeeded – aborting." >&2
102
91
exit 1
103
92
fi
104
93
105
- # ----------------------------------------------------------------------
106
- # Step 3: Authenticate with GitHub Container Registry (GHCR)
107
- # ----------------------------------------------------------------------
94
+ # ----------------------------------------------------------------
95
+ # Step 3 Authenticate with GitHub Container Registry (GHCR)
96
+ # ----------------------------------------------------------------
108
97
- name : 🔐 Log in to GHCR
109
98
uses : docker/login-action@v3
110
99
with :
111
100
registry : ghcr.io
112
101
username : ${{ github.actor }}
113
102
password : ${{ secrets.GITHUB_TOKEN }}
114
103
115
- # ----------------------------------------------------------------------
116
- # Step 4: Pull the image using the commit SHA tag
117
- # ----------------------------------------------------------------------
104
+ # ----------------------------------------------------------------
105
+ # Step 4 Pull the image using the commit SHA tag
106
+ # ----------------------------------------------------------------
118
107
- name : ⬇️ Pull image by commit SHA
119
108
run : |
120
109
IMAGE="ghcr.io/$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')"
121
110
docker pull "$IMAGE:${{ steps.meta.outputs.sha }}"
122
111
123
- # ----------------------------------------------------------------------
124
- # Step 5: Tag the image with the semantic version tag
125
- # ----------------------------------------------------------------------
112
+ # ----------------------------------------------------------------
113
+ # Step 5 Tag the image with the semantic version tag
114
+ # ----------------------------------------------------------------
126
115
- name : 🏷️ Tag image with version
127
116
run : |
128
117
IMAGE="ghcr.io/$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')"
129
- docker tag "$IMAGE:${{ steps.meta.outputs.sha }}" "$IMAGE:${{ steps.meta.outputs.tag }}"
118
+ docker tag "$IMAGE:${{ steps.meta.outputs.sha }}" \
119
+ "$IMAGE:${{ steps.meta.outputs.tag }}"
130
120
131
- # ----------------------------------------------------------------------
132
- # Step 6: Push the new tag to GHCR
133
- # ----------------------------------------------------------------------
121
+ # ----------------------------------------------------------------
122
+ # Step 6 Push the new tag to GHCR
123
+ # ----------------------------------------------------------------
134
124
- name : 🚀 Push new version tag
135
125
run : |
136
126
IMAGE="ghcr.io/$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')"
0 commit comments