Search code examples
githubgithub-actionscicd

Skip Github job if repo variable exists


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


Solution

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