Passing dotenv variables to downstream pipeline

Hi,

I would like to pass a dotenv variable to a downstream pipeline. The variable is “created” during build, because the version is extracted from a source file.

prepare-build:
   stage: prepare_build
   script:
     - export VERSION=$(cat package.json | jq -r .version)
     - echo "VERSION=$VERSION" >> build.env
   artifacts:
     reports:
       dotenv: build.env

In the next build steps the variable VERSION is available and contains the correct version value. Everything is fine so far.

But in the last step I want to pass this variable to a downstream pipeline:

trigger-deployment:
  stage: trigger_deploy
  variables:
    VERSION: $VERSION
  trigger:
     project: my/project

This doesn’t work. The variable VERSION contains the string $VERSION, not the actual version number as in all build steps before.

Is it not possible to pass dotenv variables to downstream pipelines? If not, is there any other way to do it?

Thanks for your help.

Best regards,
Daniel

Nobody? I can’t be the only one having this “issue” …

1 Like

I have the exactly same problem. I guess it is related to the fact that jobs with the trigger-key are treated as ‘trigger-bridge-jobs’ with some limitation: https://docs.gitlab.com/ee/ci/multi_project_pipelines.html#limitations
Most important one: They are not run on a Gitlab-Runner. My guess is the automatic reading of the dotenv-Artifact is normally handled by the Runner.

So for now you probably have to trigger the downstream pipeline manually: https://docs.gitlab.com/ee/ci/triggers/README.html#when-used-with-multi-project-pipelines
This will allow you to pass variables yourself: https://docs.gitlab.com/ee/ci/triggers/README.html#making-use-of-trigger-variables

This sounds like exactly the problem I’ve encountered too. I created https://gitlab.com/gitlab-org/gitlab/-/issues/223224 for it.

This is the documentation you want to take a closer look at:

So for the specific example shown above this should work:

trigger-deployment:
  stage: trigger_deploy
  needs:
     - prepare-build
  trigger:
     project: my/project

So you don’t redeclare the variables you can just use them as long as your needs or dependency keys are configured.

where did you find “don’t redeclare the variables” in the documentation? For the child pipeline variables should be redefined.

@sena I happened to see that In Pass an environment variable to another job, it says

Inherited variables take precedence over certain types of new variable definitions such as job defined variables

The section below that in the docs gives an example:

build:
  stage: build
  script:
    - echo "BUILD_VARIABLE=value_from_build_job" >> build.env
  artifacts:
    reports:
      dotenv: build.env

deploy:
  stage: deploy
  variables:
    BUILD_VARIABLE: value_from_deploy_job
  script:
    - echo "$BUILD_VARIABLE"  # Output is: 'value_from_build_job' due to precedence
  environment: production

It seems that redeclaring variables would not have an effect, since “inherited” variables take precedence. It’s not exactly clear from this section of the docs why loading dotenv files is termed inheritance, but I imagine the dotenv functionality in gitlab is modeled on passing environment variables to child processes in shell scripts.

1 Like