buildkite-plugins / docker-compose-buildkite-plugin

🐳⚡️ Run build scripts, and build + push images, w/ Docker Compose
MIT License
172 stars 140 forks source link

Unable to pass environment variables for interpolation #379

Closed suzaku closed 1 year ago

suzaku commented 1 year ago

I've tried setting env in both step level and plugin level with no luck:

env:
- MY_VAR=${MY_VAR}

I have also tried setting propagate-environment: true for the plugin.

But I get warnings like The MY_VAR variable is not set. Defaulting to a blank string. every time.

The version I use is v4.7.0.

toote commented 1 year ago

hi @suzaku, I'm not sure I understand what the exact issue is. As far as I understand, the two ways you mentioned should work so I'm quite puzzled.

I have tried these two pipelines and they both work as expected:

env:
  MY_VAR: value1
steps:
  - label: test1
    env:
      MY_VAR_2: value2
    command: 'echo $${MY_VAR} and $${MY_VAR_2}'
    plugins:
      - docker-compose#v4.7.0:
          env: [ MY_VAR, MY_VAR_2 ]
          run: tests

As well as propagate environment:

env:
  MY_VAR: value1
steps:
  - label: test1
    env:
      MY_VAR_2: value2
    command: 'echo $${MY_VAR} and $${MY_VAR_2}'
    plugins:
      - docker-compose#v4.7.0:
          propagate-environment: true
          run: tests

The one thing I can say is that your environment definition appears to be wrong (as it is supposed to be a dictionary/map, not a list)

suzaku commented 1 year ago

So it only works for run, right? Is there a env passing mechanism for build?

toote commented 1 year ago

Oh, you are running builds.

Docker has no way to pass environment variables for build steps. You need to use args instead... They are similar, but not exactly the same as they have different semantics (args can be used during builds but not available during runs). To be able to have both you need to do a little trick in you dockerfile

suzaku commented 1 year ago

No, the environment variables are for interpolation in the docker-compose.yml, not for the docker build itself.

suzaku commented 1 year ago

Since there's no way to build and push multiple tags at the same plugin configuration, I'm trying to add multiple services in the docker-compose.yml and specify multiple services in build. To differentiate them, I want to use environment variables and interpolation.

toote commented 1 year ago

Could you share some part of your docker compose file to understand exactly where you are using the environment variables then?

suzaku commented 1 year ago

Never mind. I will just write my own custom script and use environment variables.