on:
    push:
        branches:
            - master

permissions:
    contents: write
    pull-requests: write
    id-token: write

name: release
jobs:
    release_please:
        runs-on: ubuntu-latest
        outputs:
            major: ${{ steps.release.outputs.major }}
            minor: ${{ steps.release.outputs.minor }}
            patch: ${{ steps.release.outputs.patch }}
            release_created: ${{ steps.release.outputs.release_created }}
        steps:
            - uses: google-github-actions/release-please-action@v3
              id: release
              with:
                  release-type: node
                  package-name: ${{env.NPM_MODULE_NAME}}
                  pull-request-title-pattern: 'chore${scope}: release ${version} [skip-ci]'
            # The logic below handles the npm publication:
            - uses: actions/checkout@v3
              # these if statements ensure that a publication only occurs when
              # a new release is created:
              if: ${{ steps.release.outputs.release_created }}
            - uses: actions/setup-node@v3
              with:
                  node-version: 18
                  registry-url: 'https://registry.npmjs.org'
              if: ${{ steps.release.outputs.release_created }}
            - run: npm ci
              if: ${{ steps.release.outputs.release_created }}
            - run: npm publish --provenance --access public
              env:
                  NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
              if: ${{ steps.release.outputs.release_created }}

    publish_docker:
        runs-on: ubuntu-latest
        needs: release_please
        if: ${{needs.release_please.outputs.release_created}}
        steps:
            - run: echo version v${{needs.release_please.outputs.major}}.${{needs.release_please.outputs.minor}}.${{needs.release_please.outputs.patch}}

            - uses: actions/checkout@v3

            - name: Set up QEMU
              uses: docker/setup-qemu-action@v3
              with:
                  platforms: 'arm64,arm'

            - name: Set up Docker Buildx
              id: buildx
              uses: docker/setup-buildx-action@v3
              with:
                  platforms: linux/arm64,linux/amd64,linux/arm/v7

            - name: Login to DockerHub
              uses: docker/login-action@v3
              with:
                  username: ${{ secrets.DOCKERHUB_USERNAME }}
                  password: ${{ secrets.DOCKERHUB_TOKEN }}

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

            - name: Build and push
              uses: docker/build-push-action@v5
              with:
                  context: .
                  platforms: ${{ steps.buildx.outputs.platforms }}
                  push: true
                  tags: |
                      ${{ github.repository }}:${{needs.release_please.outputs.major}}.${{needs.release_please.outputs.minor}}.${{needs.release_please.outputs.patch}}
                      ${{ github.repository }}:${{needs.release_please.outputs.major}}.${{needs.release_please.outputs.minor}}
                      ${{ github.repository }}:${{needs.release_please.outputs.major}}
