GitHub Actions 中的存储库数值

huangapple go评论67阅读模式
英文:

Repository values in Github Actions

问题

I was trying to switch CircleCI to Github Actions for one of my private repository.

But I had an issue with accessing repository values in my python code. When I set "secret" via GitHub pages it works perfectly, but "variables" are not working for me at all. I expected that it was a stupid issue, but I am unable to find it out anywhere.

Let's have a simple run-tests.yml:

  name: Run tests
  on: push

  jobs:
    execute_tests:
        name: Install requirements and execute tests
        runs-on: ubuntu-latest

        steps:
            - name: ...
              run: ...
            - name: Execute tests
              env:
                DJANGO_SETTINGS_MODULE: sms.settings
                SMS_PASSWORD: ${{secrets.SMS_PASSWORD}}
                SMS_USERNAME: ${{env.SMS_USERNAME}}
              run:
                pytest -vs

When I list os.environ the SMS_USERNAME is defined but it has an empty string inside. When I change env.* to secrets.* (and replace env variable with secret) it works as expected. Any idea what I'm doing wrong?

I was copying the names of the variable to prevent typo issues. There are no special characters inside SMS_USERNAME (just [a-z]+)

英文:

I was trying to switch CircleCI to Github Actions for one of my private repository.

But I had an issue with accessing repository values in my python code. When I set "secret" via GitHub pages it works perfectly, but "variables" are not working for me at all. I expected that it was a stupid issue, but I am unable to find it out anywhere.

Let's have a simple run-tests.yml:

  name: Run tests
  on: push

  jobs:
    execute_tests:
        name: Install requirements and execute tests
        runs-on: ubuntu-latest

        steps:
            - name: ...
              run: ...
            - name: Execute tests
              env:
                DJANGO_SETTINGS_MODULE: sms.settings
                SMS_PASSWORD: ${{secrets.SMS_PASSWORD}}
                SMS_USERNAME: ${{env.SMS_USERNAME}}
              run:
                pytest -vs

When I list os.environ the SMS_USERNAME is defined but it has empty string inside. When I change env.* to secrets.* (and replace env variable with secret) it works as expected. Any idea what I'm doing wrong?

I was copying the names of the variable to prevent typo issues. There are no special characters inside SMS_USERNAME (just [a-z]+)

答案1

得分: 1

使用新的 vars.VARIABLE_NAME 上下文来引用存储库设置中定义的变量

            - name: 执行测试
              env:
                DJANGO_SETTINGS_MODULE: sms.settings
                SMS_PASSWORD: ${{secrets.SMS_PASSWORD}}
                SMS_USERNAME: ${{vars.SMS_USERNAME}}
              run:
                pytest -vs

为了明确起见,env 变量适用于所有通过 env: 块在工作流、作业或步骤级别设置的环境变量,以及使用特殊工作流命令设置的环境变量

英文:

Use the new vars.VARIABLE_NAME context to reference variables defined in the repository settings.

            - name: Execute tests
              env:
                DJANGO_SETTINGS_MODULE: sms.settings
                SMS_PASSWORD: ${{secrets.SMS_PASSWORD}}
                SMS_USERNAME: ${{vars.SMS_USERNAME}}
              run:
                pytest -vs

For clarity, the env variables work for all environment variables that are set either at the workflow, job or step level using the env: block. And the environment variables that were set using the special workflow command.

huangapple
  • 本文由 发表于 2023年3月12日 16:59:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定