Github Actions Matrix include entries conditionally based on GitHub Events

Viewed 882

I currently have a workflow that triggers a deployment when merging to master and makes sure the dev job passes before deploying prod. There are a total of 4 environments (2 dev and 2 prod), I want to be able to reduce code duplication and reduce the number of jobs to 1 using github matrix if that's possible because the only thing that changes between jobs is the value of environmental variables.

The problem I'm running into is there doesn't seem to be a way to conditionally include or exclude entries based on a github event. If anybody has input or a knows of a good workaround I'd really appreciate it.

Files and contents are below.

apply.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 }}

deploy.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
1 Answers

The matrix solution looks like this for your example:

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 }}"
Related