buildkite-plugins / docker-compose-buildkite-plugin

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

Use of `env` in step is ignored by docker-compose plugin environment interpolation in commands, resorting to global `env` #385

Closed mattmm1 closed 1 year ago

mattmm1 commented 1 year ago

Hi I expect that in-step use of env variable will be interpreted in docker-compose keys rather than only intrepreting global env variables.

I can't tell if this is due to something in buildkite or docker-compose but I have a setup similar to the following:

env:
  ENVIRONMENT: dev
  IMAGE_REPO_URL: globalrepo
  BUILDKIT_INLINE_CACHE: 1
    - label: "Build and push containers: Build and push to DEV"
      plugins:
      - ecr#v2.5.0:
          login: true
      - docker-compose#v4.9.0:
          build: myproject-${ENVIRONMENT}
          image-repository: ${IMAGE_REPO_URL}/myproject-${ENVIRONMENT}
          image-name: latest
    - label: "Build and push containers: Build and push to UAT"
      env:
        ENVIRONMENT: uat
        IMAGE_REPO_URL: anotherrepo
      plugins:
      - ecr#v2.5.0:
          login: true
      - docker-compose#v4.9.0:
          build: myproject-${ENVIRONMENT}
          image-repository: ${IMAGE_REPO_URL}/myproject-${ENVIRONMENT}
          image-name: latest

I notice that the in step use of env is ignored. Both steps use the global env settings. Have I used this instep env incorrectly or no buildkite plugin supports the use of instep env in this way? Or is it an enhancement that could be added to docker-compose plugin?

I'm using docker-compose#v4.9.0 as in the above example.

Thanks - (love the plugin - constantly using it).

toote commented 1 year ago

@mattmm1 the environment variables you are talking about are not being shared through the plugin so there is nothing in the step itself that can use the variables values you are setting up at the step level.

As far as I understand, your issue comes from a misunderstanding of when variables are interpolated. As the variables are used in the pipeline definition itself and are not escaped, they are interpolated at the time the pipeline is uploaded with the values they have at that time: the global one. Not sure if you can actually escape them there to have them interpolated when the step runs, but you can try :)

mattmm1 commented 1 year ago

Hey @toote yes ok. I can see the docs mention runtime variables need to be escaped so I'll try that and yep realise that's not a docker-compose plugin issue - thanks for your help here :) . I think the buildkite docs could be clearer about this though as I didn't have to escape the global variables for them to be used: https://buildkite.com/docs/pipelines/environment-variables#environment-variable-precedence-job-environment (or perhaps I just haven't seen it there). I might see if the forum is right place to raise this.