From f5f8d1694734122c2ef2a5ab660eb106087c4a09 Mon Sep 17 00:00:00 2001 From: jrandolf <101637635+jrandolf@users.noreply.github.com> Date: Thu, 19 May 2022 13:26:42 +0200 Subject: [PATCH] chore: automate tag on release (#8366) --- .github/workflows/publish-on-tag.yml | 6 ++--- .github/workflows/release.yml | 2 +- .github/workflows/tag-on-release.yml | 30 ++++++++++++++++++++++++ utils/get_latest_changelog.js | 34 ++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/tag-on-release.yml create mode 100755 utils/get_latest_changelog.js diff --git a/.github/workflows/publish-on-tag.yml b/.github/workflows/publish-on-tag.yml index 9487bbf38d7dd..bcee3cabcb2b5 100644 --- a/.github/workflows/publish-on-tag.yml +++ b/.github/workflows/publish-on-tag.yml @@ -8,7 +8,8 @@ on: jobs: publish: runs-on: ubuntu-latest - permissions: read-all + permissions: + contents: read steps: - name: Checkout uses: actions/checkout@v3 @@ -40,8 +41,7 @@ jobs: needs: publish runs-on: ubuntu-latest permissions: - actions: read|write - contents: read|write + contents: write steps: - name: Checkout uses: actions/checkout@v3 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 22469d320a421..9e52dacb81acc 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -6,7 +6,7 @@ jobs: release: runs-on: ubuntu-latest permissions: - contents: read|write + contents: write pull-requests: write steps: - name: Checkout diff --git a/.github/workflows/tag-on-release.yml b/.github/workflows/tag-on-release.yml new file mode 100644 index 0000000000000..8cf0e97ed449c --- /dev/null +++ b/.github/workflows/tag-on-release.yml @@ -0,0 +1,30 @@ +name: tag-on-release + +on: + pull_request: + types: + - closed + branches: + - 'releases/**' + +jobs: + tag: + if: github.event.pull_request.merged == true + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Get version + id: get-version + run: | + echo ::set-output name=VERSION::$(jq -r .version ./package.json) + - name: Generate latest changelog + run: node utils/get_latest_changelog.js > ${{ steps.get-version.outputs.VERSION }}-CHANGELOG.txt + - name: Tag and release + uses: softprops/action-gh-release@v1 + with: + name: ${{ steps.get-version.outputs.VERSION }} + tag_name: v${{ steps.get-version.outputs.VERSION }} + body_path: ${{ steps.get-version.outputs.VERSION }}-CHANGELOG.txt diff --git a/utils/get_latest_changelog.js b/utils/get_latest_changelog.js new file mode 100755 index 0000000000000..254f9e8b5f160 --- /dev/null +++ b/utils/get_latest_changelog.js @@ -0,0 +1,34 @@ +#!/usr/bin/env node + +(async () => { + const { createReadStream } = require('fs'); + const { join } = require('path'); + const { createInterface } = require('readline'); + + const lines = []; + let isRecording = false; + + for await (const line of createInterface({ + input: createReadStream(join(__dirname, '../CHANGELOG.md'), { + encoding: 'utf-8', + }), + })) { + if (line.startsWith('## ')) { + if (!isRecording) { + isRecording = true; + continue; + } else { + break; + } + } + if (isRecording) { + lines.push(line); + } + } + + if (lines.length === 0) { + throw new Error('Latest changelog should be non-empty.'); + } + + console.log(lines.join('\n').trim()); +})();