AI Hints
Home›CI/CD with GitHub Actions Tutorial›First Workflows Practice
CI/CD with GitHub Actions · Lesson 35 of 138

Practice: Two Triggers

One workflow, many events.

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

Next challenge. ACI workflow that runs on pushes to main, on pull requests, and by hand. And one extra step that only runs on the main branch. Pause and try it.

Practice: Two Triggers
From the video: Practice: Two Triggers
  • Three triggers
  • One main-only step
  • Pause and try

One answer

Push with branches main, pull request, and workflow dispatch give us all three triggers. The tests always run. The last step has an if condition, comparing github dot ref name with main, so it only runs on the main branch. Notice the single quotes inside the expression.

.github/workflows/ci.ymlyaml
name: CI
on:
  push:
    branches: [main]
  pull_request:
  workflow_dispatch:
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - run: pytest -q
      - name: Extra checks on main
        if: github.ref_name == 'main'
        run: echo "Main is healthy at $(git rev-parse --short HEAD)"

Main and a branch

On main, the extra step runs, and prints the short commit hash. With dash dash ref, as a pull request from a feature branch, the extra step is skipped, and the log says so. Exactly what we wanted.

Terminal
run-steps .github/workflows/ci.yml
Workflow: CI  (event: push, branch: main)
Job: test
  ✓ Run pytest -q
      ...                                                                                [100%]
      3 passed in 0.01s
  ✓ Extra checks on main
      Main is healthy at 7418016
  Job succeeded
run-steps .github/workflows/ci.yml --event pull_request --ref feature
Workflow: CI  (event: pull_request, branch: feature)
Job: test
  ✓ Run pytest -q
      ...                                                                                [100%]
      3 passed in 0.01s
  - Extra checks on main  (skipped)
  Job succeeded

Summary

One workflow can listen to many events, and if conditions change what runs. Next, a quiz on this section.

Key takeaways
  • push, pull_request, dispatch
  • if: github.ref_name == 'main'
  • Skipped steps are logged
Next up: First Workflows Quiz — test yourself