Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add buildArgs option for Docker build #932

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build-push-image/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 14 additions & 1 deletion build-push-image/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> {
const buildArgs: Record<string, string> = {}
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')
Expand All @@ -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)
Expand All @@ -44,9 +56,10 @@ async function run() {
labels,
image,
useSSH,
buildArgs,
})

await dockerPush(image)
}

run().catch(setFailed)
run().catch(setFailed)
9 changes: 8 additions & 1 deletion lib/dockerCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type BuildOptions = {
labels: { [key: string]: string }
image: Image
useSSH: boolean
buildArgs: Record<string, string>
}

export type Image = {
Expand All @@ -45,6 +46,7 @@ export async function dockerBuild({
labels,
image,
useSSH,
buildArgs, // Add buildArgs to function parameters
}: BuildOptions) {
info('Building image...')

Expand All @@ -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, {
Expand Down
Loading