name: Publish docker containers

'on':
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'
      - 'v[0-9]+.[0-9]+.[0-9]+-pre[0-9]+'

  workflow_dispatch:
    inputs:
      publish:
        description: 'Publish tag to Docker Hub & GitHub Registry'
        required: false
        type: boolean
        default: false
      tag:
        description: 'Git Tag to build and publish'
        required: false
        type: string
        default: ''
      apps:
        description: 'Applications to build and publish'
        required: false
        type: choice
        options:
          - '["explorer", "token", "trading"]'
          - '["explorer"]'
          - '["token"]'
          - '["trading"]'
      archs:
        description: 'Architecture to build and publish'
        required: false
        type: choice
        options:
          - linux/amd64, linux/arm64
          - linux/amd64
          - linux/arm64

jobs:
  master:
    strategy:
      fail-fast: false
      matrix:
        app: ${{ fromJson(inputs.apps || '["explorer", "token", "trading"]') }}
    name: Build the ${{ matrix.app }} image
    runs-on: ubuntu-latest
    steps:
      - name: Check out code
        uses: actions/checkout@v3
        with:
          ref: ${{ inputs.tag }}

      - name: Set up QEMU
        id: quemu
        uses: docker/setup-qemu-action@v2

      - name: Available platforms
        run: echo ${{ steps.qemu.outputs.platforms }}

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

      - name: Login to DockerHub
        if: ${{ inputs.publish || startsWith(github.ref, 'refs/tags/') }}
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Determine Docker Image tag
        id: tags
        run: |
          hash=$(git rev-parse HEAD|cut -b1-8)
          versionTag=${{ inputs.tag || startsWith(github.ref, 'refs/tags/') && github.ref_name || '${hash}' }}
          echo ::set-output name=version::${versionTag}
          echo ::set-output name=npmVersion::$(cat dockerfiles/${{ matrix.app =='trading' && 'Dockerfile.next' || 'Dockerfile.cra' }} | grep FROM | head -n 1 | awk '{print $2}' | cut -d ':' -f 2 | cut -d '-' -f 1 )

      - name: Print config
        run: |
          git rev-parse --verify HEAD
          git status
          echo "inputs.tag=${{ inputs.tag }}"
          echo "inputs.publish=${{ inputs.publish }}"
          echo "inputs.apps=${{ inputs.apps }}"
          echo "inputs.archs=${{ inputs.archs }}"
          echo "steps.tags.outputs.version=${{ steps.tags.outputs.version }}"

      - uses: actions/setup-node@v3
        with:
          node-version: ${{ steps.tags.outputs.npmVersion }}

      - name: Build frontend dists
        run: |
          yarn --verbose --pure-lockfile
          yarn nx ${{ matrix.app =='trading' && 'export' || 'build' }} ${{ matrix.app }} --pure-lockfile

      - name: Build and export to local Docker
        uses: docker/build-push-action@v3
        with:
          context: .
          push: false
          file: dockerfiles/${{ matrix.app =='trading' && 'Dockerfile.next' || 'Dockerfile.cra' }}.dist
          build-args: APP=${{ matrix.app }}
          load: true
          tags: vegaprotocol/${{ matrix.app }}:local

      - name: Sanity check docker image
        run: |
          docker run --rm vegaprotocol/${{ matrix.app }}:local cat .env
          docker run --rm vegaprotocol/${{ matrix.app }}:local ls -lah

      - name: Build and push to DockerHub
        id: docker_build
        uses: docker/build-push-action@v3
        with:
          context: .
          push: ${{ inputs.publish || startsWith(github.ref, 'refs/tags/') }}
          file: dockerfiles/${{ matrix.app =='trading' && 'Dockerfile.next' || 'Dockerfile.cra' }}.dist
          build-args: APP=${{ matrix.app }}
          platforms: ${{ inputs.archs || 'linux/amd64, linux/arm64' }}
          tags: |
            vegaprotocol/${{ matrix.app }}:latest
            vegaprotocol/${{ matrix.app }}:${{ steps.tags.outputs.version }}

      - name: Image digest
        run: echo ${{ steps.docker_build.outputs.digest }}