From c4de3961a085fd716d8fc500f5ba66e61ca17781 Mon Sep 17 00:00:00 2001 From: Sergio <77530549+sergi0g@users.noreply.github.com> Date: Mon, 3 Mar 2025 10:05:18 +0200 Subject: [PATCH] Optimize CI for docker image --- .github/actions/Dockerfile | 11 +++++++ .github/actions/build-image.yml | 55 +++++++++++++++++++++++++++++++++ .github/workflows/nightly.yml | 42 +++++++------------------ .github/workflows/release.yml | 38 ++++++----------------- 4 files changed, 88 insertions(+), 58 deletions(-) create mode 100644 .github/actions/Dockerfile create mode 100644 .github/actions/build-image.yml diff --git a/.github/actions/Dockerfile b/.github/actions/Dockerfile new file mode 100644 index 0000000..7180f22 --- /dev/null +++ b/.github/actions/Dockerfile @@ -0,0 +1,11 @@ +FROM --platform=$BUILDPLATFORM alpine AS builder + +ARG TARGETARCH +ARG TARGETOS + +COPY . / +RUN mv cup-$TARGETOS-$TARGETARCH cup + +FROM scratch +COPY --from=builder /cup /cup +ENTRYPOINT ["/cup"] \ No newline at end of file diff --git a/.github/actions/build-image.yml b/.github/actions/build-image.yml new file mode 100644 index 0000000..265963b --- /dev/null +++ b/.github/actions/build-image.yml @@ -0,0 +1,55 @@ +name: Build Image + +on: + workflow_call: + inputs: + tags: + description: "Docker image tags" + required: true + secrets: + GITHUB_TOKEN: + required: true + +jobs: + build-image: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Download binaries + uses: actions/download-artifact@v4 + with: + path: . + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: | + ghcr.io/sergi0g/cup + tags: ${{ inputs.tags }} + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: sergi0g + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push image + uses: docker/build-push-action@v6 + with: + context: . + file: Dockerfile + platforms: linux/amd64,linux/arm64 + push: true + tags: ${{ steps.meta.outputs.tags }} + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index ce90563..c59f77a 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -36,7 +36,7 @@ jobs: uses: actions/setup-node@v4 with: node-version: 20 - + - name: Set up Bun uses: oven-sh/setup-bun@v1 @@ -62,38 +62,20 @@ jobs: cup-linux-arm64 build-image: - needs: get-tag - runs-on: ubuntu-latest - steps: - - name: Checkout - 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: ghcr.io - username: ${{ github.repository_owner }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Build and push image - uses: docker/build-push-action@v6 - with: - context: . - platforms: linux/amd64, linux/arm64 - push: true - tags: ghcr.io/sergi0g/cup:${{ needs.get-tag.outputs.tag }} - cache-from: type=gha - cache-to: type=gha,mode=max + needs: + - get-tag + - build-binaries + uses: ./.github/actions/build-image.yml + with: + tags: ${{ needs.get-tag.outputs.tag }} + secrets: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} nightly-release: runs-on: ubuntu-latest - needs: [build-binaries, get-tag] + needs: + - build-binaries + - build-image steps: - name: Download binaries uses: actions/download-artifact@v4 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ee799dd..1721419 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -60,34 +60,16 @@ jobs: cup-linux-arm64 build-image: - needs: get-tag - runs-on: ubuntu-latest - steps: - - name: Checkout - 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: ghcr.io - username: ${{ github.repository_owner }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Build and push image - uses: docker/build-push-action@v6 - with: - context: . - platforms: linux/amd64, linux/arm64 - push: true - tags: ghcr.io/sergi0g/cup:${{ needs.get-tag.outputs.tag }},ghcr.io/sergi0g/cup:latest - cache-from: type=gha - cache-to: type=gha,mode=max + needs: + - get-tag + - build-binaries + uses: ./.github/actions/build-image.yml + with: + tags: | + ${{ needs.get-tag.outputs.tag }} + latest + secrets: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} release: runs-on: ubuntu-latest