refactor: 重构GitHub Actions工作流程为统一的自动发布管道

- 删除分离的workflow文件(auto-version-bump, release-on-version, docker-publish, release)
- 创建新的统一workflow: auto-release-pipeline.yml
- 整合版本管理、Release创建、Docker构建和Telegram通知到单一流程
- 使用[skip ci]标记避免死循环
- 解决GitHub Action提交无法触发后续workflow的问题
This commit is contained in:
shaw
2025-07-24 14:01:21 +08:00
parent d2f16e416c
commit 17e9aafe6e
4 changed files with 99 additions and 302 deletions

View File

@@ -1,21 +1,19 @@
name: Release on Version Change name: Auto Release Pipeline
on: on:
push: push:
branches: branches:
- main - main
paths:
- 'VERSION'
permissions: permissions:
contents: write contents: write
packages: write packages: write
jobs: jobs:
check-and-release: release-pipeline:
runs-on: ubuntu-latest runs-on: ubuntu-latest
# 只处理由GitHub Actions提交的VERSION更新 # 跳过由GitHub Actions创建的提交,避免死循环
if: github.event.pusher.name == 'github-actions[bot]' if: github.event.pusher.name != 'github-actions[bot]' && !contains(github.event.head_commit.message, '[skip ci]')
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v4 uses: actions/checkout@v4
@@ -23,29 +21,89 @@ jobs:
fetch-depth: 0 fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }} token: ${{ secrets.GITHUB_TOKEN }}
- name: Verify only VERSION changed - name: Check if version bump is needed
id: verify id: check
run: | run: |
# 获取最后一次提交变更的文件 # 获取当前提交的文件变更
CHANGED_FILES=$(git diff --name-only HEAD~1..HEAD) CHANGED_FILES=$(git diff --name-only HEAD~1..HEAD 2>/dev/null || git diff --name-only $(git rev-list --max-parents=0 HEAD)..HEAD)
echo "Changed files: $CHANGED_FILES" echo "Changed files:"
echo "$CHANGED_FILES"
# 检查是否只有VERSION文件 # 检查是否只有无关文件(.md, docs/, .github/等)
if [ "$CHANGED_FILES" = "VERSION" ]; then SIGNIFICANT_CHANGES=false
echo "Only VERSION file changed, proceeding with release" while IFS= read -r file; do
echo "should_release=true" >> $GITHUB_OUTPUT # 跳过空行
[ -z "$file" ] && continue
# 读取新版本号 # 检查是否是需要忽略的文件
NEW_VERSION=$(cat VERSION | tr -d '[:space:]') if [[ ! "$file" =~ \.(md|txt)$ ]] &&
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT [[ ! "$file" =~ ^docs/ ]] &&
echo "new_tag=v$NEW_VERSION" >> $GITHUB_OUTPUT [[ ! "$file" =~ ^\.github/ ]] &&
[[ "$file" != "VERSION" ]] &&
[[ "$file" != ".gitignore" ]] &&
[[ "$file" != "LICENSE" ]]; then
echo "Found significant change in: $file"
SIGNIFICANT_CHANGES=true
break
fi
done <<< "$CHANGED_FILES"
if [ "$SIGNIFICANT_CHANGES" = true ]; then
echo "Significant changes detected, version bump needed"
echo "needs_bump=true" >> $GITHUB_OUTPUT
else else
echo "Other files changed besides VERSION, skipping release" echo "No significant changes, skipping version bump"
echo "should_release=false" >> $GITHUB_OUTPUT echo "needs_bump=false" >> $GITHUB_OUTPUT
fi fi
- name: Get current version
if: steps.check.outputs.needs_bump == 'true'
id: get_version
run: |
# 获取最新的tag版本
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
echo "Latest tag: $LATEST_TAG"
# 从tag中提取版本号
VERSION=${LATEST_TAG#v}
echo "Current version: $VERSION"
echo "current_version=$VERSION" >> $GITHUB_OUTPUT
- name: Calculate next version
if: steps.check.outputs.needs_bump == 'true'
id: next_version
run: |
VERSION="${{ steps.get_version.outputs.current_version }}"
# 分割版本号
IFS='.' read -r -a version_parts <<< "$VERSION"
MAJOR="${version_parts[0]:-0}"
MINOR="${version_parts[1]:-0}"
PATCH="${version_parts[2]:-0}"
# 默认递增patch版本
NEW_PATCH=$((PATCH + 1))
NEW_VERSION="${MAJOR}.${MINOR}.${NEW_PATCH}"
echo "New version: $NEW_VERSION"
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "new_tag=v$NEW_VERSION" >> $GITHUB_OUTPUT
- name: Update VERSION file
if: steps.check.outputs.needs_bump == 'true'
run: |
echo "${{ steps.next_version.outputs.new_version }}" > VERSION
# 配置git
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# 提交VERSION文件 - 添加 [skip ci] 以避免再次触发
git add VERSION
git commit -m "chore: sync VERSION file with release ${{ steps.next_version.outputs.new_tag }} [skip ci]"
- name: Install git-cliff - name: Install git-cliff
if: steps.verify.outputs.should_release == 'true' if: steps.check.outputs.needs_bump == 'true'
run: | run: |
wget -q https://github.com/orhun/git-cliff/releases/download/v1.4.0/git-cliff-1.4.0-x86_64-unknown-linux-gnu.tar.gz wget -q https://github.com/orhun/git-cliff/releases/download/v1.4.0/git-cliff-1.4.0-x86_64-unknown-linux-gnu.tar.gz
tar -xzf git-cliff-1.4.0-x86_64-unknown-linux-gnu.tar.gz tar -xzf git-cliff-1.4.0-x86_64-unknown-linux-gnu.tar.gz
@@ -53,11 +111,11 @@ jobs:
sudo mv git-cliff-1.4.0/git-cliff /usr/local/bin/ sudo mv git-cliff-1.4.0/git-cliff /usr/local/bin/
- name: Generate changelog - name: Generate changelog
if: steps.verify.outputs.should_release == 'true' if: steps.check.outputs.needs_bump == 'true'
id: changelog id: changelog
run: | run: |
# 获取上一个tag以来的更新日志 # 获取上一个tag以来的更新日志
LATEST_TAG=$(git describe --tags --abbrev=0 --exclude="${{ steps.verify.outputs.new_tag }}" 2>/dev/null || echo "") LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -n "$LATEST_TAG" ]; then if [ -n "$LATEST_TAG" ]; then
# 排除VERSION文件的提交 # 排除VERSION文件的提交
CHANGELOG=$(git-cliff --config .github/cliff.toml $LATEST_TAG..HEAD --strip header | grep -v "bump version" | sed '/^$/d' || echo "- 代码优化和改进") CHANGELOG=$(git-cliff --config .github/cliff.toml $LATEST_TAG..HEAD --strip header | grep -v "bump version" | sed '/^$/d' || echo "- 代码优化和改进")
@@ -69,25 +127,23 @@ jobs:
echo "EOF" >> $GITHUB_OUTPUT echo "EOF" >> $GITHUB_OUTPUT
- name: Create and push tag - name: Create and push tag
if: steps.verify.outputs.should_release == 'true' if: steps.check.outputs.needs_bump == 'true'
run: | run: |
NEW_TAG="${{ steps.verify.outputs.new_tag }}" NEW_TAG="${{ steps.next_version.outputs.new_tag }}"
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag -a "$NEW_TAG" -m "Release $NEW_TAG" git tag -a "$NEW_TAG" -m "Release $NEW_TAG"
git push origin "$NEW_TAG" git push origin HEAD:main "$NEW_TAG"
- name: Create GitHub Release - name: Create GitHub Release
if: steps.verify.outputs.should_release == 'true' if: steps.check.outputs.needs_bump == 'true'
uses: softprops/action-gh-release@v1 uses: softprops/action-gh-release@v1
with: with:
tag_name: ${{ steps.verify.outputs.new_tag }} tag_name: ${{ steps.next_version.outputs.new_tag }}
name: Release ${{ steps.verify.outputs.new_version }} name: Release ${{ steps.next_version.outputs.new_version }}
body: | body: |
## 🐳 Docker 镜像 ## 🐳 Docker 镜像
```bash ```bash
docker pull ${{ secrets.DOCKERHUB_USERNAME || 'weishaw' }}/claude-relay-service:${{ steps.verify.outputs.new_tag }} docker pull ${{ secrets.DOCKERHUB_USERNAME || 'weishaw' }}/claude-relay-service:${{ steps.next_version.outputs.new_tag }}
docker pull ${{ secrets.DOCKERHUB_USERNAME || 'weishaw' }}/claude-relay-service:latest docker pull ${{ secrets.DOCKERHUB_USERNAME || 'weishaw' }}/claude-relay-service:latest
``` ```
@@ -104,15 +160,15 @@ jobs:
# Docker构建步骤 # Docker构建步骤
- name: Set up QEMU - name: Set up QEMU
if: steps.verify.outputs.should_release == 'true' if: steps.check.outputs.needs_bump == 'true'
uses: docker/setup-qemu-action@v3 uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx - name: Set up Docker Buildx
if: steps.verify.outputs.should_release == 'true' if: steps.check.outputs.needs_bump == 'true'
uses: docker/setup-buildx-action@v3 uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub - name: Log in to Docker Hub
if: steps.verify.outputs.should_release == 'true' if: steps.check.outputs.needs_bump == 'true'
uses: docker/login-action@v3 uses: docker/login-action@v3
with: with:
registry: docker.io registry: docker.io
@@ -120,31 +176,31 @@ jobs:
password: ${{ secrets.DOCKERHUB_TOKEN }} password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push Docker image - name: Build and push Docker image
if: steps.verify.outputs.should_release == 'true' if: steps.check.outputs.needs_bump == 'true'
uses: docker/build-push-action@v5 uses: docker/build-push-action@v5
with: with:
context: . context: .
platforms: linux/amd64,linux/arm64 platforms: linux/amd64,linux/arm64
push: true push: true
tags: | tags: |
${{ secrets.DOCKERHUB_USERNAME }}/claude-relay-service:${{ steps.verify.outputs.new_tag }} ${{ secrets.DOCKERHUB_USERNAME }}/claude-relay-service:${{ steps.next_version.outputs.new_tag }}
${{ secrets.DOCKERHUB_USERNAME }}/claude-relay-service:latest ${{ secrets.DOCKERHUB_USERNAME }}/claude-relay-service:latest
${{ secrets.DOCKERHUB_USERNAME }}/claude-relay-service:${{ steps.verify.outputs.new_version }} ${{ secrets.DOCKERHUB_USERNAME }}/claude-relay-service:${{ steps.next_version.outputs.new_version }}
labels: | labels: |
org.opencontainers.image.version=${{ steps.verify.outputs.new_version }} org.opencontainers.image.version=${{ steps.next_version.outputs.new_version }}
org.opencontainers.image.revision=${{ github.sha }} org.opencontainers.image.revision=${{ github.sha }}
cache-from: type=gha cache-from: type=gha
cache-to: type=gha,mode=max cache-to: type=gha,mode=max
- name: Send Telegram Notification - name: Send Telegram Notification
if: steps.verify.outputs.should_release == 'true' && env.TELEGRAM_BOT_TOKEN != '' && env.TELEGRAM_CHAT_ID != '' if: steps.check.outputs.needs_bump == 'true' && env.TELEGRAM_BOT_TOKEN != '' && env.TELEGRAM_CHAT_ID != ''
env: env:
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }} TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }} TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
continue-on-error: true continue-on-error: true
run: | run: |
VERSION="${{ steps.verify.outputs.new_version }}" VERSION="${{ steps.next_version.outputs.new_version }}"
TAG="${{ steps.verify.outputs.new_tag }}" TAG="${{ steps.next_version.outputs.new_tag }}"
REPO="${{ github.repository }}" REPO="${{ github.repository }}"
# 获取更新内容并限制长度 # 获取更新内容并限制长度

