DevOps / GitHub Actions Interview Questions
How do you use conditional steps with if: in GitHub Actions?
The if: key on a job or step controls whether it executes. It accepts a GitHub Actions expression that evaluates to true or false. When false, the step is skipped and shown as greyed-out in the run log — the job does not fail.
Common patterns:
steps: # Run only on pushes to main - name: Deploy to production if: github.ref == 'refs/heads/main' && github.event_name == 'push' run: ./deploy.sh # Run only when a previous step failed (for alerting) - name: Notify failure if: failure() run: ./send-alert.sh # Run after always â even if prior steps failed - name: Upload test report if: always() uses: actions/upload-artifact@v4 with: name: test-report path: target/surefire-reports/ # Skip on draft pull requests - name: Run expensive checks if: github.event.pull_request.draft == false run: ./full-test-suite.sh
Status-check functions available in if: expressions:
success()— true if all prior steps succeeded (the default behaviour)failure()— true if any prior step failedcancelled()— true if the workflow was cancelledalways()— always true regardless of prior step results
You can also combine expressions: if: success() && github.actor != 'dependabot[bot]'. Note that the ${{ }} wrapper is optional for if: — GitHub automatically evaluates the expression.
More Related questions...