Skip to content

Update release.yml

Update release.yml #20

Workflow file for this run

name: Release
on:
push:
branches:
- main # 监听 main 分支的推送事件
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. 获取最新的 Release 版本号
- name: Get latest release version
id: get_latest_release
run: |
LATEST_RELEASE=$(curl --silent "https://api.github.com/repos/${{ github.repository }}/releases/latest" | jq -r '.tag_name')
echo "Latest release version: $LATEST_RELEASE"
echo "LATEST_RELEASE=$LATEST_RELEASE" >> $GITHUB_ENV
# 4. 判断版本号是否有更新
- name: Check if version has changed
id: check_version
run: |
# 版本号比较
if [ "$VERSION" != "$LATEST_RELEASE" ]; 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
fi
# 5. 设置版本号递增(如果检测到版本更新)
- 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
# 6. 更新 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
# 7. 使用 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}
]
# 8. 上传构建的发行包(可选)
- name: Upload release assets
if: env.VERSION_UPDATED == 'true'
uses: actions/upload-artifact@v3
with:
name: my-release-asset
path: ./path/to/your/build/artifacts/*.tar.gz
# 9. 提交并推送更新后的版本号
- 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
# 10. 创建 GitHub Release(如果需要)
- name: Create GitHub Release
if: env.VERSION_UPDATED == 'true'
run: |
curl -X POST \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-d '{
"tag_name": "${{ env.VERSION }}",
"name": "Release ${{ env.VERSION }}",
"body": "Release notes for version ${{ env.VERSION }}",
"draft": false,
"prerelease": false
}' \
https://api.github.com/repos/${{ github.repository }}/releases