action-docker-image-publish
Version:
Release (build, tag & push) docker images.
2 lines (1 loc) • 5 kB
JavaScript
;var e=require("@actions/core"),t=require("@actions/github"),i=require("node:child_process"),r=require("md5"),n=require("path"),s=require("node:path"),o=require("semver");function a(t,r){let n="";return new Promise(((s,o)=>{i.exec(`docker ${t}`,{env:process.env,...r||{}},((t,i,r)=>{t?e.error(t):(i&&i.length>0&&(e.info(i),n+=i),r&&r.length>0&&e.error(r))})).on("close",(e=>{0===e?s(n):o(new Error(`Docker command ${t} execution failed.`))}))}))}async function g(t){e.notice(`Pushing image: ${t}`),await a(`push ${t}`),e.notice("Pushed image")}async function c(t){e.info(`Removing image ${t}.`),await a(`image rm --force ${t}`),e.info("Removed image.")}async function u(t,i){e.notice(`Tagging image ${t} with ${i}`),await a(`tag ${t} ${i}`),e.notice("Tagged image")}function l(e,t){let i;"string"==typeof e?i=e:(i=`${e.host}/${e.owner}/${e.name}`,t=e.tag);const r=i.split("/").pop();return-1!==r.indexOf(":")||-1!==r.indexOf("@")?r:t?i+(t.startsWith("sha")?`@${t}`:`:${t}`):r}function f(e){if("boolean"==typeof e)return e;if("string"==typeof e){if("true"===(e=e.toLowerCase())||"t"===e)return!0;if("false"===e||"f"===e)return!1}}function p(e){return-1!==e.indexOf("://")?e.split("://").map((e=>p(e))).join("://"):e.replace(/\/+/g,"/")}const d={"!":"%21","#":"%23",$:"%24","&":"%26","'":"%27","(":"%28",")":"%29","*":"%2A","+":"%2B",",":"%2C","/":"%2F",":":"%3A",";":"%3B","=":"%3D","?":"%3F","@":"%40","[":"%5B","]":"%5D"};function h(e){const r=["git","clone"];"branch"===e.ref.type&&r.push(`--branch ${e.ref.value}`),r.push(`https://${e.user}:${function(e){let t="";for(let i=0;i<e.length;i++)t+=d[e[i]]||e[i];return t}(e.password)}@github.com/${t.context.repo.owner}/${t.context.repo.repo}`),r.push(e.directory),i.execSync(r.join(" "),{cwd:e.root})}Promise.resolve().then((async function(){e.info("Booting...");const d=function(){const t=f(e.getInput("cleanup"))??!0,i=e.getInput("dockerFileName")||"Dockerfile",r=e.getInput("dockerFilePath")||"",n=e.getInput("token",{required:!0}),s=f(e.getInput("gitTag"))??!0,o=e.getInput("gitTagPrefix"),a=e.getInput("gitUser",{required:!0}),g=e.getInput("gitPassword",{required:!0}),c=e.getInput("registryHost",{trimWhitespace:!0}),u=e.getInput("registryUser",{trimWhitespace:!0}),l=e.getInput("registryPassword",{trimWhitespace:!0}),p=e.getInput("registryRepository",{trimWhitespace:!0}),d=e.getInput("registryTag").split("\n").map((e=>e.trim())).filter((e=>""!==e));return 0===d.length&&d.push("latest"),{cleanup:t,dockerFileName:i,dockerFilePath:r,gitTag:s,gitTagPrefix:o,gitUser:a,gitPassword:g,registryHost:c,registryUser:u,registryPassword:l,registryRepository:p,registryTags:d,token:n}}(),y=function(e){const t=e.split("/");if(!(t.length<3||"refs"!==t[0]))switch(t[1]){case"heads":return{type:"branch",value:t.slice(2).join("/")};case"pulls":return{type:"pull",value:t[2]};case"tags":return{type:"tag",value:t.slice(2).join("/")}}}(t.context.ref);if(!y)return void e.error("The git ref could not be parsed.");const m=process.cwd(),w=".output";e.info("Cloning github repository..."),h({ref:y,directory:w,root:m,user:d.gitUser,password:d.gitPassword});const $=r(t.context.ref),b=await async function(e){return(await a(`images -q ${e}`)).length>0}($);if(i.execSync(`echo "${d.registryPassword}" | docker login ${d.registryHost} -u ${d.registryUser} --password-stdin`),!b){let i=n.join(m,w);d.dockerFilePath.length>0&&(i=n.join(i,d.dockerFilePath)),e.info("Image does not exist."),await async function(t){e.notice(`Building image: ${t.imageId}`);let i="build .";const r=[["file",t.fileName],["tag",t.imageId]];if(t.labels){const e=Object.keys(t.labels);for(let i=0;i<e.length;i++)r.push(["label",`"${e[i]}=${t.labels[e[i]]}"`])}const n=[];for(let e=0;e<r.length;e++){const[t,i]=r[e];n.push(`--${t} ${i}`)}n.length>0&&(i+=` ${n.join(" ")}`);let o=t.cwd||process.cwd();s.isAbsolute(o)||(o=s.join(process.cwd(),o)),await a(i,{cwd:o}),e.notice("Built image")}({fileName:d.dockerFileName,cwd:i,imageId:$,labels:{runId:`${t.context.runId}`,runNumber:`${t.context.runNumber}`}})}const x=p(`${d.registryHost}/${d.registryRepository}`).toLowerCase();let v;if(d.gitTag&&"tag"===y.type&&(e.info("Creating tags..."),(0===d.gitTagPrefix.length||y.value.startsWith(d.gitTagPrefix))&&(v=l(x,function(e,t){if(t&&e.startsWith(t)){if("@"===(e=e.substring(t.length))[0]&&o.valid(e.substring(1)))return e.substring(1);if("@"===e[0]&&"v"===e[1]&&o.valid(e.substring(2)))return e.substring(2)}return"v"===e[0]&&o.valid(e.substring(1))&&(e=e.substring(1)),e}(y.value,d.gitTagPrefix)),await u($,v),await g(v),await c(v)),e.info("Created tags.")),"tag"!==y.type||!d.gitTag||d.registryTags.length>0){e.info("Creating tags...");for(let e=0;e<d.registryTags.length;e++)v=l(x,d.registryTags[e]),await u($,v),await g(v),await c(v);e.info("Created tags.")}d.cleanup&&(e.info("Deleting built image..."),await c($),e.info("Deleted built image.")),i.execSync(`docker logout ${d.registryHost}`),e.info("Finished.")})).catch((t=>{t instanceof Error&&e.setFailed(t.message)}));