Skip to content

Commit

Permalink
Build args support (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Firehed authored May 18, 2022
1 parent 2b1b59f commit 5d49c4c
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 2 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/self-test-multistage-docker-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
- uses: ./
id: build
with:
build-args: BUILD_ARG_1=hello, BUILD_ARG_2=goodbye, version=${{ github.sha }}
dockerfile: examples/Dockerfile
# repository: firehed/actions
# repository: gcr.io/firehed/actions
Expand All @@ -56,6 +57,22 @@ jobs:
--rm
${{ needs.build.outputs.testenv-tag }}
echo "I'm a test!"

- name: get build arg1
id: build-args
run: docker run
--rm
${{ needs.build.outputs.testenv-tag }}
sh examples/print-outputs.sh

- name: validate build arg 1
if: ${{ steps.build-args.outputs.arg1 != 'hello' }}
run: exit 1

- name: validate build arg 2
if: ${{ steps.build-args.outputs.arg2 != 'goodbye' }}
run: exit 1

- name: display server
run: echo ${{ needs.build.outputs.server-tag }}

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ While the initial build will, of course, be performed from scratch, subsequent b
| `testenv-stage` | no | | Name of stage for test environment |
| `dockerfile` | no | `Dockerfile` | Path to the Dockerfile |
| `quiet` | no | `true` | Should docker commands be passed `--quiet` |
| `build-args` | no | | Comma-separated list of `--build-arg` flags. |

## Outputs

Expand Down Expand Up @@ -69,6 +70,7 @@ jobs:
stages: env, configured
testenv-stage: testenv
server-stage: server
build-args: arg1=val1, arg2=val2

# This assumes your testenv actually runs the tests, and
# exits 0 if they all pass and nonzero on failure
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ author: Eric Stern
description: Build Docker images to maximize layer caching and minimize build times

inputs:
build-args:
description: A comma-separated list of `--build-arg` flags
required: false
default: ''
dockerfile:
description: Path to Dockerfile
required: false
Expand Down
10 changes: 9 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions examples/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ COPY . .
CMD ls

FROM configured AS testenv
ARG BUILD_ARG_1
ARG BUILD_ARG_2
RUN apk add --update --no-cache libzip-dev
RUN echo ${BUILD_ARG_1} > build_arg_1.txt
RUN echo ${BUILD_ARG_2} > build_arg_2.txt
COPY . .
RUN echo bar > bar.txt
CMD ls
2 changes: 2 additions & 0 deletions examples/print-outputs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
echo "::set-output name=arg1::$(cat build_arg_1.txt)"
echo "::set-output name=arg2::$(cat build_arg_2.txt)"
7 changes: 7 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ export function getBaseStages(): string[] {
.filter(stage => stage !== '')
}

export function getBuildArgs(): string[] {
return core.getInput('build-args')
.split(',')
.map(arg => arg.trim())
.filter(arg => arg !== '')
}

export function getAllStages(): string[] {
const stages = [
...getBaseStages(),
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
isDefaultBranch,
getFullCommitHash,
getTagForRun,
getBuildArgs,
getBaseStages,
getAllStages,
getTaggedImageForStage,
Expand Down Expand Up @@ -95,11 +96,15 @@ async function buildStage(stage: string, extraTags: string[]): Promise<string> {
const targetTag = getTaggedImageForStage(stage, getTagForRun())

const cacheFromArg = getAllPossibleCacheTargets()
.flatMap(target => ['--cache-from', target])
.flatMap(target => ['--cache-from', target])

const buildArgs = getBuildArgs()
.flatMap(arg => ['--build-arg', arg])

const result = await runDockerCommand(
'build',
// '--build-arg', 'BUILDKIT_INLINE_CACHE="1"',
...buildArgs,
...cacheFromArg,
'--file', dockerfile,
'--tag', targetTag,
Expand Down

0 comments on commit 5d49c4c

Please sign in to comment.