Skip to content

Update release.yml

Update release.yml #18

Workflow file for this run

name: Release
on:
push:
branches:
- main
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0 # 确保拉取所有历史记录和标签
- name: Get latest release tag
id: get_latest_release
run: |
LATEST_TAG=$(curl -s https://api.github.com/repos/${{ github.repository }}/releases/latest | jq -r '.tag_name')
echo "LATEST_TAG=$LATEST_TAG" >> $GITHUB_ENV
- name: Read package.json version
id: read_package_version
run: |
PACKAGE_VERSION=$(node -p "require('./package.json').version")
echo "PACKAGE_VERSION=$PACKAGE_VERSION" >> $GITHUB_ENV
- name: Compare versions
id: compare_versions
run: |
if [ -z "$LATEST_TAG" ]; then
echo "No previous tag found. Generating release notes from initial commit."
echo "RELEASE=true" >> $GITHUB_ENV
echo "PREVIOUS_TAG=$(git rev-list --max-parents=0 HEAD)" >> $GITHUB_ENV
elif [ "$LATEST_TAG" != "v${PACKAGE_VERSION}" ]; then
echo "Version has changed, proceeding with release."
echo "RELEASE=true" >> $GITHUB_ENV
echo "PREVIOUS_TAG=$LATEST_TAG" >> $GITHUB_ENV
else
echo "No version change detected."
echo "RELEASE=false" >> $GITHUB_ENV
fi
- name: Generate Release Notes
if: env.RELEASE == 'true'
id: generate_release_notes
run: |
git fetch --tags
# 获取提交信息并格式化
RELEASE_NOTES=$(git log ${{ env.PREVIOUS_TAG }}..HEAD
- name: Generate Full Changelog
if: env.RELEASE == 'true'
id: generate_full_changelog
run: |
REPO_URL=$(echo "https://github.com/${{ github.repository }}")
FULL_CHANGELOG="$REPO_URL/compare/${{ env.PREVIOUS_TAG }}...v${{ env.PACKAGE_VERSION }}"
echo "FULL_CHANGELOG=$FULL_CHANGELOG" >> $GITHUB_ENV
- name: Configure Git
if: env.RELEASE == 'true'
run: |
git config --global user.name "GitHub Actions"
git config --global user.email "[email protected]"
- name: Add package.json and commit changes
if: env.RELEASE == 'true'
run: |
git add package.json
git commit -m "Bump version to ${{ env.PACKAGE_VERSION }}" || echo "No changes to commit"
- name: Create and push tag
if: env.RELEASE == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git tag v${{ env.PACKAGE_VERSION }}
git push origin main --tags
- name: Create Release
if: env.RELEASE == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
uses: actions/create-release@v1
with:
tag_name: v${{ env.PACKAGE_VERSION }}
release_name: v${{ env.PACKAGE_VERSION }}
body: |
## What's Changed
${{ env.RELEASE_NOTES }}
**Full Changelog**: ${{ env.FULL_CHANGELOG }}