Search code examples
bashgitgithub-actions

Why does `git log` only display a single line when run in a GitHub Actions workflow?


I'm experiencing some strange behavior in GitHub Actions.
I'm attempting to generate a sort of "changelog" between two releases. For this, I format the output of git log to become simply a bullet point list of commit hashes and their respective messages.
I do that with this command:

git log --pretty=format:"- %s (%H)" ${{ steps.previous-release.outputs.tag_name }}..HEAD

steps.previous-release.outputs.tag_name is the output of cardinalby/git-get-release-action@v1 and works correctly.

To export the changelog into a GitHub step output, I run the following:

- id: create-changelog
  run: |
    git fetch --all --tags

    {
      echo 'changelog<<EOF'
      git log --pretty=format:"- %s (%H)" ${{ steps.previous-release.outputs.tag_name }}..HEAD
      echo ""
      echo 'EOF'
    } >> $GITHUB_OUTPUT

However, despite there being multiple commits between the two targets (I can confirm this locally), the resulting output only contains a single line.


Strangely, the extra echo "" is necessary. Otherwise, the step results in an error:

Error: Unable to process file command 'output' successfully.
Error: Invalid value. Matching delimiter not found 'EOF'

It's as if git log consumes the character immediately after its output (newline in the example above)? I'm not sure if that gives any clues as to the original issue.


Solution

  • In your checkout step, you must be using the default configuration. By default, actions/checkout performs a shallow clone and fetches only the latest commit i.e. fetch-depth: 1.

    For any git operation that requires the complete history, you need to configure fetch-depth option i.e. fetch-depth: 0. That should fix your issue.

    Example:

    - name: Checkout
      uses: actions/checkout@v4
      with:
        fetch-depth: 0