prm*_*mph 13 continuous-integration environment-variables github-actions github-actions-self-hosted-runners
在自托管运行器计算机上运行 Github 操作时,如何在我的 Github 操作 .yaml 脚本中访问已在计算机上设置的现有自定义环境变量?
我已设置这些变量并重新启动了运行程序虚拟机多次,但无法使用脚本中的 $VAR 语法访问它们。
在运行器的应用程序目录中,有一个.env文件,您可以在其中放置在此运行器实例上运行的作业的所有变量。
例如
LANG=en_US.UTF-8
TEST_VAR=Test!
Run Code Online (Sandbox Code Playgroud)
每次.env更改时,重新启动运行器(假设作为服务运行)
sudo ./svc.sh stop
sudo ./svc.sh start
Run Code Online (Sandbox Code Playgroud)
通过打印变量进行测试
如果您只想为一次运行设置变量,则可以export在 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
Run Code Online (Sandbox Code Playgroud)
这样,您将能够通过使用访问该变量$TEST,并且它也会在运行时出现env:
job:
runs-on: self-hosted
steps:
- run: env
- run: echo $VAR
Run Code Online (Sandbox Code Playgroud)
如果您想永久设置一个变量,您可以按照上面@frennky的建议将一个文件添加到,但是您还必须更新 shell,以便每次在运行命令之前etc/profile.d/<filename>.sh了解新的环境变量:./run.sh
带有变量的示例(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
Run Code Online (Sandbox Code Playgroud)
这样,您还可以使用 访问该变量$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
Run Code Online (Sandbox Code Playgroud)
该etc/profile.d/<filename>.sh命令将持续存在,但请记住,每次您想要启动运行器时,在执行命令之前,您都必须更新 shell./run.sh。至少我在本次测试中使用的 EC2 实例是这样工作的。
| 归档时间: |
|
| 查看次数: |
6329 次 |
| 最近记录: |