英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论