Merge pull request #336 from Hobart2967/master #139
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: "prepare release" | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
- master | |
paths: | |
- "package*.json" | |
- "src/**" | |
pull_request_target: | |
types: [labeled] | |
env: | |
BASE_BRANCH: "master" | |
HEAD_BRANCH: "prepare-release" | |
jobs: | |
label: | |
name: Clear label | |
runs-on: ubuntu-latest | |
if: contains(github.event.pull_request.labels.*.name, 'prepare-release') | |
steps: | |
- name: Clear label | |
uses: actions/github-script@v6.4.0 | |
env: | |
NUMBER: ${{ github.event.number }} | |
with: | |
script: | | |
const { NUMBER } = process.env; | |
await github.rest.issues.removeLabel({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: NUMBER, | |
name: 'prepare-release', | |
}); | |
preparation: | |
name: Prepare release | |
runs-on: ubuntu-latest | |
if: > | |
contains('push,workflow_dispatch', github.event_name) || | |
contains(github.event.pull_request.labels.*.name, 'prepare-release') | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3.3.0 | |
with: | |
ref: ${{ env.BASE_BRANCH }} | |
fetch-depth: 0 | |
- name: Setup nodejs | |
uses: actions/setup-node@v3.6.0 | |
with: | |
node-version: 14 | |
- name: Bump version | |
run: | | |
npx standard-version --skip.commit --skip.tag | |
sed 's/^### \[/## [/' -i CHANGELOG.md | |
npx prettier --write CHANGELOG.md --prose-wrap never --ignore-path ./gitignore | |
- name: Get version from package.json | |
uses: actions/github-script@v6.4.0 | |
id: version | |
with: | |
result-encoding: string | |
script: | | |
const version = require(`${process.env.GITHUB_WORKSPACE}/package.json`).version; | |
core.setOutput('version', version); | |
core.setOutput('title', 'chore(release): :bookmark: release v' + version); | |
- name: Create commit | |
env: | |
REMOTE_URL: https://x-access-token:${{secrets.GITHUB_TOKEN}}@github.com/${{github.repository}}.git | |
run: | | |
git config --global user.name 'github-actions[bot]' | |
git config --global user.email 'github-actions[bot]@users.noreply.github.com' | |
git add . | |
git commit -m 'chore(release): :bookmark: release v${{steps.version.outputs.version}}' | |
git branch -D '${{ env.HEAD_BRANCH }}' 2>&1 | sed 's/error/warning/' | |
git checkout -b '${{ env.HEAD_BRANCH }}' | |
git remote set-url origin '${{ env.REMOTE_URL }}' | |
git push -f -u origin '${{ env.HEAD_BRANCH }}' | |
- name: Get changelog context | |
id: changelog-reader | |
uses: mindsers/changelog-reader-action@v2.2.2 | |
with: | |
version: ${{ steps.version.outputs.version }} | |
path: ./CHANGELOG.md | |
- name: Prepare release PR | |
uses: actions/github-script@v6.4.0 | |
env: | |
TITLE: ${{ steps.version.outputs.title }} | |
CHANGELOG: ${{ steps.changelog-reader.outputs.changes }} | |
with: | |
script: | | |
const { HEAD_BRANCH, BASE_BRANCH, TITLE, CHANGELOG } = process.env; | |
const query = { | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
head: `${HEAD_BRANCH}`, | |
base: `${BASE_BRANCH}`, | |
title: `${TITLE}`, | |
body: `${CHANGELOG}`, | |
}; | |
core.debug(`query:\n${JSON.stringify(query, null, 2)}`); | |
const resp = await github.rest.pulls.list({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
state: 'open', | |
head: `${context.repo.owner}:${HEAD_BRANCH}`, | |
base: `${BASE_BRANCH}`, | |
direction: 'desc', | |
}); | |
core.debug(`resp:\n${JSON.stringify(resp, null, 2)}`); | |
try { | |
const pull_number = resp.data[0].number; | |
core.info(`[info]: pull_number: ${pull_number}`); | |
query.pull_number = pull_number; | |
const resp1 = await github.rest.pulls.update(query); | |
core.info('[info]: Update release PR successfully!'); | |
core.info(`[info]: See pull request in ${resp1.data.html_url}`); | |
} catch (err) { | |
if (err.message === 'Cannot read properties of undefined (reading \'number\')') { | |
core.info('[info]: Not found previous pull request.'); | |
core.info('[info]: Try to create a new pull request.'); | |
const resp2 = await github.rest.pulls.create(query); | |
core.info('[info]: Create release PR successfully!'); | |
core.info(`[info]: See pull request in ${resp2.data.html_url}`); | |
} else core.setFailed(`Prepare release PR failed with error:\n${err}`); | |
} | |
core.info(`[info]: Prepare release PR successfully!`); |