Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
adc64ad510 | ||
|
|
da6cf42355 | ||
|
|
8c76516953 | ||
|
|
15c4fffb98 |
164
.github/workflows/build-docker.yml
vendored
164
.github/workflows/build-docker.yml
vendored
@@ -18,35 +18,26 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
packages: write
|
|
||||||
|
|
||||||
outputs:
|
outputs:
|
||||||
tags: ${{ steps.meta.outputs.tags }}
|
version: ${{ steps.vars.outputs.version }}
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
major_minor: ${{ steps.vars.outputs.major_minor }}
|
||||||
version: ${{ steps.args.outputs.version }}
|
short_sha: ${{ steps.vars.outputs.short_sha }}
|
||||||
git_commit: ${{ steps.args.outputs.git_commit }}
|
build_time: ${{ steps.vars.outputs.build_time }}
|
||||||
build_time: ${{ steps.args.outputs.build_time }}
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v6
|
||||||
|
|
||||||
- name: Extract metadata for Docker
|
- name: Extract Version Components
|
||||||
id: meta
|
id: vars
|
||||||
uses: docker/metadata-action@v5
|
|
||||||
with:
|
|
||||||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
|
||||||
tags: |
|
|
||||||
type=semver,pattern={{version}}
|
|
||||||
type=semver,pattern={{major}}.{{minor}}
|
|
||||||
type=sha
|
|
||||||
type=raw,value=latest
|
|
||||||
|
|
||||||
- name: Extract Dockerfile args
|
|
||||||
id: args
|
|
||||||
run: |
|
run: |
|
||||||
echo "git_commit=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
|
VERSION=${GITHUB_REF#refs/tags/v}
|
||||||
echo "build_time=$(git show -s --format=%cI)" >> "$GITHUB_OUTPUT"
|
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:
|
build:
|
||||||
needs: prepare
|
needs: prepare
|
||||||
@@ -58,12 +49,11 @@ jobs:
|
|||||||
arch: [amd64, arm64]
|
arch: [amd64, arm64]
|
||||||
type: [default, micro, pico]
|
type: [default, micro, pico]
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
|
|
||||||
runs-on: ${{ matrix.arch == 'amd64' && 'ubuntu-latest' || 'ubuntu-24.04-arm' }}
|
runs-on: ${{ matrix.arch == 'amd64' && 'ubuntu-latest' || 'ubuntu-24.04-arm' }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v6
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v3
|
||||||
@@ -75,43 +65,99 @@ jobs:
|
|||||||
username: ${{ github.actor }}
|
username: ${{ github.actor }}
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
- name: Set Dockerfile path
|
- name: Build and push by digest
|
||||||
id: dockerfile
|
id: build
|
||||||
run: |
|
|
||||||
if [ "${{ matrix.type }}" == "default" ]; then
|
|
||||||
echo "DOCKERFILE=./Dockerfile" >> "$GITHUB_OUTPUT"
|
|
||||||
elif [ "${{ matrix.type }}" == "micro" ]; then
|
|
||||||
echo "DOCKERFILE=./Dockerfile.micro" >> "$GITHUB_OUTPUT"
|
|
||||||
else
|
|
||||||
echo "DOCKERFILE=./Dockerfile.pico" >> "$GITHUB_OUTPUT"
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: Set image tags
|
|
||||||
id: tags
|
|
||||||
run: |
|
|
||||||
if [ "${{ matrix.type }}" == "default" ]; then
|
|
||||||
TAGS=$(echo "${{ needs.prepare.outputs.tags }}" | tr '\n' ' ')
|
|
||||||
elif [ "${{ matrix.type }}" == "micro" ]; then
|
|
||||||
TAGS="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:micro ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:micro-latest ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:micro-${{ needs.prepare.outputs.version }}"
|
|
||||||
else
|
|
||||||
TAGS="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:pico ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:pico-latest ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:pico-${{ needs.prepare.outputs.version }}"
|
|
||||||
fi
|
|
||||||
echo "TAGS=$TAGS" >> "$GITHUB_OUTPUT"
|
|
||||||
|
|
||||||
- name: Build and push Docker image
|
|
||||||
uses: docker/build-push-action@v6
|
uses: docker/build-push-action@v6
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
file: ${{ steps.dockerfile.outputs.DOCKERFILE }}
|
file: ${{ matrix.type == 'default' && './Dockerfile' || format('./Dockerfile.{0}', matrix.type) }}
|
||||||
platforms: ${{ matrix.arch == 'amd64' && 'linux/amd64' || 'linux/arm64' }}
|
platforms: ${{ matrix.arch == 'amd64' && 'linux/amd64' || 'linux/arm64' }}
|
||||||
push: ${{ github.event_name != 'pull_request' }}
|
# 关键修改:不再使用 tags,而是通过 image output 按摘要推送
|
||||||
tags: ${{ steps.tags.outputs.TAGS }}
|
outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
|
||||||
labels: ${{ needs.prepare.outputs.labels }}
|
|
||||||
cache-from: |
|
|
||||||
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.type }}-latest
|
|
||||||
type=gha
|
|
||||||
cache-to: type=gha,mode=max
|
|
||||||
build-args: |
|
build-args: |
|
||||||
VERSION=${{ needs.prepare.outputs.version }}
|
VERSION=${{ needs.prepare.outputs.version }}
|
||||||
GitCommit=${{ needs.prepare.outputs.git_commit }}
|
GitCommit=${{ needs.prepare.outputs.short_sha }}
|
||||||
BuildTime=${{ needs.prepare.outputs.build_time }}
|
BuildTime=${{ needs.prepare.outputs.build_time }}
|
||||||
|
cache-from: type=gha
|
||||||
|
cache-to: type=gha,mode=max
|
||||||
|
|
||||||
|
- name:
|
||||||
|
Export digest
|
||||||
|
# 将 digest 写入文件,供后续步骤读取
|
||||||
|
run: |
|
||||||
|
mkdir -p /tmp/digests
|
||||||
|
digest="${{ steps.build.outputs.digest }}"
|
||||||
|
touch "/tmp/digests/${digest#sha256:}"
|
||||||
|
|
||||||
|
echo "$digest" > /tmp/digests/digest
|
||||||
|
|
||||||
|
- name: Upload digest
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: digest-${{ matrix.type }}-${{ matrix.arch }}
|
||||||
|
path: /tmp/digests/digest
|
||||||
|
if-no-files-found: error
|
||||||
|
retention-days: 1
|
||||||
|
|
||||||
|
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: Download digests
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
path: /tmp/digests
|
||||||
|
pattern: digest-${{ matrix.type }}-*
|
||||||
|
merge-multiple: false
|
||||||
|
|
||||||
|
- 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 }}"
|
||||||
|
|
||||||
|
DIGEST_AMD64=$(cat /tmp/digests/digest-${TYPE}-amd64/digest)
|
||||||
|
DIGEST_ARM64=$(cat /tmp/digests/digest-${TYPE}-arm64/digest)
|
||||||
|
|
||||||
|
echo "Found digests for $TYPE:"
|
||||||
|
echo "AMD64: $DIGEST_AMD64"
|
||||||
|
echo "ARM64: $DIGEST_ARM64"
|
||||||
|
|
||||||
|
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}@${DIGEST_AMD64}"
|
||||||
|
SRC_ARM64="${REPO}@${DIGEST_ARM64}"
|
||||||
|
|
||||||
|
echo "Creating manifest list with sources:"
|
||||||
|
echo " $SRC_AMD64"
|
||||||
|
echo " $SRC_ARM64"
|
||||||
|
|
||||||
|
for TAG in "${TAGS[@]}"; do
|
||||||
|
echo "Pushing tag: $TAG"
|
||||||
|
docker buildx imagetools create -t "$TAG" "$SRC_AMD64" "$SRC_ARM64"
|
||||||
|
done
|
||||||
|
|||||||
Reference in New Issue
Block a user