DEV Community

Maxime Guilbert
Maxime Guilbert

Posted on • Updated on

Gitlab-CI - How to share environment variables to the after_script?

In Gitlab-CI, you can use after_script to do some stuff after a job, what ever appends during the job.

Issue

But if you tried to share environment variables from your before_script or script part (note : I talk about environment variables which are declared with export in one of this section), you won't be able to access to them.

What cause the issue?

For the after_script execution, Gitlab will create a new shell instancce to execute it.

So all the temporary elements which are not shared accross multiple shell instances won't be accessible in after_script.

Resolution

So to resolve it, you can just print the value of the desired environment variable in a file, then read this file in the after_script section.

job:
  before_script:
    - export TEST="test"
    - echo $TEST > test
  after_script:
    - export TEST = $(cat test)
    - echo $TEST
Enter fullscreen mode Exit fullscreen mode

Links


I hope it will help you! 🍺


You want to support me?

Buy Me A Coffee

Latest comments (0)