From 4f92e2b4b4423e8713911e73a048460580a59128 Mon Sep 17 00:00:00 2001 From: Steffen Exler Date: Wed, 29 May 2024 11:45:53 +0200 Subject: [PATCH] chore: Add buildArgs option for Docker build Signed-off-by: Steffen Exler --- build-push-image/action.yml | 4 ++++ build-push-image/src/index.ts | 15 ++++++++++++++- lib/dockerCli.ts | 9 ++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/build-push-image/action.yml b/build-push-image/action.yml index b818e426..2c7754b5 100644 --- a/build-push-image/action.yml +++ b/build-push-image/action.yml @@ -34,6 +34,10 @@ inputs: description: Pass the SSH agent to the build context required: false default: 'false' + buildArgs: + description: Build arguments for Docker build + required: false + default: '' runs: using: node20 main: dist/index.js diff --git a/build-push-image/src/index.ts b/build-push-image/src/index.ts index 86395406..a2fca2bf 100644 --- a/build-push-image/src/index.ts +++ b/build-push-image/src/index.ts @@ -10,6 +10,16 @@ import { getOwnerInput, getRepoInput } from '../../lib/github' import { branchToTag, getLabels } from '../../lib/image' import { writeMetadataFile } from './metadataFile' +function parseBuildArgs(buildArgsStr: string): Record { + const buildArgs: Record = {} + const pairs = buildArgsStr.split(',') + pairs.forEach(pair => { + const [key, value] = pair.split('=') + buildArgs[key] = value + }) + return buildArgs +} + async function run() { // Inputs const namespace = getOwnerInput('namespace') @@ -20,6 +30,8 @@ async function run() { const user = getInput('user') const password = getInput('password') const useSSH = getBooleanInput('useSSH') + const buildArgsStr = getInput('buildArgs') + const buildArgs = parseBuildArgs(buildArgsStr) // Get all relevant metadata for the image const labels = getLabels(name) @@ -44,9 +56,10 @@ async function run() { labels, image, useSSH, + buildArgs, }) await dockerPush(image) } -run().catch(setFailed) +run().catch(setFailed) \ No newline at end of file diff --git a/lib/dockerCli.ts b/lib/dockerCli.ts index 48d89e85..a2ca9b2b 100644 --- a/lib/dockerCli.ts +++ b/lib/dockerCli.ts @@ -30,6 +30,7 @@ type BuildOptions = { labels: { [key: string]: string } image: Image useSSH: boolean + buildArgs: Record } export type Image = { @@ -45,6 +46,7 @@ export async function dockerBuild({ labels, image, useSSH, + buildArgs, // Add buildArgs to function parameters }: BuildOptions) { info('Building image...') @@ -53,11 +55,16 @@ export async function dockerBuild({ .map(([key, value]) => `--label "${key}=${value}"`) .join(' ') + // Concat list of build args + const buildArgsOptions = Object.entries(buildArgs) + .map(([key, value]) => `--build-arg ${key}=${value}`) + .join(' ') + const ssh = useSSH ? '--ssh default' : '' const cmd = `/usr/bin/bash -c "docker build ${ssh} -f ${dockerfile} -t ${getImageFQN( image, - )} ${labelOptions} ${context}"` + )} ${labelOptions} ${buildArgsOptions} ${context}"` debug(`Executing command:\n${cmd}`) await exec(cmd, undefined, {