name: CI

on:
  push:
    branches:
      - 'main'
    tags:
      - '*'
  pull_request:
    branches:
      - 'main'
  schedule:
    - cron: 0 2 * * *

jobs:
  test:
    if: ${{ github.event_name != 'schedule' }}
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 https://github.com/actions/checkout/releases/tag/v4.1.1
    - name: Use Node.js
      uses: actions/setup-node@1a4442cacd436585916779262731d5b162bc6ec7 # v3.8.2 https://github.com/actions/setup-node/releases/tag/v3.8.2
      with:
        node-version: 20
    - run: npm install
    - run: npm test

  docker:
    if: startsWith(github.ref, 'refs/tags/')  && !contains(github.ref, 'debug')
    runs-on: ubuntu-latest
    steps:
      -
        name: 'Extract tag name'
        shell: bash
        run: echo "tag=${GITHUB_REF##*/}" >> $GITHUB_OUTPUT
        id: extract_tag
      - # Needed for buildX, which accesses cwd
        name: Checkout
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 https://github.com/actions/checkout/releases/tag/v4.1.1
      - run: npm install --omit=dev --omit=optional
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@68827325e0b33c7199eb31dd4e31fbe9023e06e3 # v3.0.0 https://github.com/docker/setup-qemu-action/releases/tag/v3.0.0
      - # See note on build-push-action github repo on why this needed
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0 https://github.com/docker/setup-buildx-action/releases/tag/v3.0.0
      -
        name: Login to Docker Hub
        uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0 https://github.com/docker/login-action/releases/tag/v3.0.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and push Docker image
        uses: docker/build-push-action@4a13e500e55cf31b7a5d59a38ab2040ab0f42f56 # v5.1.0 https://github.com/docker/build-push-action/releases/tag/v5.1.0
        with:
          context: . # TODO: verify if needed (uses context of cwd instead of that of a fresh git checkout)
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ secrets.DOCKERHUB_USERNAME }}/simple-seeder:${{ steps.extract_tag.outputs.tag }}, ${{ secrets.DOCKERHUB_USERNAME }}/simple-seeder:latest
          build-args: TAG=${{ steps.extract_tag.outputs.tag }}

  daily_docker_build:
    if: ${{ github.event_name == 'schedule' || (startsWith(github.ref, 'refs/tags/') && !contains(github.ref, 'debug')) }}
    runs-on: ubuntu-latest
    steps:
      - # Needed for buildX, which accesses cwd
        name: Checkout
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 https://github.com/actions/checkout/releases/tag/v4.1.1
      - run: rm package-lock.json; npm install # We want the latest dependencies for the daily build
      - run: npm test
      - run: rm package-lock.json && npm install --omit=dev --omit=optional
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@68827325e0b33c7199eb31dd4e31fbe9023e06e3 # v3.0.0 https://github.com/docker/setup-qemu-action/releases/tag/v3.0.0
      - # See note on build-push-action github repo on why this needed
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0 https://github.com/docker/setup-buildx-action/releases/tag/v3.0.0
      -
        name: Login to Docker Hub
        uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0 https://github.com/docker/login-action/releases/tag/v3.0.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and push Docker image
        uses: docker/build-push-action@4a13e500e55cf31b7a5d59a38ab2040ab0f42f56 # v5.1.0 https://github.com/docker/build-push-action/releases/tag/v5.1.0
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ secrets.DOCKERHUB_USERNAME }}/simple-seeder:build-with-latest-deps
          build-args: TAG=build-with-latest-deps

  # To trigger a debug build including heapdump functionality,
  # push a tag containing 'debug'
  debug-docker:
    if: startsWith(github.ref, 'refs/tags/') && contains(github.ref, 'debug')
    runs-on: [ubuntu-latest]
    steps:
      -
        name: Checkout
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 https://github.com/actions/checkout/releases/tag/v4.1.1
      -
        name: install node
        uses: actions/setup-node@1a4442cacd436585916779262731d5b162bc6ec7 # v3.8.2 https://github.com/actions/setup-node/releases/tag/v3.8.2
        with:
          node-version: 20
      - run: npm install --omit=dev
        env:
          NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      -
        name: Login to container registry
        uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0 https://github.com/docker/login-action/releases/tag/v3.0.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and push Docker image
        uses: docker/build-push-action@4a13e500e55cf31b7a5d59a38ab2040ab0f42f56 # v5.1.0 https://github.com/docker/build-push-action/releases/tag/v5.1.0
        with:
          context: .
          file: Dockerfile-with-heapdump
          platforms: linux/amd64
          push: true
          tags: ${{ secrets.DOCKERHUB_USERNAME }}/simple-seeder:debug
