DEV Community

Maxime Guilbert
Maxime Guilbert

Posted 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.


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.


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.

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


I hope it will help you! 🍺

Top comments (0)

Why You Need to Study Javascript Fundamentals

The harsh reality for JS Developers: If you don't study the fundamentals, you'll be just another β€œCoder”. Top learnings on how to get to the mid/senior level faster as a JavaScript developer by Dragos Nedelcu.