ci: add Gitea Actions workflow for Docker image build and push
Add .gitea/workflows/build-and-push.yml that builds and pushes the Docker image to git.hty1024.com/hty1024/ai-app-ops-tools on three triggers: push to main, push of a v*.*.* tag, and manual workflow_dispatch with an optional custom tag. Uses docker/metadata-action to derive sensible tags (branch name, semver, short sha, latest on default branch) and registry- backed Buildx cache to speed up repeated builds. README gains a CI/CD section covering trigger rules, one-time setup (runner registration + GITEA_TOKEN secret with write:package scope), and how to pull / consume the published image. Co-Authored-By: Claude Opus 4 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
name: Build and Push Docker Image
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
tags:
|
||||
- 'v*.*.*'
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
tag:
|
||||
description: '自定义镜像 tag(留空则只打默认 tag)'
|
||||
required: false
|
||||
default: ''
|
||||
|
||||
env:
|
||||
REGISTRY: git.hty1024.com
|
||||
IMAGE_NAME: hty1024/ai-app-ops-tools
|
||||
|
||||
jobs:
|
||||
build-and-push:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
|
||||
steps:
|
||||
- name: Checkout source
|
||||
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: Login to Gitea Container Registry
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ${{ env.REGISTRY }}
|
||||
# 使用触发用户作为镜像 owner;token 需在仓库 Secrets 里配置
|
||||
# 推荐做法:到 Gitea「用户设置 → 应用 → 生成 Access Token」
|
||||
# 勾选 write:package 权限,将值存为仓库 Secret 名为 GITEA_TOKEN
|
||||
username: ${{ gitea.actor }}
|
||||
password: ${{ secrets.GITEA_TOKEN }}
|
||||
|
||||
- name: Compute image tags & labels
|
||||
id: meta
|
||||
uses: docker/metadata-action@v5
|
||||
with:
|
||||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
||||
tags: |
|
||||
# push 到分支:使用分支名作为 tag(如 main)
|
||||
type=ref,event=branch
|
||||
# push 语义化版本 tag:v1.2.3 -> 1.2.3 与 1.2
|
||||
type=semver,pattern={{version}}
|
||||
type=semver,pattern={{major}}.{{minor}}
|
||||
# 每次构建附带短 sha:sha-abc1234
|
||||
type=sha,prefix=sha-,format=short
|
||||
# 默认分支自动打 latest
|
||||
type=raw,value=latest,enable={{is_default_branch}}
|
||||
# 手动触发若提供了 tag,附加这个 tag
|
||||
type=raw,value=${{ inputs.tag }},enable=${{ gitea.event_name == 'workflow_dispatch' && inputs.tag != '' }}
|
||||
|
||||
- name: Build and push image
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .
|
||||
file: ./Dockerfile
|
||||
push: true
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
# 使用 registry 作为构建缓存,多次构建可显著加速
|
||||
# 若你的 Gitea 版本不支持 cache manifest,可注释掉这两行
|
||||
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache
|
||||
cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache,mode=max
|
||||
|
||||
- name: Print built tags
|
||||
run: |
|
||||
echo "已推送以下 tag:"
|
||||
echo "${{ steps.meta.outputs.tags }}"
|
||||
@@ -274,6 +274,42 @@ intents:
|
||||
|
||||
每加一个意图,AI 都能立刻自然语言调用 —— **意图模板库就是你的运维知识图谱**。
|
||||
|
||||
## CI/CD(Gitea Actions)
|
||||
|
||||
项目自带 [`.gitea/workflows/build-and-push.yml`](.gitea/workflows/build-and-push.yml),会构建 Docker 镜像并推送到 `git.hty1024.com/hty1024/ai-app-ops-tools`。
|
||||
|
||||
### 触发方式
|
||||
|
||||
| 触发 | 产生的 tag |
|
||||
|---|---|
|
||||
| push 到 `main` | `main`、`sha-<短哈希>`、`latest` |
|
||||
| push 语义化版本 tag(如 `v1.2.3`) | `1.2.3`、`1.2`、`sha-<短哈希>` |
|
||||
| 在 Gitea Web 上点 **Run workflow**(workflow_dispatch) | 默认 tag + 可选自定义 tag |
|
||||
|
||||
### 准备工作(一次性)
|
||||
|
||||
1. **注册 Gitea Actions Runner**(如未注册):在仓库 → Settings → Actions → Runners 获取注册命令
|
||||
2. **创建 Access Token**:用户头像 → Settings → Applications → Generate Token,勾选 `write:package`
|
||||
3. **在仓库 Secrets 添加**:仓库 Settings → Actions → Secrets → 新增名为 `GITEA_TOKEN`,值为上一步生成的 token
|
||||
4. **打开镜像仓库**:Gitea 默认启用 Packages,无需额外操作;首次 push 后会自动在 `Packages` 里出现
|
||||
|
||||
### 拉取镜像示例
|
||||
|
||||
```bash
|
||||
# 在使用镜像的机器上登录
|
||||
docker login git.hty1024.com -u <your-user> -p <your-token>
|
||||
|
||||
# 拉取
|
||||
docker pull git.hty1024.com/hty1024/ai-app-ops-tools:latest
|
||||
|
||||
# 或在 docker-compose.yml 中替换 build: . 为:
|
||||
# image: git.hty1024.com/hty1024/ai-app-ops-tools:latest
|
||||
```
|
||||
|
||||
### 手动触发
|
||||
|
||||
在 Gitea Web 进入仓库 → **Actions** → 选择 `Build and Push Docker Image` → 点 **Run workflow**,可选填入自定义 tag。
|
||||
|
||||
## 安全模型
|
||||
|
||||
| 设计 | 防护目标 |
|
||||
|
||||
Reference in New Issue
Block a user