aboutsummaryrefslogtreecommitdiffstats
path: root/.github/workflows/docker.yml
blob: d6a4f7b6a6d12f069f5cbb4e8fd0157ad691c9b6 (plain) (blame)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
name: Docker Build and Publish

on:
  push:
    branches:
      - 'main'
  # Manual trigger from the UI
  workflow_dispatch:
    inputs:
      message:
        description: "Message for build"
        required: true

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v4
      -
        name: Check whether there are any commits since this run was last triggered and push them and/or set the output
        id: should_run
        run: |
          git config user.name github-actions
          git config user.email github-actions@github.com

          # Defensive: update submodules in the main branch
          git checkout main
          git submodule update --init --recursive --remote --rebase --force
          git add .
          git commit -am "Update submodules [skip ci]" --no-verify || true
          git push origin main --no-verify
          git checkout -

          git rebase main

          CHANGES_COUNT=$(git log @{u}.. | wc -l)
          MANUAL_REBUILD="${{ github.event_name == 'workflow_dispatch' }}"
          VERSION_BUMP="${{ contains(github.event.inputs.message, '[version bump]') }}"
          if [ $CHANGES_COUNT -gt 0 ] || [[ $MANUAL_REBUILD == "true" && $VERSION_BUMP == "true" ]]; then
            # Do the version bump in the 'main' branch ONLY if
            #   there were other changes coming from the 'main' branch (or)
            #   this is a manual trigger with the key-phrase
            git checkout main
            TAG_NAME=$(npm version -m "%s [skip ci]" patch)
            git commit --all --amend --no-edit --no-verify
            git push origin main --no-verify
            git tag -f $TAG_NAME
            git push origin --tags --no-verify
            echo "TAG_NAME=${TAG_NAME:1}" >> $GITHUB_ENV
          fi

          echo "Number of changes: $CHANGES_COUNT"
          if [ $CHANGES_COUNT -eq 0 ] && [ $MANUAL_REBUILD != "true" ]; then
            echo "No changes found - terminating the build"
            echo "::set-output name=should_run::false"
          else   # changes > 0 (or) MANUAL_REBUILD=true
            echo "Pushing rebased commits"
            git push origin $(git rev-parse --abbrev-ref HEAD) --no-verify
          fi
      -
        name: Docker meta
        id: meta
        if: ${{ (steps.should_run.outputs.should_run != 'false') && (github.event_name != 'workflow_dispatch' || (github.event_name == 'workflow_dispatch' && (contains(github.event.inputs.message, '[main]'))) )}}
        uses: docker/metadata-action@v5
        with:
          # list of Docker images to use as base name for tags
          images: |
            ${{ github.repository }}
            ghcr.io/${{ github.repository }}
          # generate Docker tags based on the following events/attributes
          tags: |
            ${{ env.TAG_NAME }}
            latest
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        if: ${{ (steps.should_run.outputs.should_run != 'false') && (github.event_name != 'workflow_dispatch' || (github.event_name == 'workflow_dispatch' && (contains(github.event.inputs.message, '[main]'))) )}}
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
        if: ${{ (steps.should_run.outputs.should_run != 'false') && (github.event_name != 'workflow_dispatch' || (github.event_name == 'workflow_dispatch' && (contains(github.event.inputs.message, '[main]'))) )}}
      -
        name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        if: ${{ (steps.should_run.outputs.should_run != 'false') && (github.event_name != 'workflow_dispatch' || (github.event_name == 'workflow_dispatch' && (contains(github.event.inputs.message, '[main]'))) )}}
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      -
        name: Login to DockerHub
        uses: docker/login-action@v3
        if: ${{ (steps.should_run.outputs.should_run != 'false') && (github.event_name != 'workflow_dispatch' || (github.event_name == 'workflow_dispatch' && (contains(github.event.inputs.message, '[main]'))) )}}
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and push
        if: ${{ (steps.should_run.outputs.should_run != 'false') && (github.event_name != 'workflow_dispatch' || (github.event_name == 'workflow_dispatch' && (contains(github.event.inputs.message, '[main]'))) )}}
        uses: docker/build-push-action@v5
        with:
          context: .
          # If needed, we can add more platforms when requested.
          platforms: linux/amd64,linux/arm64
          # Do not push pull requests
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}