自托管环境变量不适用于Github操作 [英] Self hosted environment variables not available to Github actions

本文介绍了自托管环境变量不适用于Github操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在自托管运行器计算机上运行Github操作时,如何访问计算机上已在Github操作.yaml脚本中设置的现有自定义环境变量?

我已多次设置这些变量并重新启动运行器虚拟机,但无法使用脚本中的$VAR语法访问它们。

推荐答案

如果您只想为一次运行设置一个变量,您可以在运行./run.sh命令之前,在Github存储库上配置自托管运行器时添加export命令:

带有TEST变量的示例(Linux):

# 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

这样,您就可以使用$TEST访问该变量,并且在运行env时也会出现该变量:

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


如果要永久设置变量,可以按照上述@frennky的建议将文件添加到etc/profile.d/<filename>.sh,但您还必须为其更新外壳程序在运行./run.sh命令之前,请注意每次环境变量:

带有HTTP_PROXY变量的示例(Linux):

# 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

这样,您还可以使用$HTTP_PROXY访问该变量,并且在运行env时也会显示该变量,与上面的方式相同。

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

etc/profile.d/<filename>.sh将持续存在,但请记住,在执行./run.sh命令之前,您必须在每次要启动运行器时更新外壳。至少在我用于此测试的EC2实例中是这样工作的。

Reference

这篇关于自托管环境变量不适用于Github操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