ci: rewrite Gitee release with Python, fix empty body #36
Workflow file for this run
This file contains hidden or 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: Release | |
| on: | |
| push: | |
| tags: | |
| - 'v*' | |
| jobs: | |
| release: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Extract version from tag | |
| id: version | |
| run: echo "VERSION=${GITHUB_REF_NAME#v}" >> $GITHUB_OUTPUT | |
| - name: Update version in install.sh | |
| run: | | |
| git checkout main | |
| sed -i 's/NEXTLNMP_VER=".*"/NEXTLNMP_VER="${{ steps.version.outputs.VERSION }}"/' install.sh | |
| sed -i "s/NEXTLNMP_Ver='[0-9.]*'/NEXTLNMP_Ver='${{ steps.version.outputs.VERSION }}'/" nextlnmp.sh | |
| - name: Create tarball | |
| run: | | |
| git stash | |
| git archive --format=tar.gz --prefix=nextlnmp-${{ steps.version.outputs.VERSION }}/ -o nextlnmp-${{ steps.version.outputs.VERSION }}.tar.gz main | |
| git stash pop || true | |
| - name: Calculate SHA256 | |
| id: sha256 | |
| run: | | |
| SHA=$(sha256sum nextlnmp-${{ steps.version.outputs.VERSION }}.tar.gz | awk '{print $1}') | |
| echo "SHA256=${SHA}" >> $GITHUB_OUTPUT | |
| echo "SHA256: ${SHA}" | |
| - name: Update SHA256 in install.sh | |
| run: | | |
| sed -i "s/TARBALL_SHA256=\".*\"/TARBALL_SHA256=\"${{ steps.sha256.outputs.SHA256 }}\"/" install.sh | |
| - name: Save CHANGELOG before cleanup | |
| run: | | |
| cp CHANGELOG_DRAFT.md /tmp/changelog-saved.md 2>/dev/null || echo "No CHANGELOG" > /tmp/changelog-saved.md | |
| - name: Update README | |
| run: | | |
| export VERSION=${{ steps.version.outputs.VERSION }} | |
| export DATE=$(date '+%Y-%m-%d') | |
| python3 tools/update_readme.py | |
| - name: Push all changes in one commit | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git add install.sh nextlnmp.sh README.md CHANGELOG_DRAFT.md | |
| git diff --cached --quiet || git commit -m "chore: release v${{ steps.version.outputs.VERSION }} (version, SHA256, README)" | |
| git push origin main | |
| - name: Generate release body | |
| run: | | |
| VERSION=${{ steps.version.outputs.VERSION }} | |
| SHA=${{ steps.sha256.outputs.SHA256 }} | |
| CHANGELOG=$(cat /tmp/changelog-saved.md) | |
| cat > /tmp/release-body.md << BODYEOF | |
| ## ⚡ 一行安装 | |
| bash <(curl -sL https://gitee.com/palmmedia/nextlnmp/raw/main/install.sh) | |
| ## 📦 SHA256 | |
| ${SHA} nextlnmp-${VERSION}.tar.gz | |
| ## 📋 更新内容 | |
| ${CHANGELOG} | |
| --- | |
| 官网:https://nextlnmp.cn · QQ群:615298 | |
| BODYEOF | |
| sed -i 's/^ //' /tmp/release-body.md | |
| - name: Create GitHub Release | |
| uses: softprops/action-gh-release@v2 | |
| with: | |
| name: "NextLNMP v${{ steps.version.outputs.VERSION }}" | |
| body_path: /tmp/release-body.md | |
| files: nextlnmp-${{ steps.version.outputs.VERSION }}.tar.gz | |
| - name: Sync to mirror server | |
| uses: appleboy/ssh-action@v1.0.3 | |
| with: | |
| host: 8.148.21.254 | |
| username: root | |
| key: ${{ secrets.MIRROR_SSH_KEY }} | |
| script: | | |
| cd /data/mirror | |
| wget -q -O nextlnmp-${{ steps.version.outputs.VERSION }}.tar.gz https://github.com/NextLNMP/nextlnmp/releases/download/v${{ steps.version.outputs.VERSION }}/nextlnmp-${{ steps.version.outputs.VERSION }}.tar.gz | |
| echo "Mirror synced: nextlnmp-${{ steps.version.outputs.VERSION }}.tar.gz" | |
| - name: Create Gitee Release | |
| env: | |
| GITEE_TOKEN: ${{ secrets.GITEE_TOKEN }} | |
| VERSION: ${{ steps.version.outputs.VERSION }} | |
| run: | | |
| # 先删除已存在的同名 release(防止重发版冲突) | |
| OLD_ID=$(curl -s "https://gitee.com/api/v5/repos/palmmedia/nextlnmp/releases/tags/v${VERSION}?access_token=${GITEE_TOKEN}" | python3 -c "import sys,json; print(json.load(sys.stdin).get('id',''))" 2>/dev/null || echo "") | |
| if [ -n "$OLD_ID" ] && [ "$OLD_ID" != "None" ] && [ "$OLD_ID" != "" ]; then | |
| echo "Deleting old Gitee release: $OLD_ID" | |
| curl -s -X DELETE "https://gitee.com/api/v5/repos/palmmedia/nextlnmp/releases/${OLD_ID}?access_token=${GITEE_TOKEN}" | |
| fi | |
| # 用 Python 创建 release(避免 shell 引号嵌套问题) | |
| python3 << 'GITEE_PYEOF' | |
| import json, os, subprocess | |
| token = os.environ["GITEE_TOKEN"] | |
| version = os.environ["VERSION"] | |
| body = "" | |
| if os.path.exists("/tmp/release-body.md"): | |
| body = open("/tmp/release-body.md").read().strip() | |
| if not body: | |
| body = f"NextLNMP v{version}" | |
| payload = json.dumps({ | |
| "access_token": token, | |
| "tag_name": f"v{version}", | |
| "name": f"NextLNMP v{version}", | |
| "body": body, | |
| "target_commitish": "main", | |
| "prerelease": False | |
| }) | |
| result = subprocess.run( | |
| ["curl", "-s", "-X", "POST", | |
| "https://gitee.com/api/v5/repos/palmmedia/nextlnmp/releases", | |
| "-H", "Content-Type: application/json", | |
| "-d", payload], | |
| capture_output=True, text=True | |
| ) | |
| print("Gitee API response:", result.stdout) | |
| try: | |
| release_id = json.loads(result.stdout)["id"] | |
| print(f"Gitee Release ID: {release_id}") | |
| tarball = f"nextlnmp-{version}.tar.gz" | |
| upload = subprocess.run( | |
| ["curl", "-s", "-X", "POST", | |
| f"https://gitee.com/api/v5/repos/palmmedia/nextlnmp/releases/{release_id}/attach_files", | |
| "-F", f"access_token={token}", | |
| "-F", f"file=@{tarball}"], | |
| capture_output=True, text=True | |
| ) | |
| print("Upload response:", upload.stdout) | |
| except (KeyError, json.JSONDecodeError) as e: | |
| print(f"Failed to create Gitee release: {e}") | |
| exit(1) | |
| GITEE_PYEOF |