mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-07-12 16:02:05 +08:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
adc64ad510 | ||
|
|
da6cf42355 | ||
|
|
8c76516953 | ||
|
|
15c4fffb98 | ||
|
|
b5cdf1e880 | ||
|
|
264dd9f9ed | ||
|
|
b25df2e214 | ||
|
|
5999ddbe1d | ||
|
|
7424190ee5 | ||
|
|
87e8836c78 | ||
|
|
1a7747c2d2 |
207
.github/workflows/build-docker.yml
vendored
207
.github/workflows/build-docker.yml
vendored
@@ -7,32 +7,53 @@ on:
|
|||||||
|
|
||||||
env:
|
env:
|
||||||
REGISTRY: ghcr.io
|
REGISTRY: ghcr.io
|
||||||
IMAGE_NAME: ${{ github.repository }}
|
IMAGE_NAME: krau/saveany-bot
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: docker-build-${{ github.repository }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-and-push:
|
prepare:
|
||||||
runs-on: ubuntu-latest
|
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:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
packages: write
|
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:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v6
|
||||||
|
|
||||||
- name: Extract metadata for Docker
|
|
||||||
id: meta
|
|
||||||
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: Set up QEMU
|
|
||||||
uses: docker/setup-qemu-action@v3
|
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v3
|
||||||
@@ -44,71 +65,99 @@ jobs:
|
|||||||
username: ${{ github.actor }}
|
username: ${{ github.actor }}
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
- name: Extract Dockerfile args
|
- name: Build and push by digest
|
||||||
id: args
|
id: build
|
||||||
|
uses: docker/build-push-action@v6
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: ${{ matrix.type == 'default' && './Dockerfile' || format('./Dockerfile.{0}', matrix.type) }}
|
||||||
|
platforms: ${{ matrix.arch == 'amd64' && 'linux/amd64' || 'linux/arm64' }}
|
||||||
|
# 关键修改:不再使用 tags,而是通过 image output 按摘要推送
|
||||||
|
outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
|
||||||
|
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
|
||||||
|
|
||||||
|
- name:
|
||||||
|
Export digest
|
||||||
|
# 将 digest 写入文件,供后续步骤读取
|
||||||
run: |
|
run: |
|
||||||
echo "git_commit=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
|
mkdir -p /tmp/digests
|
||||||
echo "build_time=$(git show -s --format=%cI)" >> "$GITHUB_OUTPUT"
|
digest="${{ steps.build.outputs.digest }}"
|
||||||
|
touch "/tmp/digests/${digest#sha256:}"
|
||||||
|
|
||||||
- name: Build and push Docker image
|
echo "$digest" > /tmp/digests/digest
|
||||||
id: build-and-push
|
|
||||||
uses: docker/build-push-action@v6
|
|
||||||
with:
|
|
||||||
context: .
|
|
||||||
file: ./Dockerfile
|
|
||||||
platforms: linux/amd64,linux/arm64
|
|
||||||
push: ${{ github.event_name != 'pull_request' }}
|
|
||||||
tags: ${{ steps.meta.outputs.tags }}
|
|
||||||
labels: ${{ steps.meta.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 }}
|
|
||||||
|
|
||||||
- name: Build and push micro Docker image
|
- name: Upload digest
|
||||||
id: build-and-push-micro
|
uses: actions/upload-artifact@v4
|
||||||
uses: docker/build-push-action@v6
|
|
||||||
with:
|
with:
|
||||||
context: .
|
name: digest-${{ matrix.type }}-${{ matrix.arch }}
|
||||||
file: ./Dockerfile.micro
|
path: /tmp/digests/digest
|
||||||
platforms: linux/amd64,linux/arm64
|
if-no-files-found: error
|
||||||
push: ${{ github.event_name != 'pull_request' }}
|
retention-days: 1
|
||||||
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 }}
|
|
||||||
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 }}
|
|
||||||
|
|
||||||
- name: Build and push pico Docker image
|
create-manifest:
|
||||||
id: build-and-push-pico
|
needs: [prepare, build]
|
||||||
uses: docker/build-push-action@v6
|
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:
|
with:
|
||||||
context: .
|
registry: ${{ env.REGISTRY }}
|
||||||
file: ./Dockerfile.pico
|
username: ${{ github.actor }}
|
||||||
platforms: linux/amd64,linux/arm64
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
push: ${{ github.event_name != 'pull_request' }}
|
|
||||||
tags: |
|
- name: Download digests
|
||||||
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:pico
|
uses: actions/download-artifact@v4
|
||||||
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:pico-latest
|
with:
|
||||||
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:pico-${{ steps.meta.outputs.version }}
|
path: /tmp/digests
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
pattern: digest-${{ matrix.type }}-*
|
||||||
cache-from: |
|
merge-multiple: false
|
||||||
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
|
|
||||||
type=gha
|
- name: Create and push manifest lists
|
||||||
cache-to: type=gha,mode=max
|
run: |
|
||||||
build-args: |
|
REPO="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}"
|
||||||
VERSION=${{ steps.meta.outputs.version }}
|
VERSION="${{ needs.prepare.outputs.version }}"
|
||||||
GitCommit=${{ steps.args.outputs.git_commit }}
|
MAJOR_MINOR="${{ needs.prepare.outputs.major_minor }}"
|
||||||
BuildTime=${{ steps.args.outputs.build_time }}
|
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
|
||||||
|
|||||||
@@ -24,13 +24,12 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
|
|||||||
-X 'github.com/krau/SaveAny-Bot/config.BuildTime=${BuildTime}' \
|
-X 'github.com/krau/SaveAny-Bot/config.BuildTime=${BuildTime}' \
|
||||||
-X 'github.com/krau/SaveAny-Bot/config.Docker=true' \
|
-X 'github.com/krau/SaveAny-Bot/config.Docker=true' \
|
||||||
" \
|
" \
|
||||||
-o saveany-bot .
|
-o saveany-bot . && chmod +x saveany-bot
|
||||||
|
|
||||||
FROM scratch
|
FROM scratch
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY --from=builder /app/saveany-bot .
|
COPY --from=builder /app/saveany-bot .
|
||||||
RUN chmod +x /app/saveany-bot
|
|
||||||
|
|
||||||
ENTRYPOINT ["/app/saveany-bot"]
|
ENTRYPOINT ["/app/saveany-bot"]
|
||||||
|
|||||||
Reference in New Issue
Block a user