dnephin / dobi

A build automation tool for Docker applications
https://dnephin.github.io/dobi/
Apache License 2.0
309 stars 36 forks source link

How can I make env variables available to the `image` task? #144

Closed Fuco1 closed 6 years ago

Fuco1 commented 6 years ago

I want to tag the image based on some env variable, but there is no way to specify those for the image task. Also the build args often come from environment, for example

docker build --build-arg=NPM_TOKEN

will take the value from the environment. I don't see a way to do this currently.

I'm trying to model this docker build line as a task

docker build --build-arg=NPM_TOKEN -t "$IMAGE_DEPS_DEV" -f Dockerfile.deps.dev .
Enteee commented 6 years ago

The image task does suppor a tags parameter which do support configuration variables. Using configuration variables you can access envrionment.

I configure image resources like this:

image=image/A:
  tags: [
    "{env.TAG}"
   ]
   args:
     NPM_TOKEN: "{env.NPM_TOKEN}"
   ...

does this help?

dnephin commented 6 years ago

@Enteee example is exactly right for handling build args, and setting the image tag.

@Fuco1 the image name is separate from the "resource name". You can use environment variable in the image name as well, but you always refer to the image resource using the resource name. Example:

image=TheResourceName:
  image: "{env.SOME_VAR}"

job=something:
  use: TheResourceName

Tasks are also run using the resource name, so it would be dobi TheResourceName to build the image.

So in your case the config might look something like this:

image=deps-dev:
  image: "{env.IMAGE_DEPS_DEV}"
  args: ["NPM_TOKEN={env.NPM_TOKEN"]

However if IMAGE_DEPS_DEV contains both the name and the tag you will need to split that up into two environment variables, so you can set image name and tags separately.

Fuco1 commented 6 years ago

My issue is more about how the environment is created then. How do I put the variables in the env so that I can refer to them in the image task? Because they don't live there from the beginning or universally, they are computed on the fly. So I need a way to pass environment from one job to another or for one job to set environment for another.

Which is exactly what can be done in a job task but not in image task.

dnephin commented 6 years ago

There are a few ways to load values. If the values are static, or come from a key=value file you can use an env resource: http://dnephin.github.io/dobi/config.html#env

If the value is computed from some script, there is the option discussed in #142, and the example being added in #146.

It's true that you can't set an env var from an image task, but you can add a job task that uses the image, and capture the environment variable that way.