name: ci

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  main:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false

    services:
      postgres:
        image: postgres:15
        env:
          POSTGRES_USER: odoo
          POSTGRES_PASSWORD: odoo
          POSTGRES_DB: odoo
        ports:
          - 5432:5432
    steps:
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Login to ethumada registry
        uses: docker/login-action@v3
        with:
          registry: registry.ethumada.com
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      - name: Checkout
        uses: actions/checkout@v4
      - name: Build image
        uses: docker/build-push-action@v6
        with:
          build-args: |
            codename=${{ vars.CODENAME }}
            python_version=${{ vars.PYTHON_VERSION }}
            odoo_version=${{ vars.ODOO_VERSION }}
            odoo_org_repo=${{ vars.ODOO_ORG_REPO }}
          tags: |
            ${{ vars.TAGS }}
          load: true
      - name: Create Container
        run: |
          docker create --name test --network=host ${{ vars.TAGS }}:latest
      - name: Copy Tests into Container
        run: |
          docker cp ./tests test:/tests
      - name: Run Tests
        run: |
          docker start test
          docker exec test chmod +x /tests/runtests.sh
          docker exec test /tests/runtests.sh
      - name: Cleanup
        run: |
          docker stop test
          docker rm test
      - name: Push image
        uses: docker/build-push-action@v6
        with:
          build-args: |
            codename=${{ vars.CODENAME }}
            python_version=${{ vars.PYTHON_VERSION }}
            odoo_version=${{ vars.ODOO_VERSION }}
            odoo_org_repo=${{ vars.ODOO_ORG_REPO }}
          tags: |
            ${{ vars.TAGS }}
          push: true