View File

@@ -1,102 +0,0 @@
name: Auto Version Bump
on:
push:
branches:
- main
permissions:
contents: write
jobs:
version-bump:
runs-on: ubuntu-latest
# 跳过由GitHub Actions创建的提交避免死循环
if: github.event.pusher.name != 'github-actions[bot]'
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Check if version bump is needed
id: check
run: |
# 获取当前提交的文件变更
CHANGED_FILES=$(git diff --name-only HEAD~1..HEAD 2>/dev/null || git diff --name-only $(git rev-list --max-parents=0 HEAD)..HEAD)
echo "Changed files:"
echo "$CHANGED_FILES"
# 检查是否只有无关文件(.md, docs/, .github/等)
SIGNIFICANT_CHANGES=false
while IFS= read -r file; do
# 跳过空行
[ -z "$file" ] && continue
# 检查是否是需要忽略的文件
if [[ ! "$file" =~ \.(md|txt)$ ]] &&
[[ ! "$file" =~ ^docs/ ]] &&
[[ ! "$file" =~ ^\.github/ ]] &&
[[ "$file" != "VERSION" ]] &&
[[ "$file" != ".gitignore" ]] &&
[[ "$file" != "LICENSE" ]]; then
echo "Found significant change in: $file"
SIGNIFICANT_CHANGES=true
break
fi
done <<< "$CHANGED_FILES"
if [ "$SIGNIFICANT_CHANGES" = true ]; then
echo "Significant changes detected, version bump needed"
echo "needs_bump=true" >> $GITHUB_OUTPUT
else
echo "No significant changes, skipping version bump"
echo "needs_bump=false" >> $GITHUB_OUTPUT
fi
- name: Get current version
if: steps.check.outputs.needs_bump == 'true'
id: get_version
run: |
# 获取最新的tag版本
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
echo "Latest tag: $LATEST_TAG"
# 从tag中提取版本号
VERSION=${LATEST_TAG#v}
echo "Current version: $VERSION"
echo "current_version=$VERSION" >> $GITHUB_OUTPUT
- name: Calculate next version
if: steps.check.outputs.needs_bump == 'true'
id: next_version
run: |
VERSION="${{ steps.get_version.outputs.current_version }}"
# 分割版本号
IFS='.' read -r -a version_parts <<< "$VERSION"
MAJOR="${version_parts[0]:-0}"
MINOR="${version_parts[1]:-0}"
PATCH="${version_parts[2]:-0}"
# 默认递增patch版本
NEW_PATCH=$((PATCH + 1))
NEW_VERSION="${MAJOR}.${MINOR}.${NEW_PATCH}"
echo "New version: $NEW_VERSION"
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
- name: Update VERSION file
if: steps.check.outputs.needs_bump == 'true'
run: |
echo "${{ steps.next_version.outputs.new_version }}" > VERSION
# 配置git
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# 提交VERSION文件
git add VERSION
git commit -m "chore: bump version to ${{ steps.next_version.outputs.new_version }}"
git push origin main

View File

@@ -1,101 +0,0 @@
name: Docker Build & Push
on:
push:
tags:
- 'v*'
workflow_dispatch:
env:
REGISTRY: docker.io
IMAGE_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/claude-relay-service
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}},priority=1000
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha,prefix=sha-,format=short
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
test:
needs: build
runs-on: ubuntu-latest
if: github.event_name != 'pull_request'
permissions:
contents: read
security-events: write
steps:
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: ${{ env.IMAGE_NAME }}:latest
format: 'sarif'
output: 'trivy-results.sarif'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v3
if: always()
with:
sarif_file: 'trivy-results.sarif'
update-description:
needs: build
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Update Docker Hub Description
uses: peter-evans/dockerhub-description@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
repository: ${{ secrets.DOCKERHUB_USERNAME }}/claude-relay-service
readme-filepath: ./README.md
short-description: "Claude Code API Relay Service - 多账户管理的Claude API中转服务"

View File

@@ -1,56 +0,0 @@
name: Create Release
on:
push:
tags:
- 'v*'
permissions:
contents: write
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install git-cliff
run: |
wget -q https://github.com/orhun/git-cliff/releases/download/v1.4.0/git-cliff-1.4.0-x86_64-unknown-linux-gnu.tar.gz
tar -xzf git-cliff-1.4.0-x86_64-unknown-linux-gnu.tar.gz
chmod +x git-cliff-1.4.0/git-cliff
sudo mv git-cliff-1.4.0/git-cliff /usr/local/bin/
- name: Generate changelog
id: changelog
run: |
CHANGELOG=$(git-cliff --config .github/cliff.toml --latest --strip header)
echo "content<<EOF" >> $GITHUB_OUTPUT
echo "$CHANGELOG" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Create Release
uses: softprops/action-gh-release@v1
with:
body: |
## 🐳 Docker 镜像
```bash
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/claude-relay-service:${{ github.ref_name }}
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/claude-relay-service:latest
```
## 📦 主要更新
${{ steps.changelog.outputs.content }}
## 📋 完整更新日志
查看 [所有版本](https://github.com/${{ github.repository }}/releases)
draft: false
prerelease: false
generate_release_notes: true