Skip to content

Update release.yml

Update release.yml #9

Workflow file for this run

on:
push:
branches:
- main # 监听 main 分支的推送
name: Release
jobs:
release-please:
runs-on: ubuntu-latest
steps:
# 1. 检查代码库并读取版本号
- name: Checkout code
uses: actions/checkout@v3
# 2. 获取当前版本号并检查是否有更新
- name: Get current version from package.json
id: get_version
run: |
VERSION=$(jq -r '.version' package.json)
echo "Current version: $VERSION"
echo "VERSION=$VERSION" >> $GITHUB_ENV
# 3. 检查 package.json 中版本号是否更新
- name: Check if version has changed
id: check_version
run: |
git fetch origin main # 获取最新的远程 main 分支
PREVIOUS_VERSION=$(git show origin/main:package.json | jq -r '.version')
echo "Previous version: $PREVIOUS_VERSION"
# 如果版本号发生变化,继续执行发布流程
if [ "$VERSION" != "$PREVIOUS_VERSION" ]; then
echo "Version has changed. Proceeding with release."
echo "VERSION_UPDATED=true" >> $GITHUB_ENV
else
echo "No version update detected. Skipping release."
echo "VERSION_UPDATED=false" >> $GITHUB_ENV
# 4. 设置版本号递增(如果检测到版本更新)
- name: Set version for cyclic increment
id: set_version
if: env.VERSION_UPDATED == 'true'
run: |
MAJOR_MINOR=$(echo "${{ env.VERSION }}" | cut -d '.' -f 1,2)
PATCH=$(echo "${{ env.VERSION }}" | cut -d '.' -f 3)
if [[ "$PATCH" -ge 30 ]]; then
NEXT_MINOR=$(( $(echo "${MAJOR_MINOR}" | cut -d '.' -f 2) + 1 ))
VERSION=$(echo "${MAJOR_MINOR}" | cut -d '.' -f 1).${NEXT_MINOR}.0
else
VERSION="${MAJOR_MINOR}.$((PATCH + 1))"
fi
echo "New version is $VERSION"
echo "VERSION=$VERSION" >> $GITHUB_ENV
# 5. 更新 package.json 中的版本号
- name: Update package.json version
if: env.VERSION_UPDATED == 'true'
run: |
jq --arg version "$VERSION" '.version = $version' package.json > tmp.$$.json && mv tmp.$$.json package.json
# 6. 使用 release-please 进行发布
- name: Release Please Action
if: env.VERSION_UPDATED == 'true'
uses: GoogleCloudPlatform/release-please-action@v3
id: release
with:
token: ${{ secrets.ACCESS_TOKEN }}
release-type: node
package-name: standard-version
version: ${{ env.VERSION }} # 动态使用更新后的版本号
changelog-types: '[{"type": "types", "section":"Types", "hidden": false},{"type": "revert", "section":"Reverts", "hidden": false},{"type": "feat", "section": "Features", "hidden": false},{"type": "fix", "section": "Bug Fixes", "hidden": false},{"type": "improvement", "section": "Feature Improvements", "hidden": false},{"type": "docs", "section":"Docs", "hidden": false},{"type": "style", "section":"Styling", "hidden": false},{"type": "refactor", "section":"Code Refactoring", "hidden": false},{"type": "perf", "section":"Performance Improvements", "hidden": false},{"type": "test", "section":"Tests", "hidden": false},{"type": "build", "section":"Build System", "hidden": false},{"type": "ci", "section":"CI", "hidden":false}]'
# 7. 提交并推送更新后的版本号
- name: Commit version update
if: env.VERSION_UPDATED == 'true'
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git add package.json
git commit -m "chore: update version to $VERSION"
git push origin main