Skip to main content

Events and triggers


Table of contents

  1. Types of events
  2. Git events
  3. GitHub events
  4. Scheduled events
  5. Manual triggering
  6. Practical exercises


1 - Types of events

Categories

Overview

CategoryEventsUsage
Gitpush, pull_requestStandard CI/CD
GitHubissues, release, forkAutomation
SchedulecronRecurring tasks
Manualworkflow_dispatchTests, deployments
Externalrepository_dispatchExternal integrations

🔝 Back to table of contents



2 - Git events

push

on:
push:
# Branches spécifiques
branches:
- main
- develop
- 'release/**' # Pattern glob
- '!release/beta' # Exclusion

# Tags
tags:
- 'v*' # v1.0.0, v2.0.0
- '!v*-beta' # Exclure beta

# Chemins (fichiers modifiés)
paths:
- 'src/**'
- 'package.json'
- '!**/*.md' # Exclure markdown

paths-ignore:
- 'docs/**'
- '*.md'

pull_request

on:
pull_request:
branches:
- main
- develop

types:
- opened # PR ouverte
- synchronize # Nouveaux commits
- reopened # PR réouverte
- closed # PR fermée
- ready_for_review # PR prête pour review

paths:
- 'src/**'

pull_request_target

# Pour les forks (attention : risque de sécurité)
on:
pull_request_target:
types: [opened, synchronize]

jobs:
build:
runs-on: ubuntu-latest
steps:
# Checkout du repo cible (pas du fork)
- uses: actions/checkout@v4
Security

pull_request_target has access to the repo's secrets. Never run unverified code from the fork!

Difference between PR events

EventContextSecretsUsage
pull_requestFork❌ NoStandard CI
pull_request_targetBase✅ YesLabeling, comments

🔝 Back to table of contents



3 - GitHub events

issues

on:
issues:
types:
- opened
- edited
- deleted
- labeled
- unlabeled
- assigned
- closed

jobs:
auto-label:
if: github.event.action == 'opened'
runs-on: ubuntu-latest
steps:
- run: echo "New issue: ${{ github.event.issue.title }}"

release

on:
release:
types:
- published # Release publiée
- created # Release créée
- edited # Release modifiée
- prereleased # Pre-release

jobs:
publish:
if: github.event.action == 'published'
runs-on: ubuntu-latest
steps:
- run: echo "Publishing ${{ github.event.release.tag_name }}"

workflow_run

# Déclenché après un autre workflow
on:
workflow_run:
workflows: ["CI"] # Nom du workflow source
types:
- completed # Terminé (succès ou échec)
branches: [main]

jobs:
deploy:
# Seulement si CI réussi
if: github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
steps:
- run: echo "Deploying after successful CI"

Other useful events

# Fork du repo
on: fork

# Star ajoutée
on:
watch:
types: [started]

# Discussion créée
on:
discussion:
types: [created]

# Package publié
on:
package:
types: [published]

🔝 Back to table of contents



4 - Scheduled events

cron syntax

on:
schedule:
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of week (0 - 6)
# │ │ │ │ │
# * * * * *
- cron: '0 0 * * *' # Tous les jours à minuit
- cron: '30 8 * * 1-5' # Lun-Ven à 8h30
- cron: '0 */6 * * *' # Toutes les 6 heures

Common examples

ExpressionDescription
0 0 * * *Every day at midnight UTC
0 8 * * 1Every Monday at 8am
0 0 1 * *First day of the month
*/15 * * * *Every 15 minutes
0 0 * * 0Every Sunday

Complete example

name: Nightly Build

on:
schedule:
- cron: '0 2 * * *' # 2h du matin UTC

jobs:
nightly:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Run nightly tests
run: npm run test:e2e

- name: Send report
run: ./send-report.sh
Note
  • Schedules use the UTC time zone
  • Possible delay of up to 15 minutes during peak load
  • Only triggers on the default branch

🔝 Back to table of contents



5 - Manual triggering

workflow_dispatch

on:
workflow_dispatch:
inputs:
environment:
description: 'Environment to deploy'
required: true
type: choice
options:
- staging
- production
default: 'staging'

version:
description: 'Version to deploy'
required: true
type: string

dry_run:
description: 'Dry run mode'
required: false
type: boolean
default: false

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Deploy
run: |
echo "Environment: ${{ inputs.environment }}"
echo "Version: ${{ inputs.version }}"
echo "Dry run: ${{ inputs.dry_run }}"

Input types

TypeDescription
stringFree text
booleanCheckbox
choiceDropdown list
environmentEnvironment selector

repository_dispatch

# Déclenché par API externe
on:
repository_dispatch:
types: [deploy, build]

jobs:
handle:
runs-on: ubuntu-latest
steps:
- run: |
echo "Event: ${{ github.event.action }}"
echo "Payload: ${{ toJSON(github.event.client_payload) }}"
# Déclencher via API
curl -X POST \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/OWNER/REPO/dispatches \
-d '{"event_type":"deploy","client_payload":{"env":"prod"}}'

🔝 Back to table of contents



6 - Practical exercises

Exercise 1: CI on PR

Create a workflow that runs on PRs to main:

Solution
name: PR Check

on:
pull_request:
branches: [main]
types: [opened, synchronize, reopened]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm test

Exercise 2: Manual deployment

Create a workflow with an environment choice:

Solution
name: Manual Deploy

on:
workflow_dispatch:
inputs:
environment:
type: choice
options: [staging, production]
required: true

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- run: echo "Deploying to ${{ inputs.environment }}"

Quiz

Q1. Which event allows triggering via API?

Answer

repository_dispatch

Q2. Which time zone does the schedule use?

Answer

UTC

🔝 Back to table of contents



Key takeaways

  • push and pull_request for standard CI
  • Filter by branches, tags, paths
  • schedule with cron syntax (UTC)
  • workflow_dispatch for manual triggering
  • repository_dispatch for external API
  • workflow_run to chain workflows
  • pull_request_target: watch out for security!

🔝 Back to table of contents


← Previous chapter | Next chapter: Jobs and steps →