AI Hints
Home›CI/CD with GitHub Actions Tutorial›Conditions and Matrices
CI/CD with GitHub Actions · Lesson 47 of 138

Status Functions

Success, failure, always.

⏱ 2 min read▶ Video at 53:34🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

Normally, once a step fails, the rest of the job is skipped. But some steps should run especially when something fails, like sending an alert, or always, like cleaning up. Status functions handle that.

Status Functions
From the video: Status Functions
  • Failure skips the rest
  • Alerts on failure
  • Clean up always

failure() and always()

If failure runs a step only when an earlier step failed. Perfect for alerts. If always runs a step no matter what. Perfect for cleanup. A step with no if has a hidden if success, so it only runs when everything before it passed.

.github/workflows/status.ymlyaml
name: Status
on: push
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - run: pytest -q
      - name: Tell the team
        if: failure()
        run: echo "Tests failed, sending an alert"
      - name: Clean up
        if: always()
        run: echo "Cleaning up, whatever happened"
      - name: Celebrate
        run: echo "All good"

Pass, then fail

When the tests pass, the alert is skipped, cleanup runs, and we celebrate. Now we break the code. The tests fail, so the alert runs, cleanup still runs, and the celebrate step is skipped.

Terminal
run-steps .github/workflows/status.yml | grep -E "^  [-✓✗]"
  ✓ Run pytest -q
  - Tell the team  (skipped)
  ✓ Clean up
  ✓ Celebrate
sed -i 's/tax=0.2/tax=0.3/' app.py
run-steps .github/workflows/status.yml | grep -E "^  [-✓✗]"
  ✗ Run pytest -q
  ✓ Tell the team
  ✓ Clean up

Summary

Success is the default. Failure runs only after a failure, and always runs no matter what. Next, testing many versions at once, with a matrix.

Key takeaways
  • success() default
  • if: failure()
  • if: always()
Next up: Matrix Builds — many versions at once