name: CI

on:
  pull_request:
  push:
    branches: develop

env:
  DOCKER_BUILDKIT: "1"
  DOCKER_IMAGE: "registry.revsys.com/boost"

jobs:
  test:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:12
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: postgres
        ports: ["5432:5432"]
        # options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

      redis:
        image: redis
        ports:
          - 6379:6379

    steps:
      - name: Git - Get Sources
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Set up Python 3.11
        uses: actions/setup-python@v4
        with:
          python-version: 3.11

      - uses: actions/cache@v3
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/base.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-
            ${{ runner.os }}-

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          python -m pip install -r requirements.txt
          sudo apt-get install -y ruby ruby-dev build-essential
          sudo gem install asciidoctor
        if: steps.cache.outputs.cache-hit != 'true'

      - name: Test with pytest
        env:
          DATABASE_URL: "postgres://postgres:postgres@localhost:${{ job.services.postgres.ports[5432] }}/postgres"
          SECRET_KEY: "for-testing-only"
          REDIS_HOST: "localhost"
        run: |
          python -m pytest

      - name: Lints with Black
        run: |
          python -m black --check .

  build:
    needs: [test]
    name: Build and Publish Docker image
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && github.ref == 'refs/heads/develop'

    steps:
      - name: Git - Get Sources
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Fetch Git Tags
        run: |
          git fetch --depth=1 origin +refs/tags/*:refs/tags/* || true

      - name: Set up Python 3.11
        uses: actions/setup-python@v4
        with:
          python-version: 3.11

      - name: Install Python dependencies
        run: |
          python -m pip install --upgrade pip
          python -m pip install vinnie

      - name: Bump and Tag our version
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          vinnie patch
          export VINNIE_VERSION=`vinnie version`
          git push --tags

      - name: Login to REVSYS Container Registry
        uses: docker/login-action@v2
        with:
          registry: registry.revsys.com
          username: boost
          password: ${{ secrets.REVSYS_DOCKER_REGISTRY_PASSWORD }}

      - name: Build Docker image
        run: |
          TAG=`vinnie version`

          docker build --file ./docker/Dockerfile \
            --build-arg TAG=${TAG} \
            --cache-from=${DOCKER_IMAGE}:latest \
            --tag ${DOCKER_IMAGE} .

      - name: Docker - Tag and Push
        run: |
          TAG=`vinnie version`

          docker tag ${DOCKER_IMAGE} ${DOCKER_IMAGE}:latest
          docker tag ${DOCKER_IMAGE} ${DOCKER_IMAGE}:${TAG}

          docker push ${DOCKER_IMAGE}:latest
          docker push ${DOCKER_IMAGE}:${TAG}
