name: Reusable Publish Docker Image Workflow

on:
  workflow_call:
    inputs:
      service_name:
        required: true
        type: string
      package_path:
        required: true
        type: string
      workspace_name:
        required: true
        type: string
    secrets:
      DOCKER_USERNAME:
        required: true
      DOCKER_PASSWORD:
        required: true
      AWS_ACCESS_KEY_ID:
        required: true
      AWS_SECRET_ACCESS_KEY:
        required: true

jobs:
  publish:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    - uses: actions/setup-python@v4
      with:
        python-version: '3.11'

    - name: Create Bundle Dir
      id: bundle-dir
      run: echo "temp_dir=$(mktemp -d -t ${{ inputs.service_name }}-${{ github.sha }}-XXXXXXX)" >> $GITHUB_OUTPUT

    - name: Cache build
      id: cache-build
      uses: actions/cache@v3
      with:
        path: |
          packages/**/dist
          ${{ steps.bundle-dir.outputs.temp_dir }}
        key: ${{ runner.os }}-${{ inputs.service_name }}-build-${{ github.sha }}

    - name: Set up Node
      uses: actions/setup-node@v3
      with:
        registry-url: 'https://registry.npmjs.org'
        node-version-file: '.nvmrc'

    - name: Install dependencies
      run: yarn install --immutable

    - name: Build
      if: steps.cache-build.outputs.cache-hit != 'true'
      run: yarn build ${{ inputs.package_path }}

    - name: Bundle
      if: steps.cache-build.outputs.cache-hit != 'true'
      run: yarn workspace ${{ inputs.workspace_name }} bundle --no-compress --output-directory ${{ steps.bundle-dir.outputs.temp_dir }}

    - name: Login to Docker Hub
      uses: docker/login-action@v2
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}

    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: us-east-1

    - name: Login to Amazon ECR
      id: login-ecr
      uses: aws-actions/amazon-ecr-login@v1

    - name: Set up QEMU
      uses: docker/setup-qemu-action@master
      with:
        platforms: all

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

    - name: Publish Docker image
      uses: docker/build-push-action@v3
      with:
        builder: ${{ steps.buildx.outputs.name }}
        context: ${{ steps.bundle-dir.outputs.temp_dir }}
        file: ${{ steps.bundle-dir.outputs.temp_dir }}/${{ inputs.package_path }}/Dockerfile
        platforms: linux/amd64,linux/arm64
        push: true
        tags: |
          standardnotes/${{ inputs.service_name }}:latest
          standardnotes/${{ inputs.service_name }}:${{ github.sha }}
          ${{ steps.login-ecr.outputs.registry }}/${{ inputs.service_name }}:${{ github.sha }}
          ${{ steps.login-ecr.outputs.registry }}/${{ inputs.service_name }}:latest
