Github 操作矩阵包括基于 GitHub 事件有条件的条目

cyb*_*989 5 yaml github-actions

我目前有一个工作流程,可以在合并到 master 时触发部署,并确保开发作业在部署产品之前通过。总共有 4 个环境(2 个 dev 和 2 个 prod),如果可能的话,我希望能够减少代码重复并将作业数量减少到 1 个,因为作业之间唯一改变的是环境变量。

我遇到的问题是似乎没有办法根据 github 事件有条件地包含或排除条目。如果有人提供意见或知道一个好的解决方法,我将非常感激。

文件和内容如下。

应用.yaml:

name: Terraform Apply

on:
  deployment:
  workflow_dispatch:
    inputs:
      environment:
        description: "name of the environment"
        required: true
        default: "dev"

env:
  AWS_ACCESS_KEY_ID: ${{ secrets.KEY_ID_EXAMPLE }}
  AWS_SECRET_KEY: ${{ secrets.KEY_EXAMPLE }}

jobs:
  deploy-dev:
    name: TF Apply - Dev
    runs-on: ubuntu-latest
    if: ${{ github.event.deployment.environment == 'dev' || github.event.deployment.environment == 'all' || github.event.inputs.environment == 'dev' || github.event.inputs.environment == 'all' }}
    env:
      TF_VAR_aws_provider_role_arn: arn:example
      TF_VAR_example_api_key: ${{ secrets.EXAMPLE_API_KEY }}
      TF_VAR_example_app_key: ${{ secrets.EXAMPLE_APP_KEY }}
      TF_WORKSPACE: dev
    steps:
    - name: Run Terraform Apply Composite
      uses: GetTerminus/example/multi-workspace/terraform-apply@1.0
      with:
        ssh_key: ${{ secrets.EXAMPLE_SSH_KEY }}

  deploy-dev-midwest:
    name: TF Apply - Dev Midwest
    runs-on: ubuntu-latest
    if: ${{ github.event.deployment.environment == 'dev-midwest' || github.event.deployment.environment == 'all' || github.event.inputs.environment == 'dev-midwest' || github.event.inputs.environment == 'all' }}
    env:
      TF_VAR_aws_provider_role_arn: arn:example
      TF_VAR_example_api_key: ${{ secrets.DEV_MIDWEST_API_KEY }}
      TF_VAR_example_app_key: ${{ secrets.DEV_MIDWEST_APP_KEY }}
      TF_WORKSPACE: dev-midwest
    steps:
    - name: Run Terraform Apply Composite
      uses: GetTerminus/example/multi-workspace/terraform-apply@1.0
      with:
        ssh_key: ${{ secrets.GH_ACTIONS_SSH_PRIVATE_KEY }}

  deploy-prod-east:
    name: TF Apply - Prod East
    needs: deploy-dev
    runs-on: ubuntu-latest
    if: ${{ github.event.deployment.environment == 'prod-east' || github.event.deployment.environment == 'all' || github.event.inputs.environment == 'prod-east' || github.event.inputs.environment == 'all' }}
    env:
      TF_VAR_aws_provider_role_arn: arn:example
      TF_VAR_example_api_key: ${{ secrets.PROD_EAST_API_KEY }}
      TF_VAR_example_app_key: ${{ secrets.PROD_EAST_APP_KEY }}
      TF_WORKSPACE: prod-east
    steps:
    - name: Run Terraform Apply Composite
      uses: GetTerminus/example/multi-workspace/terraform-apply@1.0
      with:
        ssh_key: ${{ secrets.GH_ACTIONS_SSH_PRIVATE_KEY }}

  deploy-prod-midwest:
    name: TF Apply - Prod Midwest
    needs: deploy-dev-midwest
    runs-on: ubuntu-latest
    if: ${{ github.event.deployment.environment == 'prod-midwest' || github.event.deployment.environment == 'all' || github.event.inputs.environment == 'prod-midwest' || github.event.inputs.environment == 'all' }}
    env:
      TF_VAR_aws_provider_role_arn: arn:example
      TF_VAR_example_api_key: ${{ secrets.PROD_MIDWEST_API_KEY }}
      TF_VAR_example_app_key: ${{ secrets.PROD_MIDWEST_APP_KEY }}
      TF_WORKSPACE: prod-midwest
    steps:
    - name: Run Terraform Apply Composite
      uses: GetTerminus/example/multi-workspace/terraform-apply@1.0
      with:
        ssh_key: ${{ secrets.GH_ACTIONS_SSH_PRIVATE_KEY }}
Run Code Online (Sandbox Code Playgroud)

部署.yaml:

name: Master Deployments

on:
  push:
    branches: [ master ]

jobs:
  deploy-all:
    name: Deploy
    runs-on: ubuntu-latest
    steps:
    - name: Create Sequential Deployments
    uses: chrnorm/deployment-action@releases/v1
    with:
      token: ${{ secrets.GH_DEPLOYMENT_ACCESS_TOKEN_EXAMPLE }}
      environment: all
Run Code Online (Sandbox Code Playgroud)

Grz*_*ski 2

对于您的示例,矩阵解决方案如下所示:

name: Terraform Apply
on:
  push:
    inputs:
      environment:
        description: "name of the environment"
        required: true
        default: "dev"

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        environment: [dev, dev-midwest, prod-east, prod-midwest]
    steps:
      - name: Deploy
        if: ${{ github.event.inputs.environment == 'all' || (matrix.environment == github.event.inputs.environment) }}
        shell: bash
        run: |
          echo "Hello World !!! ${{ matrix.environment }}"
Run Code Online (Sandbox Code Playgroud)