Compare commits

...

9 Commits

2 changed files with 78 additions and 85 deletions

View File

@@ -7,32 +7,39 @@ on:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
IMAGE_NAME: krau/saveany-bot
jobs:
build-and-push:
prepare:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
version: ${{ steps.vars.outputs.version }}
git_commit: ${{ steps.vars.outputs.git_commit }}
build_time: ${{ steps.vars.outputs.build_time }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- id: vars
run: |
VERSION=${GITHUB_REF#refs/tags/v}
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
echo "git_commit=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
echo "build_time=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> "$GITHUB_OUTPUT"
- 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
build:
needs: prepare
strategy:
matrix:
arch: [amd64, arm64]
type: [default, micro, pico]
fail-fast: false
runs-on: ${{ matrix.arch == 'amd64' && 'ubuntu-latest' || 'ubuntu-24.04-arm' }}
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
@@ -44,71 +51,58 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Dockerfile args
id: args
- name: Build and push by arch
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' }}
push: true
tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.type }}-${{ matrix.arch }}
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.type }}-${{ needs.prepare.outputs.version }}-${{ matrix.arch }}
build-args: |
VERSION=${{ needs.prepare.outputs.version }}
GitCommit=${{ needs.prepare.outputs.git_commit }}
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 group
run: |
echo "git_commit=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
echo "build_time=$(git show -s --format=%cI)" >> "$GITHUB_OUTPUT"
TAG_PREFIX="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}"
VERSION="${{ needs.prepare.outputs.version }}"
TYPE="${{ matrix.type }}"
- name: Build and push Docker image
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 }}
if [ "$TYPE" == "default" ]; then
TARGET_TAGS=("$TAG_PREFIX:latest" "$TAG_PREFIX:$VERSION")
else
TARGET_TAGS=("$TAG_PREFIX:$TYPE" "$TAG_PREFIX:$TYPE-latest" "$TAG_PREFIX:$TYPE-$VERSION")
fi
- name: Build and push micro Docker image
id: build-and-push-micro
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile.micro
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
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 }}
for final_tag in "${TARGET_TAGS[@]}"; do
docker buildx imagetools create -t "$final_tag" \
"$TAG_PREFIX:$TYPE-amd64" \
"$TAG_PREFIX:$TYPE-arm64"
done
- name: Build and push pico Docker image
id: build-and-push-pico
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile.pico
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
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 }}
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: Inspect manifest
run: |
docker buildx imagetools inspect ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ matrix.type }}

View File

@@ -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.Docker=true' \
" \
-o saveany-bot .
-o saveany-bot . && chmod +x saveany-bot
FROM scratch
WORKDIR /app
COPY --from=builder /app/saveany-bot .
RUN chmod +x /app/saveany-bot
ENTRYPOINT ["/app/saveany-bot"]