Skip to content

Commit

Permalink
Allow for global ARG substitution between stages (uber-archive#325)
Browse files Browse the repository at this point in the history
* It is correct to use global ARG for substition in child-stage

See: moby/moby#37345 (comment)

* Add missing build arg
  • Loading branch information
Rowern authored Apr 30, 2020
1 parent 3d2c1bb commit ee37808
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/parser/dockerfile/arg.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ func (d *ArgDirective) update(state *parsingState) error {
d.ResolvedVal = &d.DefaultVal
}
if !global {
// If no value is provided to this arg, we try to replace it using the global scope
// (see the testdata/build-context/global-arg/Dockerfile)
if val, ok := state.globalArgs[d.Name]; ok {
vars[d.Name] = val
d.ResolvedVal = &val
}

return state.addToCurrStage(d)
}
return nil
Expand Down
13 changes: 13 additions & 0 deletions test/python/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,16 @@ def test_build_entrypoint(registry1, storage_dir):
new_image, context_dir, storage_dir, registry=registry1.addr)
code, err = utils.docker_run_image(registry1.addr, new_image)
assert code == 0, err

def test_build_global_arg(registry1, storage_dir):
new_image = utils.new_image_name()
context_dir = os.path.join(
os.getcwd(), 'testdata/build-context/global-arg')
docker_build_args = [
"version_default=v2",
]
utils.makisu_build_image(
new_image, context_dir, storage_dir,
registry=registry1.addr, docker_args=docker_build_args)
code, err = utils.docker_run_image(registry1.addr, new_image)
assert code == 0, err
11 changes: 11 additions & 0 deletions testdata/build-context/global-arg/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ARG version_default=v1

FROM alpine:latest as base1
ARG version_default
ENV version=$version_default

FROM alpine:latest as base2
ARG version_default
ENV version2=$version_default

ENTRYPOINT if [ -z "$version" -a "$version2" = "v2" ]; then echo "This is correct"; exit 0; else exit 1; fi

0 comments on commit ee37808

Please sign in to comment.