69 lines
1.9 KiB
YAML
69 lines
1.9 KiB
YAML
name: Build and Push Docker
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- "**" # 匹配所有分支
|
|
pull_request:
|
|
branches: ["main"]
|
|
|
|
env:
|
|
REGISTRY: gitea.ideaopen.cn
|
|
IMAGE_NAME: ${{ gitea.repository }}
|
|
|
|
jobs:
|
|
build-and-push:
|
|
runs-on: color
|
|
permissions:
|
|
contents: read
|
|
packages: write
|
|
|
|
steps:
|
|
- name: 检出代码
|
|
uses: actions/checkout@v4
|
|
|
|
- name: 获取分支名称
|
|
id: branch_name
|
|
run: |
|
|
branch=${GITHUB_REF#refs/heads/}
|
|
echo "branch=$branch" >> $GITHUB_OUTPUT
|
|
if [ "$branch" = "main" ]; then
|
|
echo "env_suffix=prod" >> $GITHUB_OUTPUT
|
|
else
|
|
echo "env_suffix=dev-${branch}" >> $GITHUB_OUTPUT
|
|
fi
|
|
|
|
- name: 登录到 Gitea Container Registry
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ env.REGISTRY }}
|
|
username: ${{ gitea.actor }}
|
|
password: ${{ secrets.DOCKER_TOKEN}}
|
|
|
|
- name: 提取 Docker 元数据
|
|
id: meta
|
|
uses: docker/metadata-action@v5
|
|
with:
|
|
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
|
tags: |
|
|
type=raw,value=latest,enable=${{ steps.branch_name.outputs.branch == 'main' }}
|
|
type=raw,value=${{ steps.branch_name.outputs.env_suffix }}
|
|
type=sha,format=short,prefix=${{ steps.branch_name.outputs.env_suffix }}-
|
|
type=ref,event=tag,prefix=${{ steps.branch_name.outputs.env_suffix }}-
|
|
|
|
- name: 设置 Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
- name: 构建并推送 Docker 镜像
|
|
uses: docker/build-push-action@v5
|
|
with:
|
|
context: .
|
|
push: true
|
|
tags: ${{ steps.meta.outputs.tags }}
|
|
labels: ${{ steps.meta.outputs.labels }}
|
|
build-args: |
|
|
BUILDKIT_INLINE_CACHE=0
|
|
# 启用 BuildKit 缓存
|
|
cache-from: type=gha
|
|
cache-to: type=gha,mode=max
|