feat: 添加版本号显示和自动构建/删除 Docker 镜像功能
- 添加前端版本号显示(桌面端和移动端) - 实现 GitHub Actions 自动构建和推送 Docker 镜像 - 实现删除 release 时自动删除对应 Docker 镜像 - 支持版本号格式:v数字.数字.数字 或 v数字.数字.数字-后缀 - 添加版本号管理文档 - 更新 Dockerfile 和部署脚本支持版本号构建参数
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
name: Build and Push Docker Image
|
||||
|
||||
on:
|
||||
release:
|
||||
types:
|
||||
- published # 当通过 GitHub Releases 页面创建 release 时触发
|
||||
- deleted # 当删除 release 时触发
|
||||
|
||||
jobs:
|
||||
build-and-push:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
if: github.event.action == 'published'
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: ${{ github.event.release.tag_name }} # 使用 release 对应的 tag
|
||||
|
||||
# 构建和推送镜像(仅在发布 release 时)
|
||||
- name: Extract version from release
|
||||
if: github.event.action == 'published'
|
||||
id: extract_version
|
||||
run: |
|
||||
# 从 release tag 中提取版本号(例如 v1.0.0 -> 1.0.0)
|
||||
TAG_NAME="${{ github.event.release.tag_name }}"
|
||||
if [ -z "$TAG_NAME" ]; then
|
||||
TAG_NAME=${GITHUB_REF#refs/tags/}
|
||||
fi
|
||||
|
||||
# 验证版本号格式:v数字.数字.数字[-后缀](例如 v1.0.0, v2.10.102, v1.0.0-beta)
|
||||
if [[ ! "$TAG_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.-]+)?$ ]]; then
|
||||
echo "错误: 版本号格式不正确,应为 v数字.数字.数字 或 v数字.数字.数字-后缀 (例如: v1.0.0, v1.0.0-beta)"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
VERSION=${TAG_NAME#v} # 移除 v 前缀
|
||||
echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
|
||||
echo "TAG=$TAG_NAME" >> $GITHUB_OUTPUT
|
||||
echo "Extracted version: $VERSION"
|
||||
echo "Full tag: $TAG_NAME"
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
if: github.event.action == 'published'
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Log in to Docker Hub
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
|
||||
- name: Build and push Docker image
|
||||
if: github.event.action == 'published'
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .
|
||||
file: ./Dockerfile
|
||||
push: true
|
||||
tags: |
|
||||
wrbug/polyhermes:${{ steps.extract_version.outputs.TAG }}
|
||||
wrbug/polyhermes:latest
|
||||
build-args: |
|
||||
VERSION=${{ steps.extract_version.outputs.VERSION }}
|
||||
GIT_TAG=${{ steps.extract_version.outputs.TAG }}
|
||||
GITHUB_REPO_URL=https://github.com/WrBug/PolyHermes
|
||||
cache-from: type=registry,ref=wrbug/polyhermes:latest
|
||||
cache-to: type=inline
|
||||
|
||||
# 删除镜像(仅在删除 release 时)
|
||||
- name: Delete Docker image (when release is deleted)
|
||||
if: github.event.action == 'deleted'
|
||||
run: |
|
||||
TAG_NAME="${{ github.event.release.tag_name }}"
|
||||
|
||||
# 验证版本号格式:v数字.数字.数字[-后缀](精准匹配)
|
||||
if [[ ! "$TAG_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.-]+)?$ ]]; then
|
||||
echo "警告: 版本号格式不正确 ($TAG_NAME),应为 v数字.数字.数字 或 v数字.数字.数字-后缀 (例如: v1.0.0, v1.0.0-beta)"
|
||||
echo "跳过删除操作"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
IMAGE_NAME="wrbug/polyhermes"
|
||||
DOCKER_USERNAME="${{ secrets.DOCKER_USERNAME }}"
|
||||
DOCKER_PASSWORD="${{ secrets.DOCKER_PASSWORD }}"
|
||||
|
||||
echo "正在删除 Docker 镜像: ${IMAGE_NAME}:${TAG_NAME}"
|
||||
|
||||
# 使用 Docker Hub API v2 删除镜像标签
|
||||
RESPONSE=$(curl -s -w "\n%{http_code}" -X DELETE \
|
||||
-u "${DOCKER_USERNAME}:${DOCKER_PASSWORD}" \
|
||||
"https://hub.docker.com/v2/repositories/${IMAGE_NAME}/tags/${TAG_NAME}/")
|
||||
|
||||
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
|
||||
BODY=$(echo "$RESPONSE" | head -n-1)
|
||||
|
||||
if [ "$HTTP_CODE" = "204" ]; then
|
||||
echo "✅ 成功删除 Docker 镜像标签: ${IMAGE_NAME}:${TAG_NAME}"
|
||||
elif [ "$HTTP_CODE" = "404" ]; then
|
||||
echo "⚠️ 镜像标签不存在: ${IMAGE_NAME}:${TAG_NAME},可能已被删除或不存在"
|
||||
else
|
||||
echo "❌ 删除失败 (HTTP $HTTP_CODE): $BODY"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user