name: 发布正式版本 on: push: tags: - 'v*.*.*' permissions: contents: write packages: write concurrency: group: release-${{ github.ref }} cancel-in-progress: false jobs: prepare: name: 校验正式版本 runs-on: ubuntu-24.04 outputs: app_version: ${{ steps.version.outputs.app_version }} steps: - name: 检出完整 Git 历史 uses: actions/checkout@v6 with: fetch-depth: 0 - name: 校验标签、来源与版本顺序 id: version shell: bash run: | set -Eeuo pipefail if [[ ! "$GITHUB_REF_NAME" =~ ^v([0-9]+\.[0-9]+\.[0-9]+)$ ]]; then echo "正式版本标签必须符合 v1.2.3 格式:$GITHUB_REF_NAME" >&2 exit 1 fi git fetch origin main --no-tags if ! git merge-base --is-ancestor "$GITHUB_SHA" origin/main; then echo "正式版本标签必须指向 main 已包含的提交。" >&2 exit 1 fi newest_tag="$(git tag --list | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1)" if [ "$newest_tag" != "$GITHUB_REF_NAME" ]; then echo "拒绝用旧版本 $GITHUB_REF_NAME 覆盖 latest;当前最高版本是 $newest_tag。" >&2 exit 1 fi echo "app_version=${GITHUB_REF_NAME#v}" >> "$GITHUB_OUTPUT" desktop: name: 构建桌面端安装包 needs: prepare uses: ./.github/workflows/desktop-cd.yml with: app_version: ${{ needs.prepare.outputs.app_version }} secrets: inherit docker: name: 构建 Docker 镜像 needs: prepare uses: ./.github/workflows/docker-publish.yml with: app_version: ${{ needs.prepare.outputs.app_version }} secrets: inherit publish: name: 审批并发布正式版本 needs: - prepare - desktop - docker runs-on: ubuntu-24.04 environment: name: production-release steps: - name: 检出正式版本代码 uses: actions/checkout@v6 with: fetch-depth: 0 - name: 下载全部构建产物 uses: actions/download-artifact@v8 with: pattern: ydisks-* path: dist/artifacts merge-multiple: true - name: 校验桌面产物并生成校验和 shell: bash env: APP_VERSION: ${{ needs.prepare.outputs.app_version }} run: | set -Eeuo pipefail mkdir -p dist/release dist/digests echo "下载后的构建产物:" find dist/artifacts -maxdepth 2 -type f -printf '%P\n' | sort expected_release_files=( "ydisks-xianyu-helper-linux-amd64-$APP_VERSION.tar.gz" "ydisks-xianyu-helper-linux-arm64-$APP_VERSION.tar.gz" "Ydisks-Xianyu-Helper-Setup.exe" "Ydisks-Xianyu-Helper-$APP_VERSION-arm64.pkg" "Ydisks-Xianyu-Helper-$APP_VERSION-amd64.pkg" ) for release_file in "${expected_release_files[@]}"; do source_file="dist/artifacts/$release_file" if [ ! -f "$source_file" ]; then echo "缺少正式发布产物:$release_file" >&2 exit 1 fi cp -- "$source_file" dist/release/ done digest_count=0 while IFS= read -r artifact_file; do artifact_name="$(basename "$artifact_file")" if [[ "$artifact_name" =~ ^[0-9a-f]{64}$ ]]; then cp -- "$artifact_file" dist/digests/ digest_count=$((digest_count + 1)) fi done < <(find dist/artifacts -maxdepth 2 -type f -print) if [ "$digest_count" -ne 2 ]; then echo "Docker 镜像摘要数量错误:预期 2 个,实际 $digest_count 个。" >&2 exit 1 fi echo "准备发布的桌面产物:" find dist/release -maxdepth 1 -type f -printf '%f\n' | sort echo "准备发布的 Docker 摘要:" find dist/digests -maxdepth 1 -type f -printf '%f\n' | sort (cd dist/release && sha256sum * > SHA256SUMS) - name: 配置 Docker Buildx uses: docker/setup-buildx-action@v4 - name: 登录 GitHub Container Registry uses: docker/login-action@v4 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: 发布正式 Docker manifest shell: bash env: APP_VERSION: ${{ needs.prepare.outputs.app_version }} run: | set -Eeuo pipefail image_name="ghcr.io/${GITHUB_REPOSITORY,,}" minor_version="${APP_VERSION%.*}" digest_args=() while IFS= read -r digest_file; do digest_args+=("$image_name@sha256:$(basename "$digest_file")") done < <(find dist/digests -maxdepth 1 -type f -print | sort) docker buildx imagetools create \ --tag "$image_name:$GITHUB_REF_NAME" \ --tag "$image_name:$APP_VERSION" \ --tag "$image_name:$minor_version" \ --tag "$image_name:latest" \ --tag "$image_name:sha-$GITHUB_SHA" \ "${digest_args[@]}" docker buildx imagetools inspect "$image_name:$GITHUB_REF_NAME" docker buildx imagetools inspect "$image_name:latest" - name: 创建或更新 GitHub Release shell: bash env: GH_TOKEN: ${{ github.token }} APP_VERSION: ${{ needs.prepare.outputs.app_version }} run: | set -Eeuo pipefail release_notes_file="dist/release-notes.md" git log -1 --format='提交:%h%n%n%B' "$GITHUB_REF_NAME" > "$release_notes_file" if [ ! -s "$release_notes_file" ]; then printf '%s\n' '本版本提交未提供 release notes。' > "$release_notes_file" fi mapfile -t release_files < <(find dist/release -maxdepth 1 -type f -print | sort) if gh release view "$GITHUB_REF_NAME" >/dev/null 2>&1; then gh release edit "$GITHUB_REF_NAME" \ --title "Ydisks 闲鱼助手 $APP_VERSION" \ --notes-file "$release_notes_file" gh release upload "$GITHUB_REF_NAME" "${release_files[@]}" --clobber else gh release create "$GITHUB_REF_NAME" "${release_files[@]}" \ --verify-tag \ --notes-file "$release_notes_file" \ --title "Ydisks 闲鱼助手 $APP_VERSION" fi