feat: 添加 Web 项目发布构建的 GitHub Action,包括版本发布、Docker 镜像构建和推送,支持灵活的输入参数和环境变量配置,优化 CI/CD 流程。

This commit is contained in:
Lyda
2025-08-20 17:12:59 +08:00
parent d13a6b9f38
commit 3144b5c203
8 changed files with 1130 additions and 0 deletions

View File

@@ -0,0 +1,151 @@
name: "多环境发布"
on:
workflow_dispatch:
inputs:
environment:
description: "部署环境"
required: true
default: "staging"
type: choice
options:
- staging
- production
- testing
version_type:
description: "版本类型"
required: true
default: "patch"
type: choice
options:
- patch
- minor
- major
custom_tags:
description: "自定义Docker标签每行一个"
required: false
default: ""
jobs:
release:
runs-on: ubuntu-latest
name: "发布到 ${{ github.event.inputs.environment }} 环境"
strategy:
matrix:
include:
- environment: staging
dockerfile: "./container/dev/Dockerfile"
app_env: "staging"
registry: "docker-registry.bjxgj.com"
namespace: "staging"
- environment: production
dockerfile: "./container/prod/Dockerfile"
app_env: "production"
registry: "docker-registry.bjxgj.com"
namespace: "production"
- environment: testing
dockerfile: "./container/test/Dockerfile"
app_env: "testing"
registry: "docker-registry.bjxgj.com"
namespace: "testing"
steps:
- name: 检出代码
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITEA_TOKEN }}
- name: 设置构建环境
uses: actions/xgj/setup-env@main
with:
docker-registry: ${{ matrix.registry }}
docker-password: ${{ secrets.DOCKER_PASSWORD }}
kube-config: ${{ secrets.KUBE_CONFIG }}
- name: 安装依赖
uses: actions/xgj/npm-install@main
with:
package-manager: "pnpm"
- name: 准备环境特定的标签
id: env_tags
run: |
ENVIRONMENT="${{ github.event.inputs.environment }}"
VERSION_TYPE="${{ github.event.inputs.version_type }}"
CUSTOM_TAGS="${{ github.event.inputs.custom_tags }}"
# 基础标签
TAGS="${ENVIRONMENT}"
TAGS="${TAGS}\n${ENVIRONMENT}-${VERSION_TYPE}"
TAGS="${TAGS}\n$(date +%Y%m%d)-${ENVIRONMENT}"
# 添加自定义标签
if [[ -n "$CUSTOM_TAGS" ]]; then
while IFS= read -r tag; do
if [[ -n "$tag" ]]; then
TAGS="${TAGS}\n${tag}-${ENVIRONMENT}"
fi
done <<< "$CUSTOM_TAGS"
fi
{
echo "tags<<EOF"
echo -e "$TAGS"
echo "EOF"
} >> $GITHUB_OUTPUT
echo "🏷️ 环境特定标签:"
echo -e "$TAGS" | sed 's/^/ - /'
- name: 发布构建
id: release
uses: actions/xgj/release-web@main
with:
gitea-token: ${{ secrets.GITEA_TOKEN }}
app-env: ${{ matrix.app_env }}
sentry-auth-token: ${{ secrets.SENTRY_AUTH_TOKEN }}
sentry-dsn: ${{ vars.SENTRY_DSN }}
enable-sentry: ${{ matrix.environment == 'production' && 'true' || 'false' }}
docker-registry: ${{ matrix.registry }}
dockerfile-path: ${{ matrix.dockerfile }}
release-command: "npm run release -- --release -V --increment ${{ github.event.inputs.version_type }}"
docker-tags: ${{ steps.env_tags.outputs.tags }}
- name: 部署到Kubernetes
run: |
ENVIRONMENT="${{ github.event.inputs.environment }}"
VERSION="${{ steps.release.outputs.version }}"
NAMESPACE="${{ matrix.namespace }}"
IMAGE="${{ matrix.registry }}/${{ github.event.repository.name }}:${VERSION}"
echo "🚀 部署到 ${ENVIRONMENT} 环境..."
echo "📦 版本: ${VERSION}"
echo "🐳 镜像: ${IMAGE}"
echo "📍 命名空间: ${NAMESPACE}"
kubectl set image deployment/web-app \
web-app=${IMAGE} \
-n ${NAMESPACE}
kubectl rollout status deployment/web-app -n ${NAMESPACE}
echo "✅ 部署完成!"
- name: 发布总结
run: |
echo "🎉 多环境发布完成!"
echo ""
echo "📋 发布信息:"
echo " - 环境: ${{ github.event.inputs.environment }}"
echo " - 版本: ${{ steps.release.outputs.version }}"
echo " - 版本类型: ${{ github.event.inputs.version_type }}"
echo " - Docker仓库: ${{ matrix.registry }}"
echo " - Kubernetes命名空间: ${{ matrix.namespace }}"
echo ""
echo "🏷️ 主要标签:"
echo " - latest默认"
echo " - prod默认"
echo " - ${{ steps.release.outputs.version }}(版本标签)"
echo " - ${{ github.event.inputs.environment }}(环境标签)"