Skip to content

Substitute build args from config into parsed Dockerfile before processing deps#828

Merged
nkubala merged 4 commits intoGoogleContainerTools:masterfrom
nkubala:build_args
Jul 18, 2018
Merged

Substitute build args from config into parsed Dockerfile before processing deps#828
nkubala merged 4 commits intoGoogleContainerTools:masterfrom
nkubala:build_args

Conversation

@nkubala
Copy link
Copy Markdown
Contributor

@nkubala nkubala commented Jul 18, 2018

This PR passes the buildArgs from Docker artifacts in skaffold.yaml to the Docker dependency parser, which substitutes the args into the parsed Dockerfile AST before processing any other commands. This allows args to be substituted correctly when provided as a build arg and implicitly referenced in a Dockerfile, e.g.

skaffold.yaml

build:
  artifacts:
  - imageName: foo/bar
    workspace: .
    docker:
      buildArgs:
        FOO: "bar"

Dockerfile

...
ARG FOO
COPY $FOO.txt /tmp/bar.txt
...

will now copy bar.txt to /tmp/bar.txt.

Fixes #777

Copy link
Copy Markdown
Contributor

@dgageot dgageot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a few nits

func runDeps(out io.Writer, filename, dockerfile, context string) error {
config, err := cmdutil.ParseConfig(filename)
if err != nil {
return err
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: wrap the error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

func runContext(out io.Writer, filename, context string) error {
config, err := cmdutil.ParseConfig(filename)
if err != nil {
return err
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: wrap the error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


func getBuildArgsForDockerfile(config *config.SkaffoldConfig, dockerfile string) map[string]*string {
for _, artifact := range config.Build.Artifacts {
if artifact.DockerArtifact != nil && artifact.DockerArtifact.DockerfilePath == dockerfile {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we compare normalized paths?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I also realized this was using the top level skaffold.yaml even if the user provided a relative path to the dockerfile which was breaking some scenarios, so I added logic to infer the path to the skaffold.yaml if it's not provided. PTAL

var RetrieveImage = retrieveImage

func readDockerfile(workspace, absDockerfilePath string) ([]string, error) {
func readDockerfile(buildArgs map[string]*string, workspace, absDockerfilePath string) ([]string, error) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: would have added those arguments last

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair, done

@bobcatfish
Copy link
Copy Markdown
Contributor

Hey @nkubala what do you think about adding a new test case or expanding the existing unit tests to cover this case that was failing? (or maybe it's already covered and i'm missing it?)

@nkubala
Copy link
Copy Markdown
Contributor Author

nkubala commented Jul 18, 2018

@bobcatfish added a test

@nkubala nkubala merged commit 6a97251 into GoogleContainerTools:master Jul 18, 2018
@nkubala nkubala deleted the build_args branch July 18, 2018 20:51
@joseaio
Copy link
Copy Markdown

joseaio commented Jan 8, 2020

Any news on this feature?
Any workaround?
example: use ARG as value for ENV variable > use $ENV var

ARG MY_ARG=default_value
ENV MY_ENV=$MY_ARG
ADD ${MYENV}.txt /path/to/file.txt

@dgageot
Copy link
Copy Markdown
Contributor

dgageot commented Jan 8, 2020

@joseaio I believe this was fixed in #3439

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dockerfile: COPY/ADD with build arg variables are not expanded

4 participants