#!/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"); const bitwardenSession = new BitwardenSession(); const { username: registryUsername, password: registryPassword } = (await bitwardenSession.getItem(args.registry))?.login ?? {}; if (!(registryUsername && registryPassword)) { throw new Error("where's the login info bruh"); } logger.log(`Logging in to Docker registry: ${args.registry}`); await getStdout( [ "docker", "login", "--username", registryUsername, "--password", registryPassword, args.registry, ], ); 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`, tag, buildCmd); await getStdout( buildCmd, { clearEnv: true, env: {}, }, ); const pushCmd = [ "docker", "push", tag, ]; logger.log(`pushing`, pushCmd); await getStdout(pushCmd); }; if (import.meta.main) { try { await run(); } catch (e) { logger.error("womp womp D:", e); throw e; } }