Skip to content

Branch Based Workflow Execution

olsido edited this page Aug 5, 2024 · 1 revision

Use Case

Running workflows based on specific branches is useful for branch-specific operations, such as deployments to different environments or running different tests based on the branch.

Implementation

We will create one workflow for the ‘develop’ branch and another workflow for the ‘master’ branch. The final workflow will run only if the previous workflow ran on the ‘develop’ branch.

Workflow on ‘develop’ Branch (.github/workflows/branch-based-workflow-execution–develop.yml)

name: Branch Based Workflow Execution - Develop  # Name of the workflow

on:
  push:  # Trigger this workflow on push events
    branches:
      - develop  # Only trigger on pushes to the develop branch

jobs:
  develop-job:  # Job name
    runs-on: ubuntu-latest  # Use the latest Ubuntu runner
    steps:
      - name: Echo Develop Branch  # Step to echo a message
        run: echo "This workflow runs on the develop branch."  # Print a message indicating the branch

View on Gist       View in this repo

Workflow on ‘master’ Branch (.github/workflows/branch-based-workflow-execution–master.yml)

name: Branch Based Workflow Execution - Final  # Name of the workflow

on:
  workflow_run:  # Trigger this workflow based on the completion of another workflow
    workflows: ["Branch Based Workflow Execution - Develop", "Branch Based Workflow Execution - Master"]  # List of workflows to monitor
    types: [completed]  # Trigger on completion of the monitored workflows
    branches:
      - develop  # Only trigger if the monitored workflow ran on the develop branch

jobs:
  final-job:  # Job name
    runs-on: ubuntu-latest  # Use the latest Ubuntu runner
    steps:
      - name: Echo Final Step  # Step to echo a message
        run: echo "The previous workflow ran on the develop branch."  # Print a message indicating the monitored workflow ran on the develop branch

View on Gist       View in this repo

Final Workflow (.github/workflows/branch-based-workflow-execution–final.yml)

name: Branch Based Workflow Execution - Final  # Name of the workflow

on:
  workflow_run:  # Trigger this workflow based on the completion of another workflow
    workflows: ["Branch Based Workflow Execution - Develop", "Branch Based Workflow Execution - Master"]  # List of workflows to monitor
    types: [completed]  # Trigger on completion of the monitored workflows
    branches:
      - develop  # Only trigger if the monitored workflow ran on the develop branch

jobs:
  final-job:  # Job name
    runs-on: ubuntu-latest  # Use the latest Ubuntu runner
    steps:
      - name: Echo Final Step  # Step to echo a message
        run: echo "The previous workflow ran on the develop branch."  # Print a message indicating the monitored workflow ran on the develop branch

View on Gist       View in this repo

This setup ensures that workflows run based on the branch they are associated with. The final workflow only runs if the previous workflows ran on the ‘develop’ branch, demonstrating branch-specific workflow dependencies.

Clone this wiki locally