Skip to content

Commit

Permalink
ci: fix publish step (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain authored Jun 30, 2022
1 parent 185d8f8 commit e4d9543
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 59 deletions.
11 changes: 0 additions & 11 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,3 @@ jobs:
run: |
npm i
npm run build
- name: Save PR number
if: ${{ always() }}
run: echo ${{ github.event.number }} > ./pr-id.txt

- name: Upload PR number
if: ${{ always() }}
uses: actions/upload-artifact@v2
with:
name: pr
path: ./pr-id.txt
7 changes: 4 additions & 3 deletions .github/workflows/pull_request_release_next.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,22 @@ jobs:
- name: Install dependencies & Build
run: |
npm i
npm run build
- name: Publish version
run: |
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >> .npmrc
node ./scripts/publishWithNoPrompt
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
DIST_TAG: 'alpha'

- name: Set Current Version
if: success()
run: |
CURRENT_VERSION=$(node -p 'require("./package.json").version')
echo "CURRENT_VERSION=${CURRENT_VERSION}" >> $GITHUB_ENV
DIST_VERSION=$(node -p 'require("./package.json").version')
echo "DIST_VERSION=${DIST_VERSION}" >> $GITHUB_ENV
# 发布失败后,更新 checkRun 状态
- uses: actions/github-script@v6
Expand Down
39 changes: 25 additions & 14 deletions .github/workflows/real_release.yml
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
name: Release After PR Merged
name: Release By PR Comment

on:
pull_request:
branches:
- master
types:
- closed
issue_comment:

# Example output: in a PR branch (head) named `feature-1 to a target branch (base) of `release-v1`, you'll see:
# github.base_ref: release-v1
# github.head_ref: feature-1
# github.ref: refs/pull/2/merge

jobs:
if_merged:
if: "${{ github.event.pull_request.merged == true && startsWith(github.event.pull_request.title, 'chore(release): v') }}"
if_release_comment:
if: "${{ github.event.issue.pull_request && startsWith(github.event.pull_request.title, 'chore(release): v') && startsWith(github.event.comment.body, '/release') }}"
runs-on: ubuntu-latest
steps:
- name: "Check if user has write access"
uses: "lannonbr/repo-permission-check-action@2.0.0"
with:
permission: "write"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Checkout
uses: actions/checkout@v2
with:
ref: ${{ github.base_ref }}

- name: Setup Version & Tag
run: |
CURRENT_VERSION=$(echo $PR_TITLE | sed 's/^chore(release): v//')
echo "CURRENT_VERSION=${CURRENT_VERSION}" >> $GITHUB_ENV
echo "DIST_VERSION=${CURRENT_VERSION}" >> $GITHUB_ENV
DIST_VERSION=$(node -e "console.log(require('./package.json').version)")
echo "DIST_VERSION=${DIST_VERSION}" >> $GITHUB_ENV
echo "DIST_TAG=fakeLatest" >> $GITHUB_ENV
# 在 checkTag 中使用这个环境变量
echo "TAG=${CURRENT_VERSION}" >> $GITHUB_ENV
echo "TAG=${DIST_VERSION}" >> $GITHUB_ENV
echo $PR_CONTENT >> ./releaselog.md
env:
PR_TITLE: ${{ github.event.pull_request.title }}
PR_CONTENT: ${{ github.event.pull_request.body }}

- name: Use Node.js 14
uses: actions/setup-node@v1
with:
node-version: 14
registry-url: https://registry.npmjs.org/

- uses: mukunku/tag-exists-action@v1.0.0
id: checkTag
Expand All @@ -58,14 +60,15 @@ jobs:
- name: Install dependencies & Build
run: |
npm i
npm run build
- name: Publish version
run: |
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >> .npmrc
node ./scripts/publishWithNoPrompt
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Create Release
id: create_release
Expand All @@ -79,3 +82,11 @@ jobs:
body_path: ./releaselog.md
draft: true
prerelease: true

- uses: actions/github-script@v6
if: success()
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const script = require('./scripts/github/notifyReleaseResult.js')
return await script('Release', {github, context, core})
30 changes: 18 additions & 12 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ on:
workflow_dispatch:
inputs:
version:
description: "The version you want to release, eg: 1.0.0"
description: "The version you want to release, eg: 1.0.0. **no prefix v!**"
required: true
release_branch:
description: "The release branch, eg: v2.14, if run on master, please make sure the release_branch is set"
description: "The release branch, eg: master, if run on master, please make sure the release_branch is set"
required: false
default: "master"

Expand Down Expand Up @@ -53,35 +53,41 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# 安装依赖并构建
- name: Install dependencies & Build
- name: Install dependencies
if: steps.checkTag.outputs.exists == 'false'
run: |
npm i
npm run build
# 生成 Changelog, 会将内容写入到 ./releaselog.md 中
- name: Create Changelog
if: steps.checkTag.outputs.exists == 'false'
id: release-log
- name: Generate Changelog, Version, Commit and Push
if: steps.checkTag.outputs.exists == 'false' && github.event.inputs.release_branch != ''
run: |
node ./scripts/publishWithNoPrompt
npm run changelog
git add .
git commit -am "chore(release): v${DIST_VERSION}"
git checkout -b v${DIST_VERSION}
git push origin v${DIST_VERSION}
LATEST_CHANGELOG=$(npx extract-changelog-release)
echo ${LATEST_CHANGELOG}
echo "LATEST_CHANGELOG=${LATEST_CHANGELOG}" >> $GITHUB_ENV
env:
DRY_RUN: true
DIST_TAG: latest
DIST_VERSION: ${{ github.event.inputs.version }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# 在 master 分支运行时,自动基于 Release 分支创建 PR
- name: Create Pull Request
if: github.event.ref == 'refs/heads/master' && github.event.inputs.release_branch != ''
if: github.event.inputs.release_branch != ''
uses: actions/github-script@v6
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
await github.rest.pulls.create({
owner: github.repository.owner.login,
repo: github.repository.name,
owner: context.repo.owner,
repo: context.repo.repo,
title: 'chore(release): v${{ github.event.inputs.version }}',
head: '{{ github.event.inputs.release_branch }}',
head: 'v${{ github.event.inputs.version }}',
base: 'master',
body: process.env.LATEST_CHANGELOG
})
9 changes: 5 additions & 4 deletions .github/workflows/release_candidate_by_input.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,24 @@ jobs:
- name: Install dependencies & Build
run: |
npm i
npm run build
- name: Publish version
run: |
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >> .npmrc
node ./scripts/publishWithNoPrompt
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
DIST_TAG: 'beta'

- name: Adding markdown
if: success()
run: |
CURRENT_VERSION=$(node -p 'require("./package.json").version')
echo '### Released :rocket:' $CURRENT_VERSION >> $GITHUB_STEP_SUMMARY
DIST_VERSION=$(node -p 'require("./package.json").version')
echo '### Released :rocket:' $DIST_VERSION >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo $CURRENT_VERSION >> $GITHUB_STEP_SUMMARY
echo $DIST_VERSION >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo 'user input ref:' $INPUT_REF >> $GITHUB_STEP_SUMMARY
echo '```log' >> $GITHUB_STEP_SUMMARY
Expand Down
4 changes: 2 additions & 2 deletions scripts/github/createIssueCommentPrNextResult.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { createVersionText } = require('./helpers');

module.exports = async ({ github, context, core }) => {
const commentBody = createVersionText('PR Next', process.env.CURRENT_VERSION);
const commentBody = createVersionText('PR Next', process.env.DIST_VERSION, context);

await github.rest.issues.createComment({
issue_number: context.issue.number,
Expand All @@ -19,7 +19,7 @@ module.exports = async ({ github, context, core }) => {
check_run_id: process.env.CHECK_RUN_ID,
output: {
title: 'PR Next Version publish successful!',
summary: `A version for pull request is **published**. version: **${process.env.CURRENT_VERSION}**`,
summary: `A version for pull request is **published**. version: **${process.env.DIST_VERSION}**`,
},
});
};
2 changes: 1 addition & 1 deletion scripts/github/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const createVersionText = (type = 'Pre-Release', version) => {
export const createVersionText = (type = 'Pre-Release', version, context) => {
return (
`🎉 ${type} version ` +
version +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { createVersionText } = require('./helpers');

module.exports = async ({ github, context, core }) => {
const issueBody = createVersionText('Pre-Release', process.env.CURRENT_VERSION);
module.exports = async (type, { github, context, core }) => {
const issueBody = createVersionText(type, process.env.DIST_VERSION, context);

await github.rest.issues.createComment({
issue_number: context.issue.number,
Expand Down
20 changes: 13 additions & 7 deletions scripts/publishUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ function generateSematicVersion(tag, versionLevel, currentVersion) {
}`;
} else {
// 仅限 alpha 和 beta
const newNumber = Number(curNumber) + 1;
let newNumber = Number(curNumber) + 1;
if (isNaN(newNumber)) {
newNumber = 1;
}
newVersion = `${
tag === 'alpha'
? `${curMajor}.${curMinor}.${curPatch}-alpha.${newNumber}`
: `${curMajor}.${curMinor}.${curPatch}-beta.${newNumber}`
}`;
}
return newVersion
return newVersion;
}

// 拿到版本信息, 以 antd-mini 为基准
Expand Down Expand Up @@ -98,7 +101,7 @@ function genNewVersion(tag, currentVersion) {
])
.then((res) => {
const { version } = res;
const newVersion = generateSematicVersion(tag, version, currentVersion)
const newVersion = generateSematicVersion(tag, version, currentVersion);
resolve(newVersion);
})
.catch((error) => {
Expand Down Expand Up @@ -133,11 +136,13 @@ function npmPublish(oldVersion, newVersion, tag) {
* @param {'latest' | 'alpha' | 'beta'} tag
* @param {string} newVersion
*/
function doPublish(tag, newVersion) {
function doPublish(tag, newVersion, dryRun = false) {
console.log('发布中 ...');
const originPkgJson = require(PKG_JSON_PATH);
publish('antd-mini', tag, newVersion);
publish('antd-mini-rpx', tag, newVersion);
if (!dryRun) {
publish('antd-mini', tag, newVersion);
publish('antd-mini-rpx', tag, newVersion);
}
// 只回写正式版的 package.json
if (tag === 'latest') {
originPkgJson.version = newVersion;
Expand Down Expand Up @@ -202,5 +207,6 @@ module.exports = {
updatePkgJson,
gitSync,
doPublish,
generateSematicVersion
generateSematicVersion,
execSync
};
7 changes: 4 additions & 3 deletions scripts/publishWithNoPrompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@ const {
console.log('dist tag:', process.env.DIST_TAG);

let version = process.env.DIST_VERSION;
let versionLevel = process.env.DIST_VERSION_LEVEL;
if (!version) {
const currentVersion = require('../package.json').version;
console.log(`currentVersion`, currentVersion);
version = generateSematicVersion(
process.env.DIST_TAG,
'patch',
versionLevel ?? 'patch',
currentVersion
);
}

console.log('version:', version);

doPublish(process.env.DIST_TAG, version);
doPublish(process.env.DIST_TAG, version, process.env.DRY_RUN);

if (process.env.DIST_VERSION) {
if (process.env.DIST_VERSION && !process.env.DRY_RUN) {
// 只对 release 的分支打 Tag
execSync(`git tag ${process.env.DIST_VERSION}`);
execSync(
Expand Down

0 comments on commit e4d9543

Please sign in to comment.