Skip to content

Commit f5d260c

Browse files
authored
ci: Push latest tag only when building from a tag or release (#273)
1 parent 0057a9c commit f5d260c

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

.github/workflows/image-build-and-publish.yml

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,18 @@ jobs:
2626
- name: Compute version number
2727
id: version-string
2828
run: |
29-
DATE="$(date +%Y%m%d)"
30-
COMMIT="$(git rev-parse --short HEAD)"
31-
echo "tag=0.$DATE.$GITHUB_RUN_NUMBER+ref.$COMMIT" >> "$GITHUB_OUTPUT"
29+
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
30+
# For main branch, use semver with -dev suffix
31+
echo "tag=0.0.1-dev.$GITHUB_RUN_NUMBER+$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
32+
elif [[ "${{ github.ref }}" == refs/tags/* ]]; then
33+
# For tags, use the tag as is (assuming it's semver)
34+
TAG="${{ github.ref_name }}"
35+
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
36+
else
37+
# For other branches, use branch name and run number
38+
BRANCH="${{ github.ref_name }}"
39+
echo "tag=0.0.1-$BRANCH.$GITHUB_RUN_NUMBER+$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
40+
fi
3241
3342
- name: Login to GitHub Container Registry
3443
uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 #pin@v3.4.0
@@ -46,7 +55,14 @@ jobs:
4655
- name: Build and Push Image to GHCR
4756
run: |
4857
TAG=$(echo "${{ steps.version-string.outputs.tag }}" | sed 's/+/_/g')
49-
KO_DOCKER_REPO=$BASE_REPO ko build --platform=linux/amd64,linux/arm64 --bare -t $TAG ./cmd/thv \
58+
TAGS="-t $TAG"
59+
60+
# Add latest tag only if building from a tag
61+
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
62+
TAGS="$TAGS -t latest"
63+
fi
64+
65+
KO_DOCKER_REPO=$BASE_REPO ko build --platform=linux/amd64,linux/arm64 --bare $TAGS ./cmd/thv \
5066
--image-label=org.opencontainers.image.source=https://github.com/StacklokLabs/toolhive,org.opencontainers.image.title="toolhive",org.opencontainers.image.vendor=Stacklok
5167
5268
- name: Sign Image with Cosign
@@ -56,6 +72,11 @@ jobs:
5672
TAG=$(echo "${{ steps.version-string.outputs.tag }}" | sed 's/+/_/g')
5773
# Sign the ko image
5874
cosign sign -y $BASE_REPO:$TAG
75+
76+
# Sign the latest tag if building from a tag
77+
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
78+
cosign sign -y $BASE_REPO:latest
79+
fi
5980
6081
operator-image-build-and-publish:
6182
runs-on: ubuntu-latest
@@ -117,7 +138,14 @@ jobs:
117138
- name: Build and Push Image to GHCR
118139
run: |
119140
TAG=$(echo "${{ steps.version-string.outputs.tag }}" | sed 's/+/_/g')
120-
KO_DOCKER_REPO=$BASE_REPO ko build --platform=linux/amd64,linux/arm64 --bare -t $TAG ./cmd/thv-operator \
141+
TAGS="-t $TAG"
142+
143+
# Add latest tag only if building from a tag
144+
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
145+
TAGS="$TAGS -t latest"
146+
fi
147+
148+
KO_DOCKER_REPO=$BASE_REPO ko build --platform=linux/amd64,linux/arm64 --bare $TAGS ./cmd/thv-operator \
121149
--image-label=org.opencontainers.image.source=https://github.com/StacklokLabs/toolhive,org.opencontainers.image.title="toolhive-operator",org.opencontainers.image.vendor=Stacklok
122150
123151
- name: Sign Image with Cosign
@@ -127,3 +155,8 @@ jobs:
127155
TAG=$(echo "${{ steps.version-string.outputs.tag }}" | sed 's/+/_/g')
128156
# Sign the ko image
129157
cosign sign -y $BASE_REPO:$TAG
158+
159+
# Sign the latest tag if building from a tag
160+
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
161+
cosign sign -y $BASE_REPO:latest
162+
fi

.github/workflows/releaser.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ jobs:
105105
fi
106106
echo "hashes=$hashes" >> $GITHUB_OUTPUT
107107
108+
image-build-and-push:
109+
name: Build and Sign Image
110+
needs: [ release ]
111+
permissions:
112+
contents: write
113+
packages: write
114+
id-token: write
115+
uses: ./.github/workflows/image-build-and-publish.yml
116+
108117
# provenance:
109118
# name: Generate provenance (SLSA3)
110119
# needs:

0 commit comments

Comments
 (0)