130 lines
3.9 KiB
YAML
130 lines
3.9 KiB
YAML
name: Build and Publish Docker Image
|
|
|
|
on:
|
|
push:
|
|
tags:
|
|
- "v*"
|
|
|
|
env:
|
|
REGISTRY: ghcr.io
|
|
IMAGE_NAME: krau/saveany-bot
|
|
|
|
concurrency:
|
|
group: docker-build-${{ github.repository }}
|
|
cancel-in-progress: true
|
|
|
|
jobs:
|
|
prepare:
|
|
runs-on: ubuntu-latest
|
|
permissions:
|
|
contents: read
|
|
outputs:
|
|
version: ${{ steps.vars.outputs.version }}
|
|
major_minor: ${{ steps.vars.outputs.major_minor }}
|
|
short_sha: ${{ steps.vars.outputs.short_sha }}
|
|
build_time: ${{ steps.vars.outputs.build_time }}
|
|
steps:
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v6
|
|
|
|
- name: Extract Version Components
|
|
id: vars
|
|
run: |
|
|
VERSION=${GITHUB_REF#refs/tags/v}
|
|
|
|
MAJOR_MINOR=$(echo "$VERSION" | cut -d. -f1,2)
|
|
|
|
SHORT_SHA=$(git rev-parse --short HEAD)
|
|
|
|
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
|
|
echo "major_minor=$MAJOR_MINOR" >> "$GITHUB_OUTPUT"
|
|
echo "short_sha=$SHORT_SHA" >> "$GITHUB_OUTPUT"
|
|
echo "build_time=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> "$GITHUB_OUTPUT"
|
|
|
|
build:
|
|
needs: prepare
|
|
permissions:
|
|
contents: read
|
|
packages: write
|
|
strategy:
|
|
matrix:
|
|
arch: [amd64, arm64]
|
|
type: [default, micro, pico]
|
|
fail-fast: false
|
|
runs-on: ${{ matrix.arch == 'amd64' && 'ubuntu-latest' || 'ubuntu-24.04-arm' }}
|
|
|
|
steps:
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
- name: Login to GitHub Container Registry
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ env.REGISTRY }}
|
|
username: ${{ github.actor }}
|
|
password: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
- name: Build and push (Temporary Tags)
|
|
uses: docker/build-push-action@v6
|
|
with:
|
|
context: .
|
|
# 根据类型选择 Dockerfile
|
|
file: ${{ matrix.type == 'default' && './Dockerfile' || format('./Dockerfile.{0}', matrix.type) }}
|
|
# 仅构建当前架构
|
|
platforms: ${{ matrix.arch == 'amd64' && 'linux/amd64' || 'linux/arm64' }}
|
|
push: true
|
|
# 推送带有架构后缀的临时标签,供后续合并使用
|
|
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:tmp-${{ matrix.type }}-${{ matrix.arch }}
|
|
build-args: |
|
|
VERSION=${{ needs.prepare.outputs.version }}
|
|
GitCommit=${{ needs.prepare.outputs.short_sha }}
|
|
BuildTime=${{ needs.prepare.outputs.build_time }}
|
|
cache-from: type=gha
|
|
cache-to: type=gha,mode=max
|
|
|
|
create-manifest:
|
|
needs: [prepare, build]
|
|
runs-on: ubuntu-latest
|
|
permissions:
|
|
packages: write
|
|
strategy:
|
|
matrix:
|
|
type: [default, micro, pico]
|
|
steps:
|
|
- name: Login to GitHub Container Registry
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ env.REGISTRY }}
|
|
username: ${{ github.actor }}
|
|
password: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
- name: Create and push manifest lists
|
|
run: |
|
|
REPO="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}"
|
|
VERSION="${{ needs.prepare.outputs.version }}"
|
|
MAJOR_MINOR="${{ needs.prepare.outputs.major_minor }}"
|
|
SHA="${{ needs.prepare.outputs.short_sha }}"
|
|
TYPE="${{ matrix.type }}"
|
|
TAGS=()
|
|
|
|
if [ "$TYPE" == "default" ]; then
|
|
TAGS+=("$REPO:latest")
|
|
TAGS+=("$REPO:$VERSION")
|
|
TAGS+=("$REPO:$MAJOR_MINOR")
|
|
TAGS+=("$REPO:sha-$SHA")
|
|
else
|
|
TAGS+=("$REPO:$TYPE")
|
|
TAGS+=("$REPO:$TYPE-latest")
|
|
TAGS+=("$REPO:$TYPE-$VERSION")
|
|
fi
|
|
|
|
SRC_AMD64="$REPO:tmp-$TYPE-amd64"
|
|
SRC_ARM64="$REPO:tmp-$TYPE-arm64"
|
|
|
|
for TAG in "${TAGS[@]}"; do
|
|
docker buildx imagetools create -t "$TAG" "$SRC_AMD64" "$SRC_ARM64"
|
|
done
|