Skip to content
Hands-on IT IT Drills

Day 2: Triggers and Expressions

Goal: Define when a workflow runs – push to specific branches, pull requests, manual start with inputs – and pass data between steps and jobs with expressions, conditions and outputs. · Duration: approx. 75 min · Prerequisite: Day 1

Briefing

  • on: knows dozens of events. The most important: push, pull_request, workflow_dispatch (manual), schedule (timetable).
  • Filters such as branches: and paths-ignore: narrow down when an event counts.
  • ${{ … }} is an expression. GitHub substitutes it before the runner sees the step. Inside are contexts such as github, inputs, steps, needs.
  • if: skips steps or jobs. $GITHUB_OUTPUT passes values to later steps, outputs: to later jobs.
 push main ──┐
 pull_request ┼─► on: ─► jobs.info ──outputs──► jobs.zeigen (needs: info)
 dispatch ───┘           steps ── $GITHUB_OUTPUT ──► steps.<id>.outputs.<name>

Drill 1: Narrowing down triggers

So far every push starts the workflow, even a change to a README. New ci.yml:

cd ~/gha-lab/notes-actions
cat > .github/workflows/ci.yml <<'EOF'
name: ci
on:
  push:
    branches: [main]
    paths-ignore: ['**.md']
  pull_request:

jobs:
  test:
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-go@v7
        with:
          go-version-file: go.mod
      - name: Statische Prüfung
        run: go vet ./...
      - run: go test ./...
EOF
git commit -qam "ci: nur main und Pull-Requests"
git push -q
  • branches: [main]: Pushes to other branches no longer trigger push. For them there is the pull request.
  • paths-ignore: ['**.md']: If a push changes only Markdown files, nothing runs.
  • pull_request: without a filter: every pull request, no matter which branch it targets.

Now just a README:

echo "# notes-actions" > README.md
git add README.md
git commit -qm "README"
git push -q
gh run list --limit 2

Expected (after a few seconds, IDs differ):

STATUS  TITLE                         WORKFLOW  BRANCH  EVENT  ID           ELAPSED  AGE
✓       ci: nur main und Pull-Requ... ci        main    push   41607670136  24s      1m
✓       ci: go vet                    ci        main    push   41607356701  19s      3m

The commit “README” is not in the list – no run.

Save your progress? Sign up for free

Drill 2: Manual start with inputs

A second workflow to experiment with:

cat > .github/workflows/hallo.yml <<'EOF'
name: hallo
on:
  workflow_dispatch:
    inputs:
      gruss:
        description: Womit begrüßen?
        default: Hallo
      stufe:
        type: choice
        options: [leise, laut]
        default: leise

jobs:
  info:
    runs-on: ubuntu-24.04
    steps:
      - name: Begrüßen
        env:
          GRUSS: ${{ inputs.gruss }}
          WER: ${{ github.actor }}
        run: echo "$GRUSS, $WER! Commit ${GITHUB_SHA::7} auf $GITHUB_REF_NAME"
EOF
git add .github
git commit -qm "hallo: Workflow zum Ausprobieren"
git push -q
gh workflow run hallo.yml -f gruss=Moin

Expected:

https://github.com/<du>/notes-actions/actions/runs/41607798762

gh reports the address of the new run; the number at the end is its ID.

  • workflow_dispatch starts the workflow only manually – in the browser via “Run workflow” or with gh workflow run. For that it must live on the default branch.
  • -f name=wert sets an input. Without -f stufe=…, the default leise applies.
  • ${{ inputs.gruss }} sits under env:, not directly in run:. Day 9 shows why – in short: inputs are foreign text and never belong directly in a script.
  • The runner itself sets GITHUB_SHA and GITHUB_REF_NAME as environment variables. ${GITHUB_SHA::7} is Bash and shortens to seven characters.
gh run list --workflow hallo.yml --limit 1

Expected:

STATUS  TITLE  WORKFLOW  BRANCH  EVENT              ID           ELAPSED  AGE
*       hallo  hallo     main    workflow_dispatch  41607798762  1s       now

Wait until the run is finished (gh run watch <ID>), then:

gh run view <ID> --log | grep 'Moin,'

Expected (timestamp shortened, commit differs):

info    Begrüßen    2026-11-03T09:02:38Z Moin, <du>! Commit 5fa343e auf main

Save your progress? Sign up for free

Drill 3: Conditions

