自行托管的环境变量在 Github Actions 中不可用。

12
在自托管的运行器机器上运行Github actions时,如何在我的Github action .yaml脚本中访问已设置在该机器上的现有自定义环境变量?我已经设置了这些变量并重新启动了运行器虚拟机多次,但在我的脚本中使用$VAR语法无法访问它们。

你把那些变量设置在哪里了?设置完变量后,你有重新启动运行器吗? - jessehouwing
当您在自托管的运行程序内执行run: env命令时,是否会在控制台上打印$VAR?(请参见Github Runner上的Workflow examplerun)。如果是,请尝试使用${{ env.VAR }} - GuiFalourd
1
@GuiFalourd,“run:env”并未显示所有的环境变量。“${{ env.VAR }}”也无法访问它。 - prmph
你是如何设置环境变量的? - jessehouwing
1
你尝试过在/etc/profile.d/中使用脚本来设置它们吗? - frennky
显示剩余2条评论
3个回答

8
在运行程序的应用目录中,有一个名为.env的文件,您可以在其中放置所有作业在此运行实例上运行所需的变量。
例如:
LANG=en_US.UTF-8
TEST_VAR=Test!

每次更改 .env 文件时,重新启动运行程序(假设作为服务运行)。
sudo ./svc.sh stop
sudo ./svc.sh start

通过打印变量进行测试

enter image description here


这也是一个很有用的解决方案。我在https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/using-a-proxy-server-with-self-hosted-runners#using-a-env-file-to-set-the-proxy-configuration的文档中遇到了.env文件,但不知道它还可以用于除代理之外的变量。 - gsf

2
如果您想为仅一次运行设置变量,则可以在在Github存储库上配置自托管运行程序时添加导出命令,然后再运行./run.sh命令:
例如(Linux)使用“TEST”变量:
# 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

enter image description here


如果您想永久设置一个变量,可以像@frennky建议的那样将文件添加到etc/profile.d/<filename>.sh中,但是在运行./run.sh命令之前,每次都必须更新shell以使其了解新的环境变量:例如(linux)使用HTTP_PROXY变量:
# 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"将保留,但请记住,每次想要启动runner之前,您都必须更新shell,然后执行./run.sh命令。至少这是我用于此测试的EC2实例的工作方式。"

enter image description here

这句话的意思是“

参考资料

”。

0
你可以在runsvc.sh中更改shebang(顶部行)为#!/bin/bash --login,以确保shell获取你在.profile中配置的任何环境变量。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接