Skip to content

Latest commit

 

History

History
362 lines (249 loc) · 4.08 KB

File metadata and controls

362 lines (249 loc) · 4.08 KB

Events in GitHub Actions (Triggers)


Definition In GitHub Actions, an event is something that happens inside GitHub which causes a workflow to start. Events tell the workflow when it should run.

Every workflow starts with:

on:

Whatever you write under this section decides the trigger.


Types of Events There are many events. Below are the most commonly used events in DevOps and CI/CD teaching.

  1. push
  2. pull_request
  3. workflow_dispatch
  4. schedule
  5. release
  6. issues
  7. create and delete
  8. workflow_run
  9. path based triggers
  10. tag based triggers
  11. multiple events combined

Now each one explained in detail.


  1. push Event

Runs when someone pushes code to a branch.

Example:

on: push

Runs on all branches.

If you want only main branch:

on:
  push:
    branches: [ main ]

If you want multiple branches:

on:
  push:
    branches:
      - main
      - dev

Push event runs whenever a commit is pushed.


  1. pull_request Event

Runs when a pull request is created, reopened, or updated.

Example:

on: pull_request

Runs for all PR branches targeting any branch.

To run only when PR targets main:

on:
  pull_request:
    branches: [ main ]

Usage: Testing, linting, security scanning before merging.


  1. workflow_dispatch Event

Used for manual running. Adds a "Run workflow" button in Actions tab.

on:
  workflow_dispatch:

Without this, manual run is not possible.

You can also add inputs:

on:
  workflow_dispatch:
    inputs:
      username:
        description: "Enter name"
        required: true

  1. schedule Event

Used to run workflows on a time schedule (cron jobs).

Example: run every day at midnight.

on:
  schedule:
    - cron: "0 0 * * *"

Example: run every hour

cron: "0 * * * *"

Usage: Nightly builds Daily reports Automatic backups Regular health checks


  1. release Event

Runs when a release is created.

on:
  release:
    types: [created]

Usage: Packaging Publishing software Deploying final artifacts


  1. issues Event

Runs when an issue is opened, edited, or closed.

Example:

on:
  issues:
    types: [opened]

Usage: Auto reply bot Auto label bot


  1. create and delete Events

Runs when a branch or tag is created or deleted.

Example:

on:
  create

or

on:
  delete

Usage: Notify team when new branch is created Handle tag creation


  1. workflow_run Event

Triggered after another workflow finishes.

Example:

on:
  workflow_run:
    workflows: ["Build"]
    types:
      - completed

This runs after the workflow named Build completes.

Usage: Chaining workflows Post-deployment tasks


  1. Path based events

Trigger workflow only when specific files are changed.

Example:

on:
  push:
    paths:
      - "src/**"

Runs only if files inside src folder changed.

Exclude paths:

on:
  push:
    paths-ignore:
      - "docs/**"

Usage: Run tests only when code changes Skip workflow when docs change


  1. Tag based events

Trigger on tag creation.

Example:

on:
  push:
    tags:
      - "v*"

Runs when tag starts with v (v1.0, v2.3 etc)

Usage: Release builds Version packaging


  1. Multiple Events Combined

You can combine events using list.

Example:

on:
  push:
  pull_request:
  workflow_dispatch:

Or:

on: [push, pull_request]

Complete Workflow Example Using Many Events

name: Multi Event Workflow

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
  workflow_dispatch:
  schedule:
    - cron: "0 5 * * *"
  release:
    types: [created]

jobs:
  demo:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Triggered workflow"

Explanation: Runs on push to main Runs on PR to main Runs manually Runs every day at 5AM Runs when a release is created


Simple Final Explanation

Events decide when a workflow will run. Different events are used for different automation needs. push and pull_request are the most used. workflow_dispatch is for manual run. schedule is for cron jobs.