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.
More on GitHub Actions
What are workflows, jobs, steps and actions in GitHub Actions?BeginnerWhat events can trigger a workflow?BeginnerWhat is the difference between `pull_request` and `pull_request_target`?AdvancedWhat is a runner, and how do GitHub-hosted and self-hosted runners differ?BeginnerHow do you pass data between steps and between jobs?IntermediateHow does a build matrix work?Intermediate