mirror of
https://github.com/MatrixSeven/file-transfer-go.git
synced 2026-05-17 09:37:28 +08:00
feat:修复github action
This commit is contained in:
187
Dockerfile
187
Dockerfile
@@ -1,144 +1,69 @@
|
||||
|
||||
name: 🐳 Build and Push Docker Image
|
||||
# ==============================================
|
||||
# AMD64 Dockerfile - 使用官方 Go 基础镜像
|
||||
# ==============================================
|
||||
|
||||
on:
|
||||
# 手动触发
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
version:
|
||||
description: '版本号 (例如: v1.0.5)'
|
||||
required: true
|
||||
default: 'v1.0.5'
|
||||
type: string
|
||||
push_to_hub:
|
||||
description: '推送到 Docker Hub'
|
||||
required: true
|
||||
default: true
|
||||
type: boolean
|
||||
build_multiarch:
|
||||
description: '构建多架构镜像 (amd64 + arm64)'
|
||||
required: true
|
||||
default: true
|
||||
type: boolean
|
||||
# 前端构建阶段
|
||||
FROM node:20-alpine AS frontend-builder
|
||||
|
||||
# 推送标签时触发
|
||||
push:
|
||||
tags:
|
||||
- 'v*.*.*'
|
||||
# 国内镜像源优化
|
||||
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
|
||||
npm config set registry https://registry.npmmirror.com
|
||||
|
||||
# PR 时构建测试(不推送)
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
WORKDIR /app/chuan-next
|
||||
|
||||
env:
|
||||
REGISTRY: docker.io
|
||||
IMAGE_NAME: matrixseven/file-transfer-go
|
||||
# 前端依赖和构建
|
||||
COPY chuan-next/package.json ./
|
||||
RUN npm install
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: 🏗️ Build & Push Docker Image
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
|
||||
steps:
|
||||
- name: 📥 Checkout code
|
||||
uses: actions/checkout@v4
|
||||
COPY chuan-next/ ./
|
||||
# 临时移除 API 目录进行 SSG 构建
|
||||
RUN if [ -d "src/app/api" ]; then mv src/app/api /tmp/api-backup; fi && \
|
||||
NEXT_EXPORT=true npm run build && \
|
||||
if [ -d "/tmp/api-backup" ]; then mv /tmp/api-backup src/app/api; fi
|
||||
|
||||
- name: 🚀 Set up QEMU
|
||||
uses: docker/setup-qemu-action@v3
|
||||
with:
|
||||
platforms: linux/amd64,linux/arm64
|
||||
|
||||
- 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=${{ inputs.version }},enable=${{ github.event_name == 'workflow_dispatch' }}
|
||||
# ==============================================
|
||||
|
||||
- name: 🔧 Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
with:
|
||||
platforms: linux/amd64,linux/arm64
|
||||
driver-opts: |
|
||||
network=host
|
||||
buildkitd-flags: |
|
||||
--allow-insecure-entitlement=network.host
|
||||
# Go 构建阶段
|
||||
FROM golang:1.21-alpine AS go-builder
|
||||
|
||||
- name: 🔑 Login to Docker Hub
|
||||
if: github.event_name != 'pull_request'
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ${{ env.REGISTRY }}
|
||||
username: ${{ secrets.DOCKER_HUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_HUB_TOKEN }}
|
||||
# 国内镜像源和代理优化
|
||||
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
|
||||
apk add --no-cache git ca-certificates tzdata
|
||||
|
||||
- name: 🐳 Build and push Docker image
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .
|
||||
file: ./Dockerfile
|
||||
platforms: ${{ (github.event_name == 'workflow_dispatch' && inputs.build_multiarch == true) || github.event_name == 'push' && 'linux/amd64,linux/arm64' || 'linux/amd64' }}
|
||||
push: ${{ github.event_name != 'pull_request' && (inputs.push_to_hub != false) }}
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
cache-from: type=gha
|
||||
cache-to: type=gha,mode=max
|
||||
build-args: |
|
||||
BUILDKIT_INLINE_CACHE=1
|
||||
provenance: false
|
||||
sbom: false
|
||||
ENV GOPROXY=https://goproxy.cn,direct
|
||||
ENV CGO_ENABLED=0
|
||||
ENV GOOS=linux
|
||||
ENV GOARCH=amd64
|
||||
|
||||
- name: 📊 Image digest
|
||||
if: github.event_name != 'pull_request'
|
||||
run: echo ${{ steps.build.outputs.digest }}
|
||||
|
||||
- name: 🎉 Build Summary
|
||||
if: always()
|
||||
run: |
|
||||
echo "## 🐳 Docker Build Summary" >> $GITHUB_STEP_SUMMARY
|
||||
echo "" >> $GITHUB_STEP_SUMMARY
|
||||
echo "### 📦 Image Details" >> $GITHUB_STEP_SUMMARY
|
||||
echo "- **Registry**: ${{ env.REGISTRY }}" >> $GITHUB_STEP_SUMMARY
|
||||
echo "- **Image**: ${{ env.IMAGE_NAME }}" >> $GITHUB_STEP_SUMMARY
|
||||
echo "- **Tags**: ${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY
|
||||
echo "- **Platforms**: ${{ (github.event_name == 'workflow_dispatch' && inputs.build_multiarch == true) || github.event_name == 'push' && 'linux/amd64,linux/arm64' || 'linux/amd64' }}" >> $GITHUB_STEP_SUMMARY
|
||||
echo "" >> $GITHUB_STEP_SUMMARY
|
||||
echo "### 🚀 Usage" >> $GITHUB_STEP_SUMMARY
|
||||
echo '```bash' >> $GITHUB_STEP_SUMMARY
|
||||
echo "docker run -d -p 8080:8080 ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ inputs.version || 'latest' }}" >> $GITHUB_STEP_SUMMARY
|
||||
echo '```' >> $GITHUB_STEP_SUMMARY
|
||||
WORKDIR /app
|
||||
|
||||
security-scan:
|
||||
name: 🔍 Security Scan
|
||||
runs-on: ubuntu-latest
|
||||
needs: build
|
||||
if: github.event_name != 'pull_request'
|
||||
|
||||
steps:
|
||||
- name: 📥 Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: 🔍 Run Trivy vulnerability scanner
|
||||
uses: aquasecurity/trivy-action@master
|
||||
with:
|
||||
image-ref: '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest'
|
||||
format: 'sarif'
|
||||
output: 'trivy-results.sarif'
|
||||
|
||||
- name: 📤 Upload Trivy scan results
|
||||
uses: github/codeql-action/upload-sarif@v3
|
||||
if: always()
|
||||
with:
|
||||
sarif_file: 'trivy-results.sarif'
|
||||
# Go 依赖
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
# 拷贝前端构建结果
|
||||
COPY --from=frontend-builder /app/chuan-next/out ./internal/web/frontend
|
||||
|
||||
# Go 源码
|
||||
COPY cmd/ ./cmd/
|
||||
COPY internal/ ./internal/
|
||||
|
||||
# 构建 Go 应用 - AMD64 架构
|
||||
RUN go build -ldflags='-w -s' -o server ./cmd
|
||||
|
||||
# ==============================================
|
||||
|
||||
FROM alpine:3.18
|
||||
|
||||
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
|
||||
apk add --no-cache ca-certificates tzdata && \
|
||||
adduser -D -s /bin/sh appuser
|
||||
|
||||
WORKDIR /app
|
||||
COPY --from=go-builder --chown=appuser:appuser /app/server ./
|
||||
USER appuser
|
||||
|
||||
EXPOSE 8080
|
||||
CMD ["./server"]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user