name: Reusable Server Utility Workflow

on:
  workflow_call:
    inputs:
      service_name:
        required: true
        type: string
      workspace_name:
        required: true
        type: string
      deploy_web:
        required: false
        default: true
        type: boolean
      deploy_worker:
        required: false
        default: true
        type: boolean
      package_path:
        required: true
        type: string
    secrets:
      DOCKER_USERNAME:
        required: true
      DOCKER_PASSWORD:
        required: true
      CI_PAT_TOKEN:
        required: true
      AWS_ACCESS_KEY_ID:
        required: true
      AWS_SECRET_ACCESS_KEY:
        required: true

jobs:
  build:
    runs-on: ubuntu-latest

    outputs:
      temp_dir: ${{ steps.bundle-dir.outputs.temp_dir }}

    steps:
    - uses: actions/checkout@v3

    - 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: Build
      run: yarn build ${{ inputs.package_path }}

    - name: Bundle
      run: yarn workspace ${{ inputs.workspace_name }} bundle --no-compress --output-directory ${{ steps.bundle-dir.outputs.temp_dir }}

  lint:
    runs-on: ubuntu-latest

    needs: build

    steps:
    - uses: actions/checkout@v3

    - name: Cache build
      id: cache-build
      uses: actions/cache@v3
      with:
        path: |
          packages/**/dist
          ${{ needs.build.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: Build
      if: steps.cache-build.outputs.cache-hit != 'true'
      run: yarn build ${{ inputs.package_path }}

    - name: Lint
      run: yarn lint:${{ inputs.service_name }}

  test:
    runs-on: ubuntu-latest

    needs: build

    steps:
    - uses: actions/checkout@v3

    - name: Cache build
      id: cache-build
      uses: actions/cache@v3
      with:
        path: |
          packages/**/dist
          ${{ needs.build.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: Build
      if: steps.cache-build.outputs.cache-hit != 'true'
      run: yarn build ${{ inputs.package_path }}

    - name: Test
      run: yarn test ${{ inputs.package_path }}

  publish:
    needs: [ build, test, lint ]

    name: Publish Docker Image
    uses: standardnotes/server/.github/workflows/common-docker-image.yml@main
    with:
      service_name: ${{ inputs.service_name }}
      bundle_dir: ${{ needs.build.outputs.temp_dir }}
      package_path: ${{ inputs.package_path }}
      workspace_name: ${{ inputs.workspace_name }}
    secrets: inherit

  deploy-web:
    if: ${{ inputs.deploy_web }}

    needs: publish

    name: Deploy Web
    uses: standardnotes/server/.github/workflows/common-deploy.yml@main
    with:
      service_name: ${{ inputs.service_name }}
      docker_image: ${{ inputs.service_name }}:${{ github.sha }}
    secrets: inherit

  deploy-worker:
    if: ${{ inputs.deploy_worker }}

    needs: publish

    name: Deploy Worker
    uses: standardnotes/server/.github/workflows/common-deploy.yml@main
    with:
      service_name: ${{ inputs.service_name }}-worker
      docker_image: ${{ inputs.service_name }}:${{ github.sha }}
    secrets: inherit