cat > .github/workflows/hallo.yml <<'EOF'
name: hallo
on:
  workflow_dispatch:
    inputs:
      gruss:
        description: Womit begrüßen?
        default: Hallo
      stufe:
        type: choice
        options: [leise, laut]
        default: leise

jobs:
  info:
    runs-on: ubuntu-24.04
    steps:
      - name: Begrüßen
        env:
          GRUSS: ${{ inputs.gruss }}
          WER: ${{ github.actor }}
        run: echo "$GRUSS, $WER! Commit ${GITHUB_SHA::7} auf $GITHUB_REF_NAME"
      - name: Laut
        if: inputs.stufe == 'laut'
        run: echo "DAS WAR LAUT"
      - name: Nur bei Fehler
        if: failure()
        run: echo "Ein Step davor ist gescheitert"
EOF
git commit -qam "hallo: Bedingungen"
git push -q
gh workflow run hallo.yml -f stufe=laut

With the ID from gh run list --workflow hallo.yml --limit 1, as soon as the run is finished:

gh run view <ID> -v | grep -E 'Laut|Fehler'

Expected:

  ✓ Laut
  - Nur bei Fehler
  • In if: you may leave out ${{ }} – GitHub reads the value as an expression anyway.
  • Without anything specified, if: success() applies: a step runs only if all before it were green. failure() turns that around, always() always runs.
  • - means skipped. For comparison, start without -f stufe=laut: then “Laut” shows a - too.

Save your progress? Sign up for free

Drill 4: Outputs between steps and jobs

cat > .github/workflows/hallo.yml <<'EOF'
name: hallo
on:
  workflow_dispatch:
    inputs:
      gruss:
        description: Womit begrüßen?
        default: Hallo

jobs:
  info:
    runs-on: ubuntu-24.04
    outputs:
      kurz: ${{ steps.sha.outputs.kurz }}
    steps:
      - name: Kurzen Commit ermitteln
        id: sha
        run: echo "kurz=${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT"
      - name: Im selben Job lesen
        env:
          KURZ: ${{ steps.sha.outputs.kurz }}
        run: echo "Step-Output $KURZ"

  zeigen:
    needs: info
    runs-on: ubuntu-24.04
    steps:
      - name: Im nächsten Job lesen
        env:
          KURZ: ${{ needs.info.outputs.kurz }}
        run: |
          echo "Job-Output $KURZ"
          echo "### Gebaut aus $KURZ" >> "$GITHUB_STEP_SUMMARY"
EOF
git commit -qam "hallo: Outputs"
git push -q
gh workflow run hallo.yml
  • id: sha gives the step a name. Whatever it writes as name=wert into the file $GITHUB_OUTPUT is afterwards available under steps.sha.outputs.name.
  • Jobs run on separate runners. A value only travels via the job’s outputs: and needs: in the next one.
  • needs: info also defines the order: zeigen waits for info. Without needs, jobs run in parallel.
  • $GITHUB_STEP_SUMMARY takes Markdown that GitHub shows on the run’s page.

With the new ID, as soon as the run is finished:

gh run view <ID> --log | grep -E 'Output [0-9a-f]{7}'

--log also contains the command lines themselves (echo "Step-Output $KURZ"). The pattern Output followed by seven hex characters matches only the outputs. Expected (commit differs):

info    Im selben Job lesen 2026-11-03T09:07:06Z Step-Output 053f22b
zeigen  Im nächsten Job lesen   2026-11-03T09:07:11Z Job-Output 053f22b

Save your progress? Sign up for free

Drill 5: A pull request

The default version should become 0.5.0-dev – on a branch of its own:

git switch -c version-vorgabe
sed -i 's/"dev"/"0.5.0-dev"/' main.go
git commit -qam "version: 0.5.0-dev als Vorgabe"
git push -q -u origin version-vorgabe
gh pr create --fill

Expected (shortened):

...
Creating pull request for version-vorgabe into main in <du>/notes-actions

https://github.com/<du>/notes-actions/pull/1

The lines with remote: before it come from the push: GitHub itself suggests creating a pull request there.

  • --fill takes title and body from the commit.
  • The push to version-vorgabe does not trigger push (only main), but the pull request does trigger pull_request.

Wait a few seconds until GitHub has created the check:

gh pr checks --watch

--watch refreshes the table until all checks are done. Expected (at the end):

Some checks were not successful
0 cancelled, 1 failing, 0 successful, 0 skipped, and 0 pending checks

   NAME                    DESCRIPTION  ELAPSED  URL
