Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting environment variable in gitlab-ci.yml script

Tags:

gitlab-ci

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 
like image 384
Manan Bordia Avatar asked Feb 08 '26 04:02

Manan Bordia


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.

like image 59
Simon Schrottner Avatar answered Feb 17 '26 18:02

Simon Schrottner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!