|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +constfs=require('fs/promises'); |
| 4 | +constpath=require('path'); |
| 5 | +constexec=require('@actions/exec'); |
| 6 | + |
| 7 | +constTAG_FILE=process.env.TAGS_FILE||'tags.txt'; |
| 8 | + |
| 9 | +asyncfunctionrun(cmd,args,opts={}){ |
| 10 | +constres=awaitexec.getExecOutput(cmd,args,{ |
| 11 | +ignoreReturnCode: opts.ignoreReturnCode||false, |
| 12 | +silent: true, |
| 13 | +}); |
| 14 | +returnres; |
| 15 | +} |
| 16 | + |
| 17 | +functionparseNameAndTag(ref){ |
| 18 | +constlastSlash=ref.lastIndexOf('/'); |
| 19 | +consttagSep=ref.indexOf(':',lastSlash+1); |
| 20 | +if(tagSep===-1)return{name: ref,tag: ''}; |
| 21 | +return{name: ref.slice(0,tagSep),tag: ref.slice(tagSep+1)}; |
| 22 | +} |
| 23 | + |
| 24 | +functionderiveBaseRef(archRef){ |
| 25 | +const{ name, tag }=parseNameAndTag(archRef); |
| 26 | +if(!tag)returnnull; |
| 27 | +if(tag.endsWith('-amd64'))return`${name}:${tag.slice(0,-'-amd64'.length)}`; |
| 28 | +if(tag.endsWith('-arm64'))return`${name}:${tag.slice(0,-'-arm64'.length)}`; |
| 29 | +returnnull; |
| 30 | +} |
| 31 | + |
| 32 | +functionarchFromTag(tag){ |
| 33 | +if(tag.endsWith('-amd64'))return'amd64'; |
| 34 | +if(tag.endsWith('-arm64'))return'arm64'; |
| 35 | +returnnull; |
| 36 | +} |
| 37 | + |
| 38 | +asyncfunctiongetSourceDigest(ref){ |
| 39 | +constres=awaitrun('docker',['buildx','imagetools','inspect',ref,'--format','{{.Digest}}'],{ignoreReturnCode: true}); |
| 40 | +if(res.exitCode!==0)thrownewError(`Failed to inspect source image: ${ref}`); |
| 41 | +constout=res.stdout.trim(); |
| 42 | +if(!out.startsWith('sha256:'))thrownewError(`Unexpected digest for ${ref}: ${out}`); |
| 43 | +returnout; |
| 44 | +} |
| 45 | + |
| 46 | +asyncfunctiongetCompositeManifest(baseRef){ |
| 47 | +constres=awaitrun('docker',['buildx','imagetools','inspect',baseRef,'--format','{{json .Manifest}}'],{ignoreReturnCode: true}); |
| 48 | +if(res.exitCode!==0)returnnull; |
| 49 | +try{ |
| 50 | +returnJSON.parse(res.stdout.trim()); |
| 51 | +}catch{ |
| 52 | +returnnull; |
| 53 | +} |
| 54 | +} |
| 55 | + |
| 56 | +functioncompositeDigestMap(manifestObj){ |
| 57 | +if(!manifestObj?.manifests)returnnull; |
| 58 | +constmap={}; |
| 59 | +for(constmofmanifestObj.manifests){ |
| 60 | +constarch=m?.platform?.architecture; |
| 61 | +constdigest=m?.digest; |
| 62 | +if(arch&&digest)map[arch]=digest; |
| 63 | +} |
| 64 | +returnmap; |
| 65 | +} |
| 66 | + |
| 67 | +asyncfunctionrecreateComposite(baseRef,sources){ |
| 68 | +constargs=['buildx','imagetools','create','-t',baseRef, ...sources]; |
| 69 | +constres=awaitrun('docker',args,{ignoreReturnCode: true}); |
| 70 | +if(res.exitCode!==0)thrownewError(`Failed to create composite for ${baseRef}`); |
| 71 | +} |
| 72 | + |
| 73 | +asyncfunctionprocessGroup(baseRef,archRefs){ |
| 74 | +console.log(`\n==> Processing ${baseRef}`); |
| 75 | + |
| 76 | +constsources=[]; |
| 77 | +constsourceDigests={}; |
| 78 | +for(constarchof['amd64','arm64']){ |
| 79 | +constref=archRefs[arch]; |
| 80 | +if(ref){ |
| 81 | +constdigest=awaitgetSourceDigest(ref); |
| 82 | +sourceDigests[arch]=digest; |
| 83 | +sources.push(ref); |
| 84 | +console.log(` Source ${arch} digest: ${digest}`); |
| 85 | +} |
| 86 | +} |
| 87 | + |
| 88 | +if(sources.length===0){ |
| 89 | +console.warn(` Skipping: no valid arch variants for ${baseRef}`); |
| 90 | +return{ baseRef,status: 'skipped'}; |
| 91 | +} |
| 92 | + |
| 93 | +constmanifestObj=awaitgetCompositeManifest(baseRef); |
| 94 | +letneedsCreate=false; |
| 95 | +letreason=''; |
| 96 | + |
| 97 | +if(!manifestObj){ |
| 98 | +needsCreate=true; |
| 99 | +reason='not found'; |
| 100 | +}else{ |
| 101 | +constmap=compositeDigestMap(manifestObj); |
| 102 | +if(!map){ |
| 103 | +needsCreate=true; |
| 104 | +reason='not a manifest list'; |
| 105 | +}else{ |
| 106 | +for(constarchofObject.keys(sourceDigests)){ |
| 107 | +if(map[arch]!==sourceDigests[arch]){ |
| 108 | +needsCreate=true; |
| 109 | +reason='outdated digests'; |
| 110 | +break; |
| 111 | +} |
| 112 | +} |
| 113 | +} |
| 114 | +} |
| 115 | + |
| 116 | +if(needsCreate){ |
| 117 | +console.log(` Creating manifest list (${reason}) -> ${baseRef}`); |
| 118 | +awaitrecreateComposite(baseRef,sources); |
| 119 | +return{ baseRef,status: 'updated', reason }; |
| 120 | +}else{ |
| 121 | +console.log(` Up to date: ${baseRef}`); |
| 122 | +return{ baseRef,status: 'ok'}; |
| 123 | +} |
| 124 | +} |
| 125 | + |
| 126 | +asyncfunctionmain(){ |
| 127 | +constfilePath=path.resolve(process.cwd(),TAG_FILE); |
| 128 | +constcontent=awaitfs.readFile(filePath,'utf8'); |
| 129 | +constlines=content.split(/\r?\n/).map(s=>s.trim()).filter(Boolean); |
| 130 | + |
| 131 | +constgroups=newMap(); |
| 132 | +for(constlineoflines){ |
| 133 | +const{ tag }=parseNameAndTag(line); |
| 134 | +constarch=archFromTag(tag); |
| 135 | +constbaseRef=deriveBaseRef(line); |
| 136 | +if(!arch||!baseRef){ |
| 137 | +console.warn(`Skipping invalid line: ${line}`); |
| 138 | +continue; |
| 139 | +} |
| 140 | +constg=groups.get(baseRef)||{}; |
| 141 | +g[arch]=line; |
| 142 | +groups.set(baseRef,g); |
| 143 | +} |
| 144 | + |
| 145 | +if(groups.size===0){ |
| 146 | +console.error('No valid tags found in tags.txt'); |
| 147 | +process.exitCode=2; |
| 148 | +return; |
| 149 | +} |
| 150 | + |
| 151 | +letupdated=0; |
| 152 | +letskipped=0; |
| 153 | +for(const[baseRef,archRefs]ofgroups.entries()){ |
| 154 | +try{ |
| 155 | +constres=awaitprocessGroup(baseRef,archRefs); |
| 156 | +if(res.status==='updated')updated++; |
| 157 | +if(res.status==='skipped')skipped++; |
| 158 | +}catch(e){ |
| 159 | +console.error(`Error processing ${baseRef}: ${e.message}`); |
| 160 | +process.exitCode=1; |
| 161 | +} |
| 162 | +} |
| 163 | + |
| 164 | +console.log(`\nSummary: ${groups.size} composites checked, ${updated} updated, ${skipped} skipped.`); |
| 165 | +} |
| 166 | + |
| 167 | +main().catch(err=>{ |
| 168 | +console.error(err); |
| 169 | +process.exit(1); |
| 170 | +}); |
0 commit comments