ci(gitea): update Docker build workflow

- Update registry to use GHCR (GitHub Container Registry)
- Simplify environment variables and remove unused ones
- Refactor branch handling and tag generation
- Remove Docker metadata action and simplify build process
- Add debug information output
- Update comments and structure for better readability
This commit is contained in:
Havoc420Mac 2025-04-05 12:56:54 +08:00
parent 483296f906
commit ea770f4ae8

View File

@ -12,8 +12,9 @@ on:
branches: ["main"] branches: ["main"]
env: env:
REGISTRY: 113.44.68.213:3000 REGISTRY: ghcr.io # 直接使用GHCR
IMAGE_NAME: ${{ gitea.repository }} IMAGE_NAME: test # 对应您要推送的镜像名称
GHCR_USER: havoc412 # 您的GitHub用户名
jobs: jobs:
build-and-push: build-and-push:
@ -23,13 +24,11 @@ jobs:
packages: write packages: write
steps: steps:
- name: 获取 Gitea 配置 - name: 打印调试信息
run: | run: |
echo ${{ env.REGISTRY }} echo "Registry: ${{ env.REGISTRY }}"
echo ${{ gitea.actor }} echo "Image: ${{ env.GHCR_USER }}/${{ env.IMAGE_NAME }}"
echo ${{ secrets.GHCR_USER }} echo "GitHub Actor: ${{ gitea.actor }}"
echo ${{ secrets.GHCR_TOKEN }}
echo ${{ env.IMGAE_NAME }}
- name: 检出代码 - name: 检出代码
uses: actions/checkout@v4 uses: actions/checkout@v4
@ -37,47 +36,45 @@ jobs:
- name: 获取分支名称 - name: 获取分支名称
id: branch_name id: branch_name
run: | run: |
# 从 GITHUB_REF 提取分支名
branch=${GITHUB_REF#refs/heads/} branch=${GITHUB_REF#refs/heads/}
echo "branch=$branch" >> $GITHUB_OUTPUT echo "branch=${branch}" >> $GITHUB_OUTPUT
# 如果是 main 分支,使用 prod 标签,否则使用 dev-分支名 # 主分支用latest其他分支用dev-分支名
if [ "$branch" = "main" ]; then if [ "${branch}" = "main" ]; then
echo "env_suffix=prod" >> $GITHUB_OUTPUT echo "tag=latest" >> $GITHUB_OUTPUT
else else
echo "env_suffix=dev-${branch}" >> $GITHUB_OUTPUT # 替换非法字符为-
safe_branch=$(echo "${branch}" | sed 's/[^a-zA-Z0-9]/-/g')
echo "tag=dev-${safe_branch}" >> $GITHUB_OUTPUT
fi fi
- name: 登录到 Github Container Registry - name: 登录到 GitHub Container Registry
uses: docker/login-action@v3 uses: docker/login-action@v3
with: with:
registry: ghcr.io registry: ${{ env.REGISTRY }}
username: ${{ secrets.GHCR_USER }} username: ${{ env.GHCR_USER }}
password: ${{ secrets.GHCR_TOKEN }} password: ${{ secrets.GHCR_TOKEN }} # 在Gitea仓库设置中添加此secret
- name: 提取 Docker 元数据 # - name: 构建并推送 Docker 镜像
id: meta # uses: docker/build-push-action@v5
uses: docker/metadata-action@v5 # with:
with: # context: .
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} # push: true
tags: | # tags: |
# main 分支使用标准标签 # ${{ env.REGISTRY }}/${{ env.GHCR_USER }}/${{ env.IMAGE_NAME }}:${{ steps.branch_name.outputs.tag }}
type=raw,value=latest,enable=${{ steps.branch_name.outputs.branch == 'main' }} # labels: |
type=raw,value=${{ steps.branch_name.outputs.env_suffix }} # org.opencontainers.image.source=${{ gitea.server_url }}/${{ gitea.repository }}
type=sha,format=short,prefix=${{ steps.branch_name.outputs.env_suffix }}- # build-args: |
type=ref,event=tag,prefix=${{ steps.branch_name.outputs.env_suffix }}- # BUILDKIT_INLINE_CACHE=1
- name: 构建并推送 Docker 镜像 # - name: 通过SSH部署到服务器
uses: docker/build-push-action@v5 # if: ${{ steps.branch_name.outputs.branch == 'main' }} # 仅main分支触发部署
with: # run: |
context: . # ssh -o StrictHostKeyChecking=no \
push: true # -i "${{ secrets.SSH_PRIVATE_KEY }}" \
tags: ${{ steps.meta.outputs.tags }} # ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << 'EOF'
labels: ${{ steps.meta.outputs.labels }} # # 拉取最新镜像
build-args: | # docker pull ${{ env.REGISTRY }}/${{ env.GHCR_USER }}/${{ env.IMAGE_NAME }}:${{ steps.branch_name.outputs.tag }}
BUILDKIT_INLINE_CACHE=0 # # 重启服务(根据实际情况修改)
# 添加清理缓存的命令 # docker-compose down || true
outputs: type=docker,cleancache=true # docker-compose up -d
# EOF
# - name: Trigger Portainer Webhook
# run: |
# curl -k -X POST ${{ secrets.BACKEND_WEBHOOK_URL }}