feat: github action
Some checks failed
Build and Push Docker Image to Aliyun Registry / build-and-push (push) Has been cancelled

Signed-off-by: Caijinglong <cjl_spy@163.com>
This commit is contained in:
Caijinglong 2025-06-25 18:04:34 +08:00
parent e453c63620
commit 5bb683fa82
Signed by: cai
GPG Key ID: 92FBD82121AC80AA
4 changed files with 202 additions and 2 deletions

View File

@ -2,3 +2,4 @@
- 创建了nginx+php-fpm通用Docker镜像提供Alpine和Debian两个版本。Alpine版本安全性更高(0漏洞 vs 152漏洞),推荐生产环境使用。包含完整的配置文件、安全扫描报告和使用文档。
- 更新Dockerfile添加了完整的PHP扩展集合包括bcmath、bz2、dom、exif、fileinfo、ftp、gd、gettext、iconv、mbstring、mysqli、opcache、pcntl、pdo系列、pgsql、posix、shmop、simplexml、soap、sockets、sqlite3、sysvsem、sysvshm、xml系列、xsl、zip、ldap、redis等扩展满足用户的全面需求
- 为common-nginx-fpm项目创建了GitHub Action工作流支持自动构建Docker镜像并推送到阿里云镜像仓库使用GitHub Secrets管理认证信息支持多种触发条件和自动标签管理

View File

@ -1,6 +1,6 @@
{
"project_path": "/Users/cai/dockers/common-nginx-fpm",
"last_organized": "2025-06-25T08:51:05.280441Z",
"total_entries": 2,
"last_organized": "2025-06-25T10:01:54.806905Z",
"total_entries": 3,
"version": "1.0.0"
}

75
.github/workflows/docker-build-push.yml vendored Normal file
View File

@ -0,0 +1,75 @@
name: Build and Push Docker Image to Aliyun Registry
on:
push:
branches:
- main
tags:
- 'v*'
pull_request:
branches:
- main
workflow_dispatch:
env:
REGISTRY: ${{ secrets.ALIYUN_REGISTRY }}
IMAGE_NAME: ${{ secrets.IMAGE_NAME || github.repository }}
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Aliyun Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.ALIYUN_USERNAME }}
password: ${{ secrets.ALIYUN_PASSWORD }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=raw,value=latest,enable={{is_default_branch}}
type=raw,value=alpine,enable={{is_default_branch}}
labels: |
org.opencontainers.image.title=Common Nginx PHP-FPM
org.opencontainers.image.description=A common Nginx + PHP-FPM Docker image with Alpine Linux
org.opencontainers.image.vendor=Common Docker Images
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Image digest
run: echo ${{ steps.build.outputs.digest }}
- name: Update deployment status
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
run: |
echo "✅ Docker image built and pushed successfully!"
echo "🏷️ Tags: ${{ steps.meta.outputs.tags }}"
echo "📦 Registry: ${{ env.REGISTRY }}"
echo "🖼️ Image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}"

124
GITHUB_ACTION_SETUP.md Normal file
View File

@ -0,0 +1,124 @@
# GitHub Action 配置说明
## 概述
已为项目创建了 GitHub Action 工作流,用于自动构建 Docker 镜像并推送到阿里云容器镜像仓库。
## 配置文件
- **工作流文件**: `.github/workflows/docker-build-push.yml`
- **触发条件**:
- 推送到 `main` 分支
- 创建 tag`v` 开头)
- Pull Request 到 `main` 分支
- 手动触发
## 必需的 GitHub Secrets
在 GitHub 仓库的 Settings > Secrets and variables > Actions 中添加以下 Secrets
### 必需配置
- `ALIYUN_REGISTRY` - 阿里云镜像仓库地址
- 示例: `registry.cn-hangzhou.aliyuncs.com`
- `ALIYUN_USERNAME` - 阿里云镜像仓库用户名
- `ALIYUN_PASSWORD` - 阿里云镜像仓库密码
### 可选配置
- `IMAGE_NAME` - 镜像名称(可选)
- 如果不设置,将使用 GitHub 仓库名称
- 示例: `your-namespace/common-nginx-fpm`
## 镜像标签策略
工作流会自动生成以下标签:
### 分支推送
- `main``latest`, `alpine`
- 其他分支 → `branch-name`
### Tag 推送
- `v1.2.3``1.2.3`, `1.2`, `1`, `latest`, `alpine`
- `v2.0.0-beta.1``2.0.0-beta.1`
### Pull Request
- PR #123`pr-123`
## 功能特性
- ✅ 单架构构建 (linux/amd64)
- ✅ 构建缓存优化
- ✅ 自动标签管理
- ✅ 元数据标签
- ✅ 构建状态输出
- ✅ 支持手动触发
## 使用示例
### 1. 配置 Secrets
```bash
# 在 GitHub 仓库设置中添加以下 Secrets:
ALIYUN_REGISTRY=registry.cn-hangzhou.aliyuncs.com
ALIYUN_USERNAME=your-username
ALIYUN_PASSWORD=your-password
IMAGE_NAME=your-namespace/common-nginx-fpm # 可选
```
### 2. 触发构建
```bash
# 方式1: 推送到 main 分支
git push origin main
# 方式2: 创建版本标签
git tag v1.0.0
git push origin v1.0.0
# 方式3: 在 GitHub 网页上手动触发
# Actions > Build and Push Docker Image to Aliyun Registry > Run workflow
```
### 3. 使用构建的镜像
```bash
# 拉取最新版本
docker pull registry.cn-hangzhou.aliyuncs.com/your-namespace/common-nginx-fpm:latest
# 拉取特定版本
docker pull registry.cn-hangzhou.aliyuncs.com/your-namespace/common-nginx-fpm:v1.0.0
# 运行容器
docker run -d -p 80:80 registry.cn-hangzhou.aliyuncs.com/your-namespace/common-nginx-fpm:latest
```
## 故障排除
### 1. 认证失败
- 检查 `ALIYUN_USERNAME``ALIYUN_PASSWORD` 是否正确
- 确认阿里云账号有推送权限
### 2. 镜像仓库不存在
- 在阿里云控制台创建对应的镜像仓库
- 确认 `ALIYUN_REGISTRY``IMAGE_NAME` 配置正确
### 3. 构建失败
- 查看 GitHub Actions 日志
- 检查 Dockerfile 语法
- 确认依赖包可用性
## 安全建议
1. **使用专用账号**: 为 CI/CD 创建专用的阿里云子账号
2. **最小权限**: 只授予必要的镜像推送权限
3. **定期轮换**: 定期更新密码和访问密钥
4. **监控日志**: 定期检查构建和推送日志
## 扩展功能
如需添加更多功能,可以考虑:
- 多架构构建 (linux/amd64, linux/arm64)
- 安全扫描集成
- 构建通知 (Slack, 钉钉等)
- 自动部署到测试环境
- 镜像签名验证