我想为多个环境(例如开发、质量保证和生产)创建一个手动触发管道。在每个环境的管道中,我有 3-4 个变量,这些变量取决于环境选择。我的想法是在管道的单独步骤中将这些变量设置为环境变量,以便根据初始输入正确设置它们。但是我不确定这有多优化,我想问是否有不同的解决方案来解决我的问题。在我的问题下,我提供了我的初步想法:
on:
workflow_dispatch:
inputs:
environment:
description: 'Environment to run tests against'
required: true
default: 'stage'
type: choice
options:
- dev
- stage
- prod
permissions:
contents: write
pull-requests: write
security-events: write
jobs:
test_auth_fn:
runs-on: ...
permissions:
contents: read
id-token: write
steps:
- name: Set parameters based on input
run: |
if [ "${{ inputs.environment }}" == "dev" ]; then
echo "Setting parameters for DEV"
export PARAM_1=dev_value
export PARAM_2=dev_specific_value
elif [ "${{ inputs.environment }}" == "stage" ]; then
echo "Setting parameters for PROD"
export PARAM_1=prod_value
export PARAM_2=prod_specific_value
else
echo "Setting parameters for STAGING"
export PARAM_1=staging_value
export PARAM_2=staging_specific_value
fi
- name: Use parameters
run: |
echo "PARAM_1 is ${{ env.PARAM_1 }}"
echo "PARAM_2 is ${{ env.PARAM_2 }}"
您无需为此创建专用步骤,GitHub Actions 在
env
部分中原生支持它,如下所示:
env:
PARAM_1: >-
${{ inputs.environment == 'dev' && 'dev_value'
|| inputs.environment == 'stage' && 'staging_value'
|| inputs.environment == 'prod' && 'prod_value'
}}
PARAM_2: >-
${{ inputs.environment == 'dev' && 'dev_specific_value'
|| inputs.environment == 'stage' && 'staging_specific_value'
|| inputs.environment == 'prod' && 'prod_specific_value'
}}