Manan Bordia
Manan Bordia

Reputation: 492

Setting environment variable in gitlab-ci.yml script

I am converting the GitHub Action script below to a GitLab CI script.

From the GitHub documentation, I understand that the line below sets the value of an environment variable - but I couldn't find any resource for setting environment variables in GitLab.

run: >
     DISPLAY=:0 xvfb-run -s '-screen 0 1024x768x24' julia --project=monorepo -e 'using Pkg; Pkg.test("GLMakie", coverage=true)'
     && echo "TESTS_SUCCESSFUL=true" >> $GITHUB_ENV 

Upvotes: 14

Views: 44026

Answers (1)

Simon Schrottner
Simon Schrottner

Reputation: 4764

There are multiple ways to set environment variables, and it depends on what you want to achieve:

  1. use it within the same job
  2. use it in another job

Use it within the same job

In Bash or other Shells you can set an environment variable via export - in your case it would look like:

job:
  script:
    - DISPLAY=:0 xvfb-run -s '-screen 0 1024x768x24' julia --project=monorepo -e 'using Pkg; Pkg.test("GLMakie", coverage=true)' && export TESTS_SUCCESSFUL=true
    - echo $TESTS_SUCCESSFUL #verification that it is set and can be used within the same job

Use it within another job

To handover variables to another job you need to define an artifact:report:dotenv. It is a file which can contain a list of key-value-pairs which will be injected as Environment variable in the follow up jobs.

The structure of the file looks like:

KEY1=VALUE1
KEY2=VALUE2

and the definition in the .gitlab-ci.yml looks like

job:
  # ...
  artifacts:
    reports:
      dotenv: <path to file>

and in your case this would look like


job:
  script:
    - DISPLAY=:0 xvfb-run -s '-screen 0 1024x768x24' julia --project=monorepo -e 'using Pkg; Pkg.test("GLMakie", coverage=true)' && echo "TESTS_SUCCESSFUL=true" >> build.env
  artifacts:
    reports:
      dotenv: build.env

job2:
  needs: ["job"]
  script:
    - echo $TESTS_SUCCESSFUL

see https://docs.gitlab.com/ee/ci/variables/#pass-an-environment-variable-to-another-job for further information.

Upvotes: 19

Related Questions