Search code examples
githubgithub-actions

Output from first job appears empty in second job in pull request GitHub Actions workflow


Inspired by answers such as this one, I've tried to create my own GHA with two jobs where the second uses the output from the first.

Things seem work work fine on push, but when the workflow is executed during a pull request the output of get_message (i.e. ${{ needs.get_message.outputs.commitmsg }}) is empty.

I specifically need to get the last commit message and use to trigger a conditional behaviour, and to make this work on a pull request I can't use github.event.head_commit.message (see e.g. here).

What am I doing wrong?

on:
  push:
    branches:
      - main
      - devl
  pull_request:
    branches:
      - main
      - devl


name: test

jobs:
  get_message:
    runs-on: ubuntu-latest
    outputs:
      commitmsg: ${{ steps.getmsg.outputs.msg }}
    
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
    
      - name: Set tags env variables
        id: getmsg
        run: |
          # Get your last commit message, not the merge commit.
          msg=$(git log -1 --no-merges --pretty=%B) >> $GITHUB_OUTPUT
          echo "commit message: $msg"
        
  test:
    if: "!contains(needs.get_message.outputs.commitmsg, '[skip-ci]')" 
    needs: get_message
    runs-on: ubuntu-latest

    steps:
      - name: test env var
        if: "!contains(needs.get_message.outputs.commitmsg, '[skip-tests]')"
        run: |
          echo "${{ needs.get_message.outputs.commitmsg }}"
          echo "not skipping tests"
          
      - name: test env var2
        if: "contains(needs.get_message.outputs.commitmsg, '[skip-tests]')"
        run: |
          echo "${{ needs.get_message.outputs.commitmsg }}"
          echo "skipping tests"

Here's the output of the "get_message" job here we see [skip-tests] is part of the message: enter image description here

But the "test" job prints an empty string: enter image description here


Solution

  • You forgot " around msg=$(git log -1 --no-merges --pretty=%B) and echo keyword before it

    Change you yml file like

    name: test
    
    on:
      push:
        branches:
          - main
          - devl
      pull_request:
        branches:
          - main
          - devl
    
    jobs:
      get_message:
        runs-on: ubuntu-latest
        outputs:
          commitmsg: ${{ steps.getmsg.outputs.msg }}
        
        steps:
          - uses: actions/checkout@v4
            with:
              ref: ${{ github.event.pull_request.head.sha }}
        
          - name: Set tags env variables
            id: getmsg
            run: |
              # Get last commit message, not the merge commit.
              msg=$(git log -1 --no-merges --pretty=%B)       
              echo "msg=$(git log -1 --no-merges --pretty=%B)" >> "$GITHUB_OUTPUT"
              echo "commit message: $msg"
            
      test:
        if: "!contains(needs.get_message.outputs.commitmsg, '[skip-ci]')" 
        needs: get_message
        runs-on: ubuntu-latest
    
        steps:
          - name: test env var
            if: "!contains(needs.get_message.outputs.commitmsg, '[skip-tests]')"
            run: |
              echo "${{ needs.get_message.outputs.commitmsg }}"
              echo "not skipping tests"
              
          - name: test env var2
            if: "contains(needs.get_message.outputs.commitmsg, '[skip-tests]')"
            run: |
              echo "${{ needs.get_message.outputs.commitmsg }}"
              echo "skipping tests"