feat: refactor Docker build workflow to extract metadata and streamline image builds

This commit is contained in:
krau
2025-12-18 19:03:29 +08:00
parent ca0fd67fba
commit 1a7747c2d2

View File

@@ -9,13 +9,24 @@ env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
concurrency:
group: docker-build-${{ github.repository }}
cancel-in-progress: true
jobs:
build-and-push:
prepare:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
version: ${{ steps.meta.outputs.version }}
git_commit: ${{ steps.args.outputs.git_commit }}
build_time: ${{ steps.args.outputs.build_time }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
@@ -31,6 +42,23 @@ jobs:
type=sha
type=raw,value=latest
- name: Extract Dockerfile args
id: args
run: |
echo "git_commit=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
echo "build_time=$(git show -s --format=%cI)" >> "$GITHUB_OUTPUT"
build-default:
runs-on: ubuntu-latest
needs: prepare
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
@@ -44,12 +72,6 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Dockerfile args
id: args
run: |
echo "git_commit=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
echo "build_time=$(git show -s --format=%cI)" >> "$GITHUB_OUTPUT"
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v6
@@ -58,16 +80,40 @@ jobs:
file: ./Dockerfile
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
tags: ${{ needs.prepare.outputs.tags }}
labels: ${{ needs.prepare.outputs.labels }}
cache-from: |
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
type=gha
cache-to: type=gha,mode=max
build-args: |
VERSION=${{ steps.meta.outputs.version }}
GitCommit=${{ steps.args.outputs.git_commit }}
BuildTime=${{ steps.args.outputs.build_time }}
VERSION=${{ needs.prepare.outputs.version }}
GitCommit=${{ needs.prepare.outputs.git_commit }}
BuildTime=${{ needs.prepare.outputs.build_time }}
build-micro:
runs-on: ubuntu-latest
needs: prepare
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
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 GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push micro Docker image
id: build-and-push-micro
@@ -80,16 +126,40 @@ jobs:
tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:micro
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:micro-latest
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:micro-${{ steps.meta.outputs.version }}
labels: ${{ steps.meta.outputs.labels }}
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:micro-${{ needs.prepare.outputs.version }}
labels: ${{ needs.prepare.outputs.labels }}
cache-from: |
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:micro-latest
type=gha
cache-to: type=gha,mode=max
build-args: |
VERSION=${{ steps.meta.outputs.version }}
GitCommit=${{ steps.args.outputs.git_commit }}
BuildTime=${{ steps.args.outputs.build_time }}
VERSION=${{ needs.prepare.outputs.version }}
GitCommit=${{ needs.prepare.outputs.git_commit }}
BuildTime=${{ needs.prepare.outputs.build_time }}
build-pico:
runs-on: ubuntu-latest
needs: prepare
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
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 GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push pico Docker image
id: build-and-push-pico
@@ -102,13 +172,13 @@ jobs:
tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:pico
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:pico-latest
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:pico-${{ steps.meta.outputs.version }}
labels: ${{ steps.meta.outputs.labels }}
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:pico-${{ needs.prepare.outputs.version }}
labels: ${{ needs.prepare.outputs.labels }}
cache-from: |
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:pico-latest
type=gha
cache-to: type=gha,mode=max
build-args: |
VERSION=${{ steps.meta.outputs.version }}
GitCommit=${{ steps.args.outputs.git_commit }}
BuildTime=${{ steps.args.outputs.build_time }}
VERSION=${{ needs.prepare.outputs.version }}
GitCommit=${{ needs.prepare.outputs.git_commit }}
BuildTime=${{ needs.prepare.outputs.build_time }}