X  ci/test (pull_request)               17s      https://github.com/<du>/notes-...

Red. Why? The ID is in gh run list --limit 1:

gh run view <ID> --log-failed | grep -E 'FAIL|version='

Expected (preceding columns shortened):

... --- FAIL: TestMetrics (0.00s)
...     main_test.go:97: fehlt: notes_build_info{store="memory",version="dev"} 1
...         notes_build_info{store="memory",version="0.5.0-dev"} 1
... FAIL
... FAIL    example.com/notes-api   0.147s
... FAIL

The test expects the old default. This is exactly what the check in the pull request is for: the error shows up before it lands on main. Adjust the test and push again:

sed -i 's/version="dev"/version="0.5.0-dev"/' main_test.go
git commit -qam "test: Version 0.5.0-dev"
git push -q

A new push to the pull request’s branch restarts the check. After a few seconds:

gh pr checks --watch

Expected (at the end):

All checks were successful
0 cancelled, 0 failing, 1 successful, 0 skipped, and 0 pending checks

   NAME                    DESCRIPTION  ELAPSED  URL
✓  ci/test (pull_request)               18s      https://github.com/<du>/notes-...

Merge and clean up:

gh pr merge --squash --delete-branch
git log --oneline -1

Expected (shortened):

✓ Squashed and merged pull request <du>/notes-actions#1 (version: 0.5.0-dev als Vorgabe)
...
Fast-forward
 main.go      | 2 +-
 main_test.go | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
✓ Deleted local branch version-vorgabe and switched to branch main
✓ Deleted remote branch version-vorgabe
363ae6a (HEAD -> main, origin/main) version: 0.5.0-dev als Vorgabe (#1)
  • --squash combines both commits of the branch into one on main.
  • gh fetches the new state of main right away (Fast-forward).
  • The merge is a push to main – ci runs there once more.

Save your progress? Sign up for free

Checkpoint

gh workflow list

Expected:

NAME   STATE   ID
ci     active  405482113
hallo  active  405485865
gh run list --workflow ci.yml --limit 1 --json event,conclusion

Expected (as soon as the run after the merge is finished; before that, conclusion is empty):

[{"conclusion":"success","event":"push"}]
gh pr list --state merged

Expected:

Showing 1 of 1 pull request in <du>/notes-actions that match your search

ID  TITLE                           BRANCH           CREATED AT
#1  version: 0.5.0-dev als Vorgabe  version-vorgabe  about 1 minute ago

Challenge

Add a step to ci.yml that runs only for pull requests and writes to the summary which branch is to be merged into which. Check it with a second pull request.

Hint: github.event_name, github.head_ref and github.base_ref.

Solution

cat > .github/workflows/ci.yml <<'EOF'
name: ci
on:
  push:
    branches: [main]
    paths-ignore: ['**.md']
  pull_request:

jobs:
  test:
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-go@v7
        with:
          go-version-file: go.mod
      - name: Statische Prüfung
        run: go vet ./...
      - run: go test ./...
      - name: PR-Info
        if: github.event_name == 'pull_request'
        env:
          VON: ${{ github.head_ref }}
          NACH: ${{ github.base_ref }}
        run: echo "PR $VON -> $NACH" | tee -a "$GITHUB_STEP_SUMMARY"
EOF
git switch -c pr-info
git commit -qam "ci: PR-Info"
git push -q -u origin pr-info
gh pr create --fill

After a few seconds:

gh pr checks --watch >/dev/null
gh run list --workflow ci.yml --limit 1

With the ID:

gh run view <ID> --log | grep 'PR pr-info'
gh pr merge --squash --delete-branch

Expected (shortened):

test    PR-Info 2026-11-03T09:10:00Z PR pr-info -> main
✓ Squashed and merged pull request <du>/notes-actions#2 (ci: PR-Info)
...
✓ Deleted remote branch pr-info

tee -a writes the line to the log and appends it to the summary.

Save your progress? Sign up for free

Enterprise Note

  • Protect branches, don’t just filter: branches: [main] only controls when the workflow runs. That nobody pushes directly to main is enforced by a ruleset (Day 4).
  • schedule takes cron expressions in UTC, e.g. cron: '17 3 * * 1' for Mondays at 03:17. Runs start only from the default branch, can be delayed at the top of the hour, and in inactive public repositories GitHub disables them after 60 days.
  • Pull requests from forks get no secrets and a token with read permissions only. pull_request_target lifts that – and is one of the most common causes of compromised repositories (Day 9).