如何在GitHub Actions中根据所选环境获取对应URL变量?
如何在GitHub Action中根据选择的环境获取对应环境变量
你当前的配置缺少了在job中关联选中环境的步骤,导致对应环境的URL变量没有被注入到工作流里。要解决这个问题,只需在job级别添加environment字段,关联用户通过workflow_dispatch选择的环境即可。
修正后的完整配置
name: Run Playwright Tests on: workflow_dispatch: inputs: environment: type: environment description: Select the environment jobs: build: runs-on: uhg-runner-m # 关联用户选择的环境,自动加载该环境下的变量 environment: ${{ github.event.inputs.environment }} steps: - name: get-env-var run: echo "Environment URL is: $URL"
说明
- 添加
environment: ${{ github.event.inputs.environment }}后,GitHub会自动将你在对应环境设置中配置的URL变量注入到job的环境变量中。 - 当你选择
development环境时,步骤会输出Environment URL is: my-app-dev.com,完全符合你的预期。 - 你也可以用
${{ env.URL }}的形式明确引用环境变量,效果和直接用$URL一致。
内容的提问来源于stack exchange,提问作者Cap Coder
相关产品推荐
相关产品推荐

