#!/usr/bin/env -S deno run --allow-env --allow-net --allow-run import type { BuildDockerImageJobProps } from "@liz-ci/model"; import { BitwardenSession, getRequiredEnv, getStdout, loggerWithPrefix, type LoginItem, } from "@liz-ci/utils"; const args: BuildDockerImageJobProps = { registry: getRequiredEnv("registry"), namespace: getRequiredEnv("namespace"), repository: getRequiredEnv("repository"), imageTag: getRequiredEnv("imageTag"), context: getRequiredEnv("context"), dockerfile: getRequiredEnv("dockerfile"), buildTarget: getRequiredEnv("buildTarget"), }; const logger = loggerWithPrefix(() => `[${ new Date().toISOString() }] [build_image.${args.repository}.${args.imageTag}]` ); const run = async () => { logger.log( "starting docker image build job~ (⑅˘꒳˘) let's make something cute!", ); const bitwardenSession = new BitwardenSession(); const { username: registryUsername, password: registryPassword } = (await bitwardenSession.getItem(args.registry))?.login ?? {}; if (!(registryUsername && registryPassword)) { throw new Error("oh nyo! can't find the login info (。•́︿•̀。)"); } logger.log(`logging in to docker registry: ${args.registry} (˘ω˘)`); await getStdout( [ "docker", "login", "--username", registryUsername, "--password", "$DOCKER_PASSWORD", args.registry, ], { env: { DOCKER_PASSWORD: registryPassword, }, }, ); const tag = `${args.registry}/${args.namespace}/${args.repository}:${args.imageTag}`; const buildCmd = [ "docker", "build", "--target", args.buildTarget, "-t", tag, "-f", `${args.dockerfile}`, `${args.context}`, ]; logger.log(`building image~ (◕ᴗ◕✿)`, tag, buildCmd); await getStdout( buildCmd, { clearEnv: true, env: {}, }, ); const pushCmd = [ "docker", "push", tag, ]; logger.log(`sending image to wegistwy~ (>ᴗ<)`, pushCmd); await getStdout(pushCmd); }; if (import.meta.main) { try { await run(); } catch (e) { logger.error("oh nyo! something went wrong with the build (´。﹏。`)", e); throw e; } }