@@ -6,6 +6,45 @@ import { fileURLToPath } from "node:url";
66const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
77const 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 - 9 a - 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+
948execFileSync (
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