🐙
GitHub Actionsでtagのpushをtriggerに前tagからの差分をまとめたreleaseを生成する
完成形
on:
  push:
    tags:
      - 'v*'
name: Create Release
jobs:
  build:
    name: Create Release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Get the version
        id: get_version
        run: echo ::set-output name=VERSION::${GITHUB_REF#refs/tags/}
      - name: Get commit summary
        id: get_commit_summary
        run: |
          PREVIOUS_TAG=$(git tag --sort=-creatordate | sed -n 2p)
          echo "PREVIOUS_TAG: $PREVIOUS_TAG"
          COMMIT_SUMMARY="$(git log --oneline --pretty=tformat:"%h %s" $PREVIOUS_TAG..${{ github.ref }})"
          COMMIT_SUMMARY="${COMMIT_SUMMARY//$'\n'/'%0A'}"
          echo ::set-output name=COMMIT_SUMMARY::$COMMIT_SUMMARY
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ steps.get_version.outputs.VERSION }}
          release_name: Release ${{ steps.get_version.outputs.VERSION }}
          body: |
            ${{ steps.get_commit_summary.outputs.COMMIT_SUMMARY }}
          draft: false
          prerelease: false
解説
PREVIOUS_TAG=$(git tag --sort=-creatordate | sed -n 2p)
一つ前のtagを取得。
COMMIT_SUMMARY="$(git log --oneline --pretty=tformat:"%h %s" $PREVIOUS_TAG..${{ github.ref }})"
git log --oneline --pretty=tformat:"%h %s" $PREVIOUS_TAG..${{ github.ref }}で一つ前のtagとのcommit historyの差分をformatして取得。
COMMIT_SUMMARY="${COMMIT_SUMMARY//$'\n'/'%0A'}"
set-outputで改行がエスケープされないので、Process Escape Characters in Release 'body'より、行末の'\n'を'%0A'に置換。
echo ::set-output name=COMMIT_SUMMARY::$COMMIT_SUMMARY
set-outputでget_commit_summaryのoutputに設定。
結果
v*にマッチするtagをpushするといい感じにreleaseが生成される。


Discussion