Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Self hosted environment variables not available to Github actions

When running Github actions on a self hosted runner machine, how do I access existing custom environment variables that have been set on the machine, in my Github action .yaml script?

I have set those variables and restarted the runner virtual machine several times, but they are not accessible using the $VAR syntax in my script.

like image 952
prmph Avatar asked Dec 08 '25 09:12

prmph


2 Answers

Inside the application directory of the runner, there is a .env file, where you can put all variables for jobs running on this runner instance.

For example

LANG=en_US.UTF-8
TEST_VAR=Test!

Every time .env changes, restart the runner (assuming running as service)

sudo ./svc.sh stop
sudo ./svc.sh start

Test by printing the variable

enter image description here

like image 145
Ran QUAN Avatar answered Dec 10 '25 10:12

Ran QUAN


If you want to set a variable only for one run, you can add an export command when you configure the self-hosted runner on the Github repository, before running the ./run.sh command:

Example (linux) with a TEST variable:

# Create the runner and start the configuration experience
$ ./config.sh --url https://github.com/owner/repo --token ABCDEFG123456
# Add new variable
$ export TEST="MY_VALUE"
# Last step, run it!
$ ./run.sh

That way, you will be able to access the variable by using $TEST, and it will also appear when running env:

  job:
    runs-on: self-hosted
    steps:
      - run: env
      - run: echo $VAR

enter image description here


If you want to set a variable permanently, you can add a file to the etc/profile.d/<filename>.sh, as suggested by @frennky above, but you will also have to update the shell for it be aware of the new env variables, each time, before running the ./run.sh command:

Example (linux) with a HTTP_PROXY variable:

# Create the runner and start the configuration experience
$ ./config.sh --url https://github.com/owner/repo --token ABCDEFG123456
# Create new profile http_proxy.sh file
$ sudo touch /etc/profile.d/http_proxy.sh
# Update the http_proxy.sh file
$ sudo vi /etc/profile.d/http_proxy.sh
# Add manually new line in the http_proxy.sh file
$ export HTTP_PROXY=http://my.proxy:8080
# Save the changes (:wq)
# Update the shell
$ bash
# Last step, run it!
$ ./run.sh

That way, you will also be able to access the variable by using $HTTP_PROXY, and it will also appear when running env, the same way as above.

  job:
    runs-on: self-hosted
    steps:
      - run: env
      - run: echo $HTTP_PROXY
      - run: |
          cd $HOME
          pwd
          cd ../..
          cat etc/profile.d/http_proxy.sh

The etc/profile.d/<filename>.sh will persist, but remember that you will have to update the shell each time you want to start the runner, before executing ./run.sh command. At least that is how it worked with the EC2 instance I used for this test.

enter image description here

Reference

like image 21
GuiFalourd Avatar answered Dec 10 '25 09:12

GuiFalourd