Search code examples
gitgithubgithub-actions

How to trigger a dependent workflow from the same branch as the caller


Suppose I have a workflow file like this:

name: deploy-image
on:
  workflow_run:
    workflows: [ "build-image" ]
    types:
      - completed

So, when build-image is completed, deploy-image is triggered.

How can I make sure that if I trigger build-image manually (using workflow_dispatch) on a particular branch, then deploy-image will also be triggered from that branch?

Currently, deploy-image ends up being triggered by the default (master) branch.

enter image description here

I have tried adding some parameters to actions\checkout but it's not working as expected.

      - uses: actions/checkout@v3
        with:
          ref: ${{ github.event.workflow_run.head_branch }}

Solution

  • Solved! Even though it doesn't look like it, but the ref in actions/checkout does work. I confirmed by adding git branch.

    name: deploy-image
    on:
      workflow_run:
        workflows: [ "build-image" ]
        types:
          - completed
    
    permissions:
      id-token: write
      contents: read
      packages: read
      issues: write
    
    jobs:
      deploy-image-cu-en:
        steps:
          - uses: actions/checkout@v3
            with:
              ref: ${{ github.event.workflow_run.head_branch }}
    
          - run: git branch
    

    enter image description here