skip to Main Content

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?!

2

Answers


  1. Chosen as BEST ANSWER

    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 excude 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.


  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search