bashgithubgithub-actionsgithub-release

How can I set the "git tag" message as an environment variable in Github Actions?


I want to automate the release process of pypdf. The idea is that I just push a git tag and a Github Action creates a Github release.

In Bash, I use the following to set the `tag_body:

latest_tag=$(git describe --tags --abbrev=0)
tag_body=$(git tag -l "${latest_tag}" --format='%(contents:body)')

That works. But I also get the desired name with the following two steps, but the tag_body variable is always empty:

      - name: Prepare variables
        id: prepare_variables
        run: |
          git fetch --tags --force
          latest_tag=$(git describe --tags --abbrev=0)
          echo "latest_tag=$(git describe --tags --abbrev=0)" >> "$GITHUB_ENV"
          echo "tag_body<<EOF" >> $GITHUB_ENV
          echo "$(git tag -l \"${latest_tag}\" --format='%(contents:body)')" >> $GITHUB_ENV
          echo "EOF" >> $GITHUB_ENV
          echo "date=$(date +'%Y-%m-%d')" >> "$GITHUB_ENV"
          echo "tag_body2=$(git tag -l \"${latest_tag}\" --format='%(contents:body)')" >> $GITHUB_ENV
          echo "tag_body3=$(git tag -l ${git describe --tags --abbrev=0} --format='%(contents:body)')" >> $GITHUB_ENV
      - name: Create GitHub Release 🚀
        uses: actions/create-release@v1
        with:
          tag_name: ${{ github.ref }}
          release_name: Version ${{ env.latest_tag }}, ${{ env.date }}
          draft: false
          prerelease: false
          body: Body is ${{ env.tag_body }}

My suspicion is that it's either due to escaping or due to the multi-line string. How can I make the tag_body variable have the same value as locally in Bash?

Context

In case you wonder about the project: https://github.com/py-pdf/pypdf/pull/1970


Solution

  • tag_body is blank in your example because the following outputs blank:

    echo "$(git tag -l \"${latest_tag}\" --format='%(contents:body)')"
    

    You can verify that this is blank locally by doing:

    echo "$(git tag -l \"3.12.2\" --format='%(contents:body)')"
    

    By removing the \" around the $latest_tag reference, you will see that echo then output something:

    echo "$(git tag -l ${latest_tag} --format='%(contents:body)')"
    

    at which point the pipe into $GITHUB_ENV will now receive data. However, you could drop the echo altogether and pipe the output of the command itself into $GITHUB_ENV instead of passing it to echo to pipe it:

    git tag -l "${latest_tag}" --format='%(contents:body)' >> $GITHUB_ENV