14

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 
2
  • what do you want to do with the environment variable, use it in the same job, or pass it on to another job? Commented Oct 16, 2021 at 13:13
  • I want to use it in the same job. I have found that we can set variable in variables section. But how to set them within script section ? Commented Oct 18, 2021 at 18:10

1 Answer 1

19

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Just a footnote: variables created using a "dotenv" artifact can be used within another job's script. The values won't be available for use in other places like variables and rules sections.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.