skip to Main Content

I have a "Workflow B" that is automatically triggered when "Workflow A" is completed. Workflow A is triggered by each new Pull Request so the branch changes dynamically.

name: Workflow B
on:
  workflow_run:
    workflows: ['Workflow A']
    types:
      - completed
  workflow_dispatch:

jobs:
  first-workflow-job:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js
        uses: actions/setup-node@v3
        with:
          cache: 'npm'

I would expect Workflow B to run on the same branch as Workflow A.
However, Workflow B runs on master.
How can I share the branch value between these workflows?

2

Answers


  1. Chosen as BEST ANSWER

    I have finally found the solution to my problem! Again, thanks to everyone who contributed so far.

    So the solution relies on workflow_run context. The only addition we need is in Workflow_B where we access the trigger workflow's head branch:

    name: Workflow B
    on:
      workflow_run:
        workflows: ['Workflow A']
        types:
          - completed
      workflow_dispatch:
    
    jobs:
      first-workflow-job:
        runs-on: ubuntu-latest
    
        steps:
          - uses: actions/checkout@v3
            with:
              ref: ${{ github.event.workflow_run.head_branch }}
          - name: Use Node.js
            uses: actions/setup-node@v3
            with:
              cache: 'npm'
    

  2. GitHub Actions does not seem to support directly sharing outputs between different workflow.

    Workflows are designed to be self-contained, and while you can trigger one workflow after another using the workflow_run event, there’s no built-in method for passing data directly from one workflow to another.

    Workarounds, such as using artifacts or the repository itself to pass data, have certain restrictions and are often not reliable across separate workflows due to the concurrent nature of workflows.

    You might consider dawidd6/action-download-artifact, an action that downloads and extracts uploaded artifacts associated with a given workflow and commit or other criteria.

    name: Workflow B
    on:
      workflow_run:
        workflows: ['Workflow A']
        types:
          - completed
      workflow_dispatch:
    
    jobs:
      first-workflow-job:
        runs-on: ubuntu-latest
    
        steps:
          - uses: actions/checkout@v3
          - name: Use Node.js
            uses: actions/setup-node@v3
            with:
              cache: 'npm'
          - name: Download artifact
            uses: dawidd6/action-download-artifact@v2
            with:
              workflow: 'Workflow A'
              workflow_conclusion: success
              name: branch # replace with the actual name of the artifact you want to download
    

    with workflow A:

    jobs:
      first-workflow-job:
        runs-on: ubuntu-latest
    
        steps:
          ...
          - name: Set branch value as output
            id: set_branch
            run: |
              BRANCH_NAME="${{ github.head_ref }}"
              echo "branch_name=$BRANCH_NAME" > branch.txt
          - name: Archive production artifacts
            uses: actions/upload-artifact@v2
            with:
              name: branch
              path: branch.txt
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search