我有一个 GitHub Actions 工作流程,它将一些东西部署到环境中。工作流程需要登录才能访问环境,因此我们使用 GitHub 环境机密来存储凭证。
我的工作流程包含以下内容:
on:
workflow_dispatch:
inputs:
deployment_target:
type: choice
description: Choose an environment to deploy to
options:
- dev
- staging
- production
- benchmarks
...
jobs:
deploy-to-persistent-environment:
environment: ${{ github.event.inputs.deployment_target}}
steps:
- name: Login # Pseudocode
run: login -password "${{ secrets.PASSWORD }}"
- name: Deploy # Pseudocode
run: DeploymentTool.exe -environment "${{ github.event.inputs.deployment_target}}"
“基准”环境使用与“开发”环境相同的凭据。因此,我想重复使用相同的 GitHub 环境机密,而不是在 GitHub 中拥有两组重复的环境机密。
即,我想做这样的伪代码:
jobs:
deploy-to-persistent-environment: # Pseudocode
environment: ${{ if github.event.inputs.deployment_target == 'benchmarks' then 'development' else github.event.inputs.deployment_target }}
steps:
- name: Login
run: login -password "${{ secrets.PASSWORD }}"
- name: Deploy
run: DeploymentTool.exe -environment "${{ github.event.inputs.deployment_target}}"
有什么方法可以通过 GitHub Actions 实现这一点吗?
有一种方法可以直接在表达式中执行此操作,根据 Github 社区上的参考文献,使用语法
${{ x && 'ifTrue' || 'ifFalse' }}
在这种情况下:
x
为 True,则将使用 first
参数。x
为 False,则将使用 second
参数。在我们的例子中,它看起来像这样:
environment: ${{ github.event.inputs.deployment_target == 'benchmarks' && 'development' || github.event.inputs.deployment_target }}
地点:
如果
github.event.inputs.deployment_target == 'benchmarks'
为True,development
将用作环境。
如果
github.event.inputs.deployment_target == 'benchmarks'
为False,github.event.inputs.deployment_target
将用作环境。
更新伪代码:
jobs:
deploy-to-persistent-environment: # Pseudocode
environment: ${{ github.event.inputs.deployment_target == 'benchmarks' && 'development' || github.event.inputs.deployment_target }}
steps:
- name: Login
run: login -password "${{ secrets.PASSWORD }}"
- name: Deploy
run: DeploymentTool.exe -environment "${{ github.event.inputs.deployment_target}}"