Skip to content

Commit 8ab2346

Browse files
committed
Auto-resolve image version from action ref and add SHA image tags
Publish Docker images with commit SHA tags (sha-<hash>) alongside semver tags. The setup action now derives the image version from GITHUB_ACTION_REF, supporting semver tags, full SHA refs, and falling back to "1".
1 parent 4c5c9e2 commit 8ab2346

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

.github/workflows/docker-publish.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jobs:
3939
TAG="$REF_NAME"
4040
fi
4141
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
42+
echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
4243
4344
- name: Set up QEMU
4445
uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3.7.0
@@ -55,6 +56,7 @@ jobs:
5556
type=semver,pattern={{version}},value=${{ steps.version.outputs.tag }}
5657
type=semver,pattern={{major}}.{{minor}},value=${{ steps.version.outputs.tag }}
5758
type=semver,pattern={{major}},value=${{ steps.version.outputs.tag }}
59+
type=raw,value=sha-${{ steps.version.outputs.sha }},priority=100
5860
5961
- name: Login to GHCR
6062
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0

setup/action.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ inputs:
88
buildcage_version:
99
description: "Image tag"
1010
required: false
11-
default: '1'
1211
proxy_mode:
1312
description: "audit or restrict"
1413
required: false

setup/main.mjs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,45 @@ import { fileURLToPath } from "node:url";
66
const __dirname = dirname(fileURLToPath(import.meta.url));
77
const composeFile = join(__dirname, "compose.yml");
88

9+
/**
10+
* Determine the Docker image version to use.
11+
* Priority: explicit input > action ref tag > fallback "1"
12+
*
13+
* When called as `dash14/buildcage/setup@v1.0`, GITHUB_ACTION_REF is "v1.0".
14+
* Strip the "v" prefix if present, verify the image exists, then use it.
15+
* For non-v refs (commit hash, branch), check image existence with raw ref.
16+
* If the image doesn't exist, fall back to "1".
17+
*/
18+
function resolveVersion(image) {
19+
if (process.env.INPUT_BUILDCAGE_VERSION) {
20+
return process.env.INPUT_BUILDCAGE_VERSION;
21+
}
22+
23+
const ref = process.env.GITHUB_ACTION_REF || "";
24+
if (ref) {
25+
// Full SHA (40 hex chars) → prefix with "sha-" to match image tag convention
26+
const version = /^[0-9a-f]{40}$/i.test(ref) ? `sha-${ref.toLowerCase()}`
27+
: ref.startsWith("v") ? ref.slice(1)
28+
: ref;
29+
try {
30+
execFileSync("docker", ["manifest", "inspect", `${image}:${version}`], {
31+
stdio: "pipe",
32+
});
33+
return version;
34+
} catch {
35+
// Image with this version doesn't exist; fall through
36+
}
37+
}
38+
39+
return "1";
40+
}
41+
42+
const buildcageImage = (process.env.INPUT_BUILDCAGE_IMAGE
43+
|| `ghcr.io/${process.env.GITHUB_REPOSITORY}`).toLowerCase();
44+
const buildcageVersion = resolveVersion(buildcageImage);
45+
46+
console.log(`buildcage image: ${buildcageImage}:${buildcageVersion}`);
47+
948
execFileSync(
1049
"docker",
1150
["compose", "-f", composeFile, "down"],
@@ -28,8 +67,8 @@ execFileSync(
2867
ALLOWED_HTTPS_DOMAINS: process.env.INPUT_ALLOWED_HTTPS_DOMAINS || "",
2968
HTTP_PORTS: process.env.INPUT_HTTP_PORTS || "80",
3069
HTTPS_PORTS: process.env.INPUT_HTTPS_PORTS || "443",
31-
BUILDCAGE_IMAGE: process.env.INPUT_BUILDCAGE_IMAGE || `ghcr.io/${process.env.GITHUB_REPOSITORY}`.toLowerCase(),
32-
BUILDCAGE_VERSION: process.env.INPUT_BUILDCAGE_VERSION || "1",
70+
BUILDCAGE_IMAGE: buildcageImage,
71+
BUILDCAGE_VERSION: buildcageVersion,
3372
PORT: process.env.INPUT_PORT || "1234",
3473
},
3574
}

0 commit comments

Comments
 (0)