mirror of
https://github.com/MatrixSeven/file-transfer-go.git
synced 2026-02-12 16:14:42 +08:00
140 lines
4.2 KiB
YAML
140 lines
4.2 KiB
YAML
|
|
name: 🐳 Build and Push Docker Image (AMD64)
|
|
|
|
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
|
|
|
|
# 推送标签时触发
|
|
push:
|
|
tags:
|
|
- 'v*.*.*'
|
|
|
|
# PR 时构建测试(不推送)
|
|
pull_request:
|
|
branches: [ main ]
|
|
|
|
env:
|
|
REGISTRY: docker.io
|
|
IMAGE_NAME: matrixseven/file-transfer-go
|
|
|
|
jobs:
|
|
build:
|
|
name: 🏗️ Build & Push Docker Image
|
|
runs-on: ubuntu-latest
|
|
|
|
permissions:
|
|
contents: read
|
|
packages: write
|
|
|
|
steps:
|
|
- name: 📥 Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
|
|
- 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:
|
|
driver-opts: |
|
|
network=host
|
|
buildkitd-flags: |
|
|
--allow-insecure-entitlement=network.host
|
|
|
|
- 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 }}
|
|
|
|
- name: 🏗️ Set build platform
|
|
id: platforms
|
|
run: |
|
|
echo "platforms=linux/amd64" >> $GITHUB_OUTPUT
|
|
|
|
- name: 🐳 Build and push Docker image
|
|
uses: docker/build-push-action@v5
|
|
with:
|
|
context: .
|
|
file: ./Dockerfile
|
|
platforms: ${{ steps.platforms.outputs.platforms }}
|
|
push: ${{ github.event_name != 'pull_request' && (github.event_name != 'workflow_dispatch' || inputs.push_to_hub == true) }}
|
|
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
|
|
|
|
- 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**: ${{ steps.platforms.outputs.platforms }}" >> $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
|
|
|
|
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'
|
|
|