name: "Test and publish"

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:
    name: "Test and publish"
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      #Prepare node environment
      - name: "Checkout source code"
        uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - name: Read Node.js version from .nvmrc
        id: read_nvmrc
        run: |
          echo "::set-output name=node_version::$(cat .nvmrc)"
        
      - name: "Setup Node.js"
        uses: actions/setup-node@v4
        with:
          node-version: ${{ steps.read_nvmrc.outputs.node_version }}
          registry-url: 'https://registry.npmjs.org'

      - name: Install Compose
        uses: ndeloof/install-compose-action@v0.0.1
        with:
          legacy: true    # will also install in PATH as `docker-compose`

      - name: Check docker-compose is installed
        run: docker-compose -v

      #Run tests
      - run: npm ci
      - run: npm test

      #Increase version in main branch
      - name: "Automated Version Bump"
        if: github.ref == 'refs/heads/master'
        id: version-bump
        uses: "phips28/gh-action-bump-version@master"
        with:
          tag-prefix: ''
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      #Publish to npm in main branch
      - run: npm publish
        if: github.ref == 'refs/heads/master'
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Login to Docker Hub
        if: github.ref == 'refs/heads/master'
        run: |
          DOCKERHUB_USERNAME=$(node -p "require('./package.json').dockerHubUsername")
          echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
          
      - name: Build and push Docker images
        if: github.ref == 'refs/heads/master'
        run: |
          ./publish-on-docker-hub.sh "http-api"
          ./publish-on-docker-hub.sh "lambda-function"
