You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 21:55:06