0

We were able to do this on gitlabci but not after we moved to github actions. We want certain jobs like *building docker images, deploying helm chart etc.,) to be skipped if a variable DEPLOY=false is set for the repository. We are using common reusable workflows across many repositories.

jobs:
  dockerbuildpush:
    runs-on: [self-hosted, Ubuntu-22.04]
    if: env.BUILD != false

I have tried ${{ }} syntax for the same but it is not considered as a valid syntax. It seems I can do the same for actions but not for jobs or workflow. Workflow contains many jobs, jobs contain many actions. I can do env.WHATEVER in if: for actions but not for jobs or workflows?!

Gajendra D Ambi
  • 3,832
  • 26
  • 30
  • 1
    Relevant: https://stackoverflow.com/questions/75844035/github-action-how-to-reference-env-variable-in-the-job-and-step-if-condition – Azeem Apr 07 '23 at 15:21
  • Have you tried to verify the content of the variable? I think that should be a string, so quoting your conditional should work, like `if: ${{ env. BUILD != 'false' }}` – javierlga Apr 07 '23 at 17:12
  • @javierlga I have, but no go. – Gajendra D Ambi Apr 07 '23 at 20:38

2 Answers2

0

You can use If condition checks both at jobs as well as steps level.

sample.yml

name: Sample

on:
  push:
 
env:
    PUSH: true
  
jobs:
  dockerbuildpush:
    if: ${{ vars.DEPLOY != 'false' }}
    uses: owner/repo/.github/workflows/reusable-workflow.yml@main
    with:
        key1: value1
        key2: value2
        push: ${{ env.push }}
    secrets: inherit

reusable-workflow.yml

name: Sample

on:
    workflow_call:
        inputs:
            push:
                required: true
                type: boolean
            key1:
                required: true
                type: string
            key2:
                required: false
                type: string
                default: ''
        secrets:
            ABC: 
                required: true
            PQR:
                required: true

jobs:
    buildpush:
        runs-on: [self-hosted, Ubuntu-22.04]
        steps:
            - name: Build
              run: ..
            - if: ${{ inputs.build != 'false' }}
              name: Push
              run: ..

In sample.yml, dockerbuildpush job will only run if DEPLOY variable is true and in reusable-workflow.yml step push will only run if input push is true

Haridarshan
  • 1,898
  • 1
  • 23
  • 38
0

The only thing consistently worked for us across other such situations was to just add the repository name to the if condition in the re-usable workflows.

if: github.repository != 'my-org/my-repository'

If tomorrow we need to exclude more repos from such re usable workflows then we do

if: github.repository != 'my-org/my-repository' || 'my-org/my-repository1'

If the number of repos to include such re usable workflows is more than the number of repos to exclude then we can always use the == condition instead of != condition. This also transfers the burden of modification to devops and not devs/contributors/maintainers of the repo which is the way it should be. If in future when one decides to migrate from on prem to off prem github or from one github server to another, then most configuration of workflow is in the yaml's and not as vars in the repo.

Gajendra D Ambi
  • 3,832
  • 26
  • 30