name: Build/Release Additional Docker Platform Images

# Only run when:
#   - the workflow is automatically triggered during a release with the relevant tag
#   - the workflow is started from the UI with a tag
on:
  workflow_call:
    inputs:
      tag:
        required: true
        type: string
    secrets:
      DOCKERHUB_USERNAME:
        required: true
      DOCKERHUB_PASSWORD:
        required: true

env:
  BUILD_PLATFORMS: linux/arm64

jobs:
  # Build docker image, tag it with the branch and docker image tag passed in, and publish
  build-publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Set Vars
        run: |
          echo "GITHUB_SHA_SHORT=${GITHUB_SHA::7}" >> $GITHUB_ENV
          echo "GITHUB_REF_SHORT=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v3
        with:
          images: |
            blockstack/${{ github.event.repository.name }}
          tags: |
            type=ref,event=branch
            ${{ inputs.tag }}

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

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

      - name: Build/Tag/Push Image
        uses: docker/build-push-action@v2
        with:
          platforms: ${{ env.BUILD_PLATFORMS }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            STACKS_NODE_VERSION=${{ inputs.tag || env.GITHUB_SHA_SHORT }}
            GIT_BRANCH=${{ env.GITHUB_REF_SHORT }}
            GIT_COMMIT=${{ env.GITHUB_SHA_SHORT }}
          push: true

  # Build docker image, tag it with the branch and docker image tag passed in, and publish
  build-publish-stretch:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Determine Stretch Tag and build platforms
        run: |
          if [[ -z ${TAG} ]]; then
              REF=$(echo ${GITHUB_REF#refs/*/} | tr / -)
              if [[ "${REF}" == "master" ]]; then
                  echo "STRETCH_TAG=latest-stretch" >> $GITHUB_ENV
              else
                  echo "STRETCH_TAG=${REF}-stretch" >> $GITHUB_ENV
              fi
          else
              echo "STRETCH_TAG=${TAG}-stretch" >> $GITHUB_ENV
          fi
        env:
          TAG: ${{ inputs.tag }}

      - name: Set Vars
        run: |
          echo "GITHUB_SHA_SHORT=${GITHUB_SHA::7}" >> $GITHUB_ENV
          echo "GITHUB_REF_SHORT=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v3
        with:
          images: |
            blockstack/${{ github.event.repository.name }}
          tags: |
            type=ref,event=branch
            ${{ env.STRETCH_TAG }}

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

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

      - name: Build/Tag/Push Image
        uses: docker/build-push-action@v2
        with:
          platforms: ${{ env.BUILD_PLATFORMS }}
          file: Dockerfile.stretch
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            STACKS_NODE_VERSION=${{ inputs.tag || env.GITHUB_SHA_SHORT }}
            GIT_BRANCH=${{ env.GITHUB_REF_SHORT }}
            GIT_COMMIT=${{ env.GITHUB_SHA_SHORT }}
          push: true
