【发布时间】:2021-06-10 02:04:16
【问题描述】:
要在 GitHub Actions 中运行 pytest,我必须为 Python 运行环境传递一些 secrets。
例如,
- name: Test env vars for python
run: python -c 'import os;print(os.environ)'
env:
TEST_ENV: 'hello world'
TEST_SECRET: ${{ secrets.MY_TOKEN }}
但是,输出如下,
environ({
'TEST_ENV': 'hello world',
'TEST_SECRET':'',
...})
由于GitHub's redaction,它似乎无法正常工作。
根据@raspiduino 的回答,我对导入环境变量的两个选项进行了更多探索。
name: python
on: push
jobs:
test_env:
runs-on: ubuntu-latest
steps:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Test env vars for python
run: python -c 'import os;print(os.environ)'
env:
ENV_SECRET: ${{ secrets.ENV_SECRET }}
REPO_SECRET: ${{ secrets.REPO_SECRET }}
- name: Test inline env vars for python
run: ENV_SECRET=${{ secrets.ENV_SECRET }} REPO_SECRET=${{ secrets.REPO_SECRET }} python -c 'import os;print(os.environ)'
基本上,两个步骤都在相同的输出中。 REPO_SECRET 可以传递,但 ENV_SECRET 不能传递。
【问题讨论】:
-
秘密被编辑从输出,这意味着你的程序可以正确地看到它。如果您绝对需要输出它们,请尝试以某种方式(如 base64)编码以绕过 GitHub 编辑。
-
@iBug 刚刚附上了输出。它们不同于环境和存储库机密。
-
好的,我想我知道发生了什么。暂时请坚持存储库秘密并远离“环境秘密”。 GitHub 的“环境”不像操作系统。
-
嗨@northtree,如果你想看看的话,我在这里做了一些非常相似的事情:)github.com/GuillaumeFalourd/ritchie-formulas-scheduler-demo/…
标签: python environment-variables github-actions github-secret