Search code examples
bashgithubdevopsgithub-actionscicd

How to run a github workflow job conditionally and pass env vars between jobs


I have this in my github workflow .yml file

name: Release workflow (pipeline)

# Enable Buildkit and let compose use it to speed up image building
env:
  DOCKER_BUILDKIT: 1
  COMPOSE_DOCKER_CLI_BUILD: 1
  RELEASE_ID: ''  # this env var will be modified in Create release job > Set release number step, so to be used in the deploy job

on:
  workflow_dispatch:
    inputs:
      environment:
        description: Environment to create the release for
        required: true
        type: choice
        options:
          # - dev
          - prod
        default: prod

      should_auto_deploy:
        description: Auto-deploy the release
        required: false
        type: boolean
        default: 'false'

# some other lines

jobs:
  create_release:
    name: Create release
    needs: run_tests
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      # i have this line in the bash script for this step, to modify the global env var above
      # echo "RELEASE_ID=$releaseId" >> $GITHUB_ENV
      - name: Set release number
        run: |
          chmod +x ./scripts/github-workflow/set-release-number.sh
          ./scripts/github-workflow/set-release-number.sh \
            -e ${{ github.event.inputs.environment }} \
            -n $(date +%Y-%m-%d)-${{ github.ref_name }}-${{ github.run_id }}

# some other lines

  deploy:
    name: Deploy release
    needs: create_release
    # this is the problematic condition.
    # this job ALWAYS runs even when i dont tick the checkbox to set `github.event.inputs.should_auto_deploy` to true
    if: ${{ github.event.inputs.should_auto_deploy && needs.create_release.result == 'success' }}
    uses: Aplanke/ApiBackend/.github/workflows/deployment.yml@main
    with:
      environment: ${{ github.event.inputs.environment }}
      # the updated env var should be passed to this job
      release_number: ${{ github.env.RELEASE_ID }}

What I expect:

  1. Asked in How to pass data between github workflow jobs
  2. The deploy job should only run when github.event.inputs.should_auto_deploy is true and the create_release job was successful

What happens: The deploy jub always runs, even when github.event.inputs.should_auto_deploy is false (not selected in UI)

How do I fix these?


Solution

  • Booleans in GitHub actions are not real booleans, you can read about the problem here.

    You have to use: {{ github.event.inputs.should_auto_deploy == 'true' }}

    And for ENV variable - it's really bad approach to use ENV variables like that - there is explicit way of exchanging data between jobs described in here

    At the end this workflow should be like this:

    jobs:
      create_release:
        name: Create release
        needs: run_tests
        runs-on: ubuntu-latest
        outputs: ${{ steps.set_release_number.release_id }}
        steps:
          - name: Checkout code
            uses: actions/checkout@v2
          - name: Set release number
            id: set_release_number
            run: echo "::set-output name=release_id::RELEASE_ID"
      deploy:
        name: Deploy release
        needs: create_release
        if: ${{ github.event.inputs.should_auto_deploy == 'true' && needs.create_release.result == 'success' }}
        uses: Aplanke/ApiBackend/.github/workflows/deployment.yml@main
        with:
          environment: ${{ github.event.inputs.environment }}
          release_number: ${{ needs.create_release.outputs.release_id }}