Skip to content

Commit 49d413e

Browse files
committed
ci: centralize image tag computation in reusable script
1 parent 0ae90d6 commit 49d413e

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/bash
2+
# Computes the OCI image tag for Garden Linux ccloud images
3+
#
4+
# This script centralizes the image tag format computation to ensure consistency
5+
# across all workflows (nightly, dev, upload_oci).
6+
#
7+
# Usage:
8+
# ./compute-image-tag.sh <version> [flavor]
9+
#
10+
# Arguments:
11+
# version - The version for the tag (e.g., "1877.10.1", "pr-123")
12+
# flavor - The image flavor (e.g., "metal-sci-usi-amd64"). Defaults to "metal-sci-usi-amd64".
13+
#
14+
# Environment:
15+
# GITHUB_SHA - Git commit SHA (required, set automatically by GitHub Actions)
16+
#
17+
# Output:
18+
# Prints the computed image tag to stdout
19+
#
20+
# Tag format:
21+
# {version}-{flavor}-{dashed_version}-{commit_sha_short}
22+
#
23+
# Examples:
24+
# ./compute-image-tag.sh "1877.10.1"
25+
# # Output: 1877.10.1-metal-sci-usi-amd64-1877-10-1-abcd1234
26+
#
27+
# ./compute-image-tag.sh "pr-123" "metal-capi-amd64"
28+
# # Output: pr-123-metal-capi-amd64-pr-123-abcd1234
29+
30+
set -euo pipefail
31+
32+
VERSION="${1:?Error: VERSION argument required}"
33+
FLAVOR="${2:-metal-sci-usi-amd64}"
34+
35+
if [ -z "${GITHUB_SHA:-}" ]; then
36+
echo "Error: GITHUB_SHA environment variable is required" >&2
37+
exit 1
38+
fi
39+
40+
COMMIT_SHA="${GITHUB_SHA::8}"
41+
DASHED_VERSION="${VERSION//./-}"
42+
43+
IMAGE_TAG="${VERSION}-${FLAVOR}-${DASHED_VERSION}-${COMMIT_SHA}"
44+
45+
echo "$IMAGE_TAG"

.github/workflows/dev.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,18 @@ jobs:
5555
runs-on: ubuntu-latest
5656
outputs:
5757
UPLOAD_VERSION: ${{ steps.meta.outputs.upload_version }}
58-
COMMIT_SHA: ${{ steps.meta.outputs.sha }}
58+
image_tag: ${{ steps.meta.outputs.image_tag }}
5959
steps:
60-
- name: Derive image version
60+
- name: Checkout
61+
uses: actions/checkout@v4
62+
- name: Compute image metadata
6163
id: meta
6264
run: |
6365
PR_NUMBER=${{ github.event.pull_request.number }}
6466
UPLOAD_VERSION="pr-${PR_NUMBER}"
6567
echo "upload_version=${UPLOAD_VERSION}" >> $GITHUB_OUTPUT
66-
SHA="${GITHUB_SHA::8}"
67-
echo "sha=${SHA}" >> $GITHUB_OUTPUT
68+
IMAGE_TAG=$(.github/scripts/compute-image-tag.sh "${UPLOAD_VERSION}")
69+
echo "image_tag=${IMAGE_TAG}" >> $GITHUB_OUTPUT
6870
6971
upload:
7072
name: Upload PR image to OCI
@@ -83,7 +85,7 @@ jobs:
8385
if: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && github.event.action != 'closed' }}
8486
uses: ./.github/workflows/test.yml
8587
with:
86-
image_tag: "${{ needs.meta.outputs.UPLOAD_VERSION }}-metal-sci-usi-amd64-${{ needs.meta.outputs.UPLOAD_VERSION }}-${{ needs.meta.outputs.COMMIT_SHA }}"
88+
image_tag: ${{ needs.meta.outputs.image_tag }}
8789

8890
cleanup_images:
8991
name: Cleanup PR images

.github/workflows/nightly.yaml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,15 @@ jobs:
3737
needs: [build]
3838
runs-on: ubuntu-latest
3939
outputs:
40-
VERSION: ${{ needs.build.outputs.version }}
41-
DASHED_VERSION: ${{ steps.meta.outputs.dashed_version }}
42-
COMMIT_SHA: ${{ steps.meta.outputs.sha }}
40+
image_tag: ${{ steps.meta.outputs.image_tag }}
4341
steps:
4442
- name: Checkout
4543
uses: actions/checkout@v4
46-
- name: Derive image metadata
44+
- name: Compute image tag
4745
id: meta
4846
run: |
49-
SHA="${GITHUB_SHA::8}"
50-
echo "sha=${SHA}" >> $GITHUB_OUTPUT
51-
VERSION="${{ needs.build.outputs.version }}"
52-
DASHED_VERSION=${VERSION//./-}
53-
echo "dashed_version=${DASHED_VERSION}" >> $GITHUB_OUTPUT
47+
IMAGE_TAG=$(.github/scripts/compute-image-tag.sh "${{ needs.build.outputs.version }}")
48+
echo "image_tag=${IMAGE_TAG}" >> $GITHUB_OUTPUT
5449
upload_oci:
5550
name: Run glcli to publish to OCI
5651
needs: [build]
@@ -64,4 +59,4 @@ jobs:
6459
needs: [meta, upload_oci]
6560
uses: ./.github/workflows/test.yml
6661
with:
67-
image_tag: "${{ needs.meta.outputs.VERSION }}-metal-sci-usi-amd64-${{ needs.meta.outputs.DASHED_VERSION }}-${{ needs.meta.outputs.COMMIT_SHA }}"
62+
image_tag: ${{ needs.meta.outputs.image_tag }}

0 commit comments

Comments
 (0)