Setting Environment Variable in gitlab-ci.yml in script

Viewed 8060

Please help me in converting below written GitHub Action to Gitlab CI script. I am new to scripting in Gitlab.

From Github documentation I could understand that the below written line is for setting the value of environment variable. But I couldn't find any resource for setting environment variable 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 
1 Answers

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.

Related