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:andpaths-ignore:narrow down when an event counts. ${{ … }}is an expression. GitHub substitutes it before the runner sees the step. Inside are contexts such asgithub,inputs,steps,needs.if:skips steps or jobs.$GITHUB_OUTPUTpasses 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 -qbranches: [main]: Pushes to other branches no longer triggerpush. 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 2Expected (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=MoinExpected:
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_dispatchstarts the workflow only manually – in the browser via “Run workflow” or withgh workflow run. For that it must live on the default branch.-f name=wertsets an input. Without-f stufe=…, the defaultleiseapplies.${{ inputs.gruss }}sits underenv:, not directly inrun:. Day 9 shows why – in short: inputs are foreign text and never belong directly in a script.- The runner itself sets
GITHUB_SHAandGITHUB_REF_NAMEas environment variables.${GITHUB_SHA::7}is Bash and shortens to seven characters.
gh run list --workflow hallo.yml --limit 1Expected:
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=lautWith 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.ymlid: shagives the step a name. Whatever it writes asname=wertinto the file$GITHUB_OUTPUTis afterwards available understeps.sha.outputs.name.- Jobs run on separate runners. A value only travels via the job’s
outputs:andneeds:in the next one. needs: infoalso defines the order:zeigenwaits forinfo. Withoutneeds, jobs run in parallel.$GITHUB_STEP_SUMMARYtakes 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 --fillExpected (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.
--filltakes title and body from the commit.- The push to
version-vorgabedoes not triggerpush(onlymain), but the pull request does triggerpull_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 -qA new push to the pull request’s branch restarts the check. After a few seconds:
gh pr checks --watchExpected (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 -1Expected (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)
--squashcombines both commits of the branch into one onmain.ghfetches the new state ofmainright away (Fast-forward).- The merge is a push to
main–ciruns there once more.
Save your progress? Sign up for free
Checkpoint
gh workflow listExpected:
NAME STATE ID
ci active 405482113
hallo active 405485865
gh run list --workflow ci.yml --limit 1 --json event,conclusionExpected (as soon as the run after the merge is finished; before
that, conclusion is empty):
[{"conclusion":"success","event":"push"}]
gh pr list --state mergedExpected:
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 --fillAfter a few seconds:
gh pr checks --watch >/dev/null
gh run list --workflow ci.yml --limit 1With the ID:
gh run view <ID> --log | grep 'PR pr-info'
gh pr merge --squash --delete-branchExpected (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 tomainis enforced by a ruleset (Day 4). scheduletakes 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_targetlifts that – and is one of the most common causes of compromised repositories (Day 9).