Interview Prep/CI/CD/GitHub Actions

How do `if` conditions and status functions work?

IntermediateConceptGitHub Actions
Answer

if: takes an expression (no ${{ }} needed). By default a step runs only if previous steps succeeded. Status functions change that: always(), failure(), success(), cancelled().

- name: Upload logs on failure
  if: failure()
  uses: actions/upload-artifact@v4
  with: { name: logs, path: logs/ }
- name: Deploy only from main
  if: github.ref == 'refs/heads/main' && github.event_name == 'push'
  run: ./deploy.sh

Prefer always() with care: it also runs after cancellation. !cancelled() is often what you want for cleanup and reporting.

All CI/CD questions

More on GitHub Actions