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

Allow passing image tag to docker build in buildArgs #3229

Closed
Awem opened this issue Nov 12, 2019 · 6 comments
Closed

Allow passing image tag to docker build in buildArgs #3229

Awem opened this issue Nov 12, 2019 · 6 comments
Labels
area/build area/templating build/docker kind/bug Something isn't working priority/p2 May take a couple of releases

Comments

@Awem
Copy link

Awem commented Nov 12, 2019

Expected behavior

On skaffold build the tag of the docker image is immediately printed after Generating tags... and should therefore be available in the buildArgs template of the docker build configuration.

Actual behavior

It is not available.

Information

  • Skaffold version: 1.0.0
  • Operating system: Linux
  • Contents of skaffold.yaml:
apiVersion: skaffold/v1
kind: Config
build:
  artifacts:
  - image: client
    docker:
      buildArgs:
        IMAGE_NAME: '{{.IMAGE_NAME}}'
  local:
    push: true
    useDockerCLI: true

Steps to reproduce the behavior

  1. have a Dockerfile with
ARG IMAGE_NAME
ENV IMAGE_NAME ${IMAGE_NAME}
RUN echo "Building image $IMAGE_NAME
  1. skaffold build
  2. $IMAGE_NAME won't have a value unless you provide some like IMAGE_NAME=name skaffold build
@tejal29 tejal29 added the kind/question User question label Nov 12, 2019
@tejal29
Copy link
Contributor

tejal29 commented Nov 12, 2019

@Awem, skaffold runs locally and uses the local environment for substituting the variables.
Skaffold docker builder, does not parse the dockerfile to get to variables set in the dockerfile.

Does that make sense?

Please re-open this if you need more information.

@tejal29 tejal29 closed this as completed Nov 12, 2019
@tejal29 tejal29 reopened this Nov 12, 2019
@Awem
Copy link
Author

Awem commented Nov 13, 2019

Not sure whether we mean the same thing here. The Dockerfile is just for demonstration of a possible build. I do not want Skaffold to parse the Dockerfile. I am only looking for a possibility to use the name of the image (printed by Skaffold after Generating tags... on skaffold build ) in the buildArgs key of my skaffold.yaml.
Since the templating feature is supported for buildArgs this should be easy. The docs say Skaffold allows for certain fields in the config to be templated with values either from environment variables, or certain special values computed by Skaffold. The image name would be such a computed value which is already available at the time the buildArgs are passed.

@balopat balopat added kind/feature-request priority/p3 agreed that this would be good to have, but no one is available at the moment. area/build area/templating build/docker kind/bug Something isn't working priority/p2 May take a couple of releases and removed kind/question User question kind/feature-request priority/p3 agreed that this would be good to have, but no one is available at the moment. labels Nov 14, 2019
@balopat
Copy link
Contributor

balopat commented Nov 14, 2019

Thank you for opening - this is a bug, the docs are confusing and the code is terribly inconsistent.

It is supposed to work, as we do evaluate env vars there. However, our calculated values are handled inconsistently across the code base:

ExecuteEnvTemplate takes an extra map where we should pass down the calculated fields.
https://github.com/GoogleContainerTools/skaffold/blob/master/pkg/skaffold/util/env_template.go#L40

As I'm looking at references to this method - a bunch of places (including the docker builder) it is called with nil... this is inconsistent even within helm fields themselves.

The solution will be to have one way of evaluating templates in the code.

@javier-b-perez
Copy link

hi,
I have a similar use case, but I only need to pass the tag generated by skaffold.
I'm using SKAFFOLD_DEFAULT_REPO + SKAFFOLD_TAG as my workaround, but this prevent me from using tag generated by skaffold (I need to generate the tag myself, before calling skaffold).

apiVersion: skaffold/v2beta1
kind: Config
build:
  local:
    push: false
  artifacts:
    # Builder images are used to build the other containers.
    # You need to specify the registry you want to use:
    # export SKAFFOLD_DEFAULT_REPO=gcr.io/my-gcp-project
    # export SKAFFOLD_TAG=latest
  - image: gobuilder
    context: build/gobuilder
    docker:
      dockerfile: Dockerfile
  # These containers are using builders + src.
  - image: my-app
    docker:
      dockerfile: my-app/Dockerfile
      buildArgs:
        GOBUILDER: "{{.SKAFFOLD_DEFAULT_REPO}}/gobuilder:{{.SKAFFOLD_TAG}}"

The my-app Dockerfile looks like

ARG GOBUILDER=gcr.io/my-gcp-project/gobuilder:latest
FROM ${GOBUILDER} as builder
...
RUN go build ...

is there a way we can make some skaffold variables available for buildArgs?

@nkubala
Copy link
Contributor

nkubala commented Apr 24, 2020

going to close this as a dupe of #3343 since I think it will be addressed if/when we fix that one. trying to keep all the conversations in one place :)

@nkubala nkubala closed this as completed Apr 24, 2020
DanielSel added a commit to DanielSel/skaffold that referenced this issue Apr 27, 2020
DanielSel added a commit to DanielSel/skaffold that referenced this issue Apr 27, 2020
@javier-b-perez
Copy link

I don't think that #3343 fixed the issue.
I created a new one #4295 with a repo to reproduce and documented workaround.

DanielSel added a commit to DanielSel/skaffold that referenced this issue Jul 24, 2020
DanielSel added a commit to DanielSel/skaffold that referenced this issue Jul 28, 2020
DanielSel added a commit to DanielSel/skaffold that referenced this issue Jul 29, 2020
DanielSel added a commit to DanielSel/skaffold that referenced this issue Sep 11, 2020
DanielSel added a commit to DanielSel/skaffold that referenced this issue Sep 15, 2020
DanielSel added a commit to DanielSel/skaffold that referenced this issue Oct 22, 2020
nkubala pushed a commit that referenced this issue Oct 23, 2020
* implement env variable expansion for kaniko builds

* add IMAGE_REPO, IMAGE_NAME, IMAGE_TAG to generated env variables (if not set explicitly). resolves #3229

* use docker.ParseReference for kaniko env templating

* add Repo and Name attributes to ImageReference

* Update pkg/skaffold/build/cluster/kaniko_test.go

Co-authored-by: Tejal Desai <tejal29@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/build area/templating build/docker kind/bug Something isn't working priority/p2 May take a couple of releases
Projects
None yet
Development

No branches or pull requests

5 participants