Skip to content

Commit

Permalink
Merge pull request #123 from chronotc/support-build-args
Browse files Browse the repository at this point in the history
support build args
  • Loading branch information
lox committed Apr 19, 2018
2 parents 0b75592 + 02e1bed commit 2806497
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
22 changes: 22 additions & 0 deletions README.md
Expand Up @@ -105,6 +105,24 @@ steps:

Note how the values in the list can either be just a key (so the value is sourced from the environment) or a KEY=VALUE pair.

## Build Arguments

You can use the [build args key in docker-compose.yml](https://docs.docker.com/compose/compose-file/#args) to set specific build arguments when building an image.

Alternatively, if you want to set build arguments when pre-building an image, this plugin offers an `args` block of it's own:

```yml
steps:
- command: generate-dist.sh
plugins:
docker-compose#v2.0.0:
build: app
args:
- MY_CUSTOM_ARG=panda
```

Note that the values in the list must be a KEY=VALUE pair.

## Pre-building the image

To speed up run parallel steps you can add a pre-building step to your pipeline, allowing all the `run` steps to skip image building:
Expand Down Expand Up @@ -252,6 +270,10 @@ The default is `${BUILDKITE_PIPELINE_SLUG}-${BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUI

Note: this option can only be specified on a `build` step.

### `args` (optional, build only)

A list of KEY=VALUE that are passed through as build arguments when image is being built.

### `env` or `environment` (optional, run only)

A list of either KEY or KEY=VALUE that are passed through as environment variables to the container.
Expand Down
10 changes: 7 additions & 3 deletions hooks/commands/build.sh
Expand Up @@ -48,14 +48,18 @@ while read -r line ; do
[[ -n "$line" ]] && services+=("$line")
done <<< "$(plugin_read_list BUILD)"

build_args=(--pull)
build_params=(--pull)

if [[ "$(plugin_read_config NO_CACHE "false")" == "true" ]] ; then
build_args+=(--no-cache)
build_params+=(--no-cache)
fi

while read -r arg ; do
[[ -n "${arg:-}" ]] && build_params+=("--build-arg" "${arg}")
done <<< "$(plugin_read_list ARGS)"

echo "+++ :docker: Building services ${services[*]}"
run_docker_compose -f "$override_file" build "${build_args[@]}" "${services[@]}"
run_docker_compose -f "$override_file" build "${build_params[@]}" "${services[@]}"

if [[ -n "$image_repository" ]]; then
echo "~~~ :docker: Pushing built images to $image_repository"
Expand Down
18 changes: 18 additions & 0 deletions tests/build.bats
Expand Up @@ -40,6 +40,24 @@ load '../lib/shared'
unstub docker-compose
}

@test "Build with build args" {
export BUILDKITE_JOB_ID=1111
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUILD=myservice
export BUILDKITE_PIPELINE_SLUG=test
export BUILDKITE_BUILD_NUMBER=1
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_ARGS_0=MYARG=0
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_ARGS_1=MYARG=1

stub docker-compose \
"-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml build --pull --build-arg MYARG=0 --build-arg MYARG=1 myservice : echo built myservice"

run $PWD/hooks/command

assert_success
assert_output --partial "built myservice"
unstub docker-compose
}

@test "Build with a repository" {
export BUILDKITE_JOB_ID=1111
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUILD=myservice
Expand Down

0 comments on commit 2806497

Please sign in to comment.