Git & GitHub Interview Question #110

What does the if condition do in GitHub Actions?

Advanced GitHub Actions, Repository Internals, Submodules, LFS & Diagnostic Tools Senior Advanced

Quick Interview Answer

The if condition controls whether a job or step should run. It can inspect event data, previous step outcomes, matrix values, inputs, or other supported contexts.

Detailed Explanation

The if condition controls whether a job or step should run. It can inspect event data, previous step outcomes, matrix values, inputs, or other supported contexts.

Conditions help avoid unnecessary work and implement workflow branching. Complex deployment authorization should still rely on repository permissions and environment protection rather than only a YAML condition.

Code Example

steps:
  - name: Deploy
    if: ${{ github.ref == 'refs/heads/main' }}
    run: ./deploy.sh