In GitHub Actions, a trigger defines when and why a workflow runs. Triggers listen for specific events in your repository. Understanding triggers is essential to ensure your workflow runs at the right time and on the right branches.
- Triggered when code is pushed to a branch or tag.
- Commonly used for CI pipelines to automatically build and test new commits.
- Example: trigger on
mainbranch:
on:
push:
branches:
- main- Trigger on tags (e.g., version release):
on:
push:
tags:
- 'v*.*.*'- Triggered on pull requests targeting specific branches.
- Useful for running tests and validations before merging code.
- Example:
on:
pull_request:
branches:
- main- Trigger workflows automatically on a schedule using cron syntax.
- Use case: nightly builds, periodic tests, or maintenance tasks.
- Example: daily at midnight:
on:
schedule:
- cron: "0 0 * * *"- Manual trigger from the GitHub UI.
- Can accept inputs/parameters for flexible workflows.
- Example:
on:
workflow_dispatch:
inputs:
env:
description: 'Deployment environment'
required: true
default: 'staging'