mirror of
https://github.com/yusing/godoxy.git
synced 2026-02-18 08:27:43 +01:00
186 lines
5.9 KiB
YAML
186 lines
5.9 KiB
YAML
name: Docker Image CI
|
|
|
|
on:
|
|
workflow_call:
|
|
inputs:
|
|
tag:
|
|
required: true
|
|
type: string
|
|
image_name:
|
|
required: true
|
|
type: string
|
|
target:
|
|
required: true
|
|
type: string
|
|
dockerfile:
|
|
required: false
|
|
type: string
|
|
default: Dockerfile
|
|
|
|
env:
|
|
REGISTRY: ghcr.io
|
|
MAKE_ARGS: ${{ inputs.target }}=1
|
|
DIGEST_PATH: /tmp/digests/${{ inputs.target }}
|
|
DIGEST_NAME_SUFFIX: ${{ inputs.target }}
|
|
DOCKERFILE: ${{ inputs.dockerfile }}
|
|
|
|
jobs:
|
|
build:
|
|
strategy:
|
|
fail-fast: false
|
|
matrix:
|
|
include:
|
|
- runner: ubuntu-latest
|
|
platform: linux/amd64
|
|
- runner: ubuntu-24.04-arm
|
|
platform: linux/arm64
|
|
|
|
name: Build ${{ matrix.platform }}
|
|
runs-on: ${{ matrix.runner }}
|
|
|
|
permissions:
|
|
contents: read
|
|
packages: write
|
|
id-token: write
|
|
attestations: write
|
|
|
|
steps:
|
|
- name: Checkout (for tag resolution)
|
|
uses: actions/checkout@v4
|
|
with:
|
|
fetch-depth: 0
|
|
|
|
- name: Prepare
|
|
run: |
|
|
platform=${{ matrix.platform }}
|
|
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
|
|
|
|
- name: Compute VERSION for build
|
|
run: |
|
|
if [ "${GITHUB_REF_TYPE}" = "tag" ]; then
|
|
version="${GITHUB_REF_NAME}"
|
|
cache_variant="release"
|
|
elif [ "${GITHUB_REF_NAME}" = "main" ] || [ "${GITHUB_REF_NAME}" = "compat" ]; then
|
|
git fetch --tags origin main
|
|
version="$(git describe --tags --abbrev=0 origin/main 2>/dev/null || git describe --tags --abbrev=0 main 2>/dev/null || echo v0.0.0)"
|
|
cache_variant="${GITHUB_REF_NAME}"
|
|
else
|
|
version="v$(date -u +'%Y%m%d-%H%M')"
|
|
cache_variant="nightly"
|
|
fi
|
|
echo "VERSION_FOR_BUILD=$version" >> $GITHUB_ENV
|
|
echo "CACHE_VARIANT=$cache_variant" >> $GITHUB_ENV
|
|
if [ "${GITHUB_REF_TYPE}" = "branch" ]; then
|
|
echo "BRANCH_FOR_BUILD=${GITHUB_REF_NAME}" >> $GITHUB_ENV
|
|
else
|
|
echo "BRANCH_FOR_BUILD=" >> $GITHUB_ENV
|
|
fi
|
|
|
|
- name: Docker meta
|
|
id: meta
|
|
uses: docker/metadata-action@v5
|
|
with:
|
|
images: ${{ env.REGISTRY }}/${{ inputs.image_name }}
|
|
tags: |
|
|
type=raw,value=${{ inputs.tag }},event=branch
|
|
type=ref,event=tag
|
|
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
with:
|
|
platforms: ${{ matrix.platform }}
|
|
|
|
- name: Login to registry
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ env.REGISTRY }}
|
|
username: ${{ github.actor }}
|
|
password: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
- name: Build and push by digest
|
|
id: build
|
|
uses: docker/build-push-action@v6
|
|
with:
|
|
platforms: ${{ matrix.platform }}
|
|
labels: ${{ steps.meta.outputs.labels }}
|
|
file: ${{ env.DOCKERFILE }}
|
|
outputs: type=image,name=${{ env.REGISTRY }}/${{ inputs.image_name }},push-by-digest=true,name-canonical=true,push=true
|
|
cache-from: |
|
|
type=gha,scope=${{ github.workflow }}-${{ env.CACHE_VARIANT }}-${{ env.PLATFORM_PAIR }}
|
|
type=registry,ref=${{ env.REGISTRY }}/${{ inputs.image_name }}:buildcache-${{ env.CACHE_VARIANT }}-${{ env.PLATFORM_PAIR }}
|
|
cache-to: |
|
|
type=gha,scope=${{ github.workflow }}-${{ env.CACHE_VARIANT }}-${{ env.PLATFORM_PAIR }},mode=max
|
|
type=registry,ref=${{ env.REGISTRY }}/${{ inputs.image_name }}:buildcache-${{ env.CACHE_VARIANT }}-${{ env.PLATFORM_PAIR }},mode=max
|
|
build-args: |
|
|
VERSION=${{ env.VERSION_FOR_BUILD }}
|
|
MAKE_ARGS=${{ env.MAKE_ARGS }}
|
|
BRANCH=${{ env.BRANCH_FOR_BUILD }}
|
|
|
|
- name: Generate artifact attestation
|
|
uses: actions/attest-build-provenance@v1
|
|
with:
|
|
subject-name: ${{ env.REGISTRY }}/${{ inputs.image_name }}
|
|
subject-digest: ${{ steps.build.outputs.digest }}
|
|
push-to-registry: true
|
|
|
|
- name: Export digest
|
|
run: |
|
|
mkdir -p ${{ env.DIGEST_PATH }}
|
|
digest="${{ steps.build.outputs.digest }}"
|
|
touch "${{ env.DIGEST_PATH }}/${digest#sha256:}"
|
|
|
|
- name: Upload digest
|
|
uses: actions/upload-artifact@v4
|
|
with:
|
|
name: digests-${{ env.PLATFORM_PAIR }}-${{ env.DIGEST_NAME_SUFFIX }}
|
|
path: ${{ env.DIGEST_PATH }}/*
|
|
if-no-files-found: error
|
|
retention-days: 1
|
|
|
|
merge:
|
|
needs: build
|
|
runs-on: ubuntu-latest
|
|
|
|
permissions:
|
|
contents: read
|
|
packages: write
|
|
id-token: write
|
|
|
|
steps:
|
|
- name: Download digests
|
|
uses: actions/download-artifact@v4
|
|
with:
|
|
path: ${{ env.DIGEST_PATH }}
|
|
pattern: digests-*-${{ env.DIGEST_NAME_SUFFIX }}
|
|
merge-multiple: true
|
|
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
- name: Docker meta
|
|
id: meta
|
|
uses: docker/metadata-action@v5
|
|
with:
|
|
images: ${{ env.REGISTRY }}/${{ inputs.image_name }}
|
|
tags: |
|
|
type=raw,value=${{ inputs.tag }},event=branch
|
|
type=ref,event=tag
|
|
|
|
- name: Login to registry
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ env.REGISTRY }}
|
|
username: ${{ github.actor }}
|
|
password: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
- name: Create manifest list and push
|
|
id: push
|
|
working-directory: ${{ env.DIGEST_PATH }}
|
|
run: |
|
|
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
|
|
$(printf '${{ env.REGISTRY }}/${{ inputs.image_name }}@sha256:%s ' *)
|
|
|
|
- name: Inspect image
|
|
run: |
|
|
docker buildx imagetools inspect ${{ env.REGISTRY }}/${{ inputs.image_name }}:${{ steps.meta.outputs.version }}
|