From 9cee2351f410b17a41746eb38b495e393d3e4247 Mon Sep 17 00:00:00 2001 From: Victor Antonovich Date: Wed, 27 Mar 2024 16:38:59 +0300 Subject: [PATCH] Add docker image publish github workflow --- .github/workflows/build.yml | 2 +- .github/workflows/docker-publish.yml | 62 ++++++++++++++++++++++++++++ Dockerfile | 2 +- 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/docker-publish.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index eade4dd..42c7701 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,4 +1,4 @@ -name: mbusd +name: build on: push: diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 0000000..d3b788a --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -0,0 +1,62 @@ +name: docker publish + +on: + workflow_run: + workflows: [ 'build' ] + types: [ completed ] + branches: [ 'master' ] + tags: [ 'v*' ] + +env: + PLATFORMS: linux/amd64,linux/arm64,linux/arm/v7 + # github.repository as / + IMAGE_NAME: ${{ github.repository }} + +jobs: + build: + runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'success' }} + steps: + - name: Checkout repository + 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 Docker Hub + if: github.event_name != 'pull_request' + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + # Extract metadata (tags, labels) for Docker + # https://github.com/docker/metadata-action + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.IMAGE_NAME }} + tags: | + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=sha + + # Build and push Docker image with Buildx (don't push on PR) + # https://github.com/docker/build-push-action + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@v5 + with: + context: . + platforms: ${{ env.PLATFORMS }} + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha,scope=${{ env.IMAGE_NAME }} + cache-to: type=gha,mode=max,scope=${{ env.IMAGE_NAME }} diff --git a/Dockerfile b/Dockerfile index 8579f6c..5aa0386 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM alpine:latest AS build +FROM --platform=$BUILDPLATFORM alpine:latest AS build RUN apk add --no-cache alpine-sdk cmake linux-headers COPY . /mbusd WORKDIR /mbusd/build