aboutsummaryrefslogtreecommitdiff
path: root/.github/workflows/publish.yml
blob: 50a13a2a7c6027e1e329f283779fa22822985507 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
name: Publish Docker Image

on:
   push:
      branches:
         - master
      tags:
         - v**
   workflow_dispatch:

jobs:
   publish:
      name: Publish Docker Image to ghcr.io
      runs-on: ubuntu-latest
      strategy:
         fail-fast: false
         matrix:
            runner: [ubuntu-latest, macos-latest]
            
      permissions:
         packages: write

      steps:
         -  name: Check out the repo
            uses: actions/checkout@v4

         -  name: Setup variables
            run: |
               # docker/build-push-action supports comma separated tags
               DOCKER_IMAGE_TAG_BASE="ghcr.io/${{ github.repository }}"
               DOCKER_IMAGE_TAGS="$DOCKER_IMAGE_TAG_BASE:dev"

               # use ubuntu-latest to build x86 and macos-latest to build aarch64
               if [[ "${{ matrix.runner }}" == macos-latest ]]; then
                  echo "DOCKER_PLATFORM=linux/arm64" >> $GITHUB_ENV
               else
                  echo "DOCKER_PLATFORM=linux/amd64" >> $GITHUB_ENV
               fi

               # if this is a versioned release, github.ref will start with 'refs/tags/v'
               GH_REF="${{ github.ref }}"
               # cut off 'refs/tags/v'
               if [[ $GH_REF == "refs/tags/v"* ]]; then
                  VERSION=$(echo $GH_REF | cut -c 12-)
                  DOCKER_IMAGE_TAGS="$DOCKER_IMAGE_TAGS,$DOCKER_IMAGE_TAG_BASE:latest,$DOCKER_IMAGE_TAG_BASE:$VERSION"
               fi

               echo "DOCKER_IMAGE_TAGS=\"$DOCKER_IMAGE_TAGS\"" >> $GITHUB_ENV
               # this is dumb
               echo "VERSION=$VERSION" >> $GITHUB_ENV

         -  name: Check if version already exists in registry
            run: |
               # this is still dumb
               VERSION=${{ env.VERSION }}
               if docker manifest inspect ghcr.io/${{ github.repository }}:$VERSION > /dev/null 2>&1; then
                 echo "Image with version $VERSION already exists in registry"
                 exit 1
               fi

         -  name: Log in to ghcr.io
            uses: docker/login-action@v3
            with:
               registry: ghcr.io
               username: ${{ github.actor }}
               password: ${{ secrets.GITHUB_TOKEN }}

         -  name: Set up Docker Buildx
            uses: docker/setup-buildx-action@v3

         -  name: Build and push Docker Image
            uses: docker/build-push-action@v6
            with:
               context: .
               file: ./Dockerfile
               push: true
               tags: ${{ env.DOCKER_IMAGE_TAGS }}
               platforms: ${{ env.DOCKER_PLATFORM }}
               cache-from: type=gha
               cache-to: type=gha,mode=max