image: docker:latest

# Define the GitLab CI/CD pipeline
stages:
  - build
  - test

variables:
  POSTGRES_DB: test
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: ""
  POSTGRES_HOST_AUTH_METHOD: trust

# Define the job for building the Go backend application
build:
  stage: build
  image: golang:1.17.4-alpine3.14
  script:
    - cd src/backend/cmd
    - go mod download
    - go build -o app

# Define the job for running the tests
test:
  stage: test
  image: golang:1.17.4-alpine3.14
  services:
    - postgres:latest
  variables:
    Host: postgres
    User: $POSTGRES_USER
    Password: $POSTGRES_PASSWORD
    Database: $POSTGRES_DB
    POSTGRES_HOST_AUTH_METHOD: trust
  before_script:
    - apk add --no-cache postgresql-client
    - apk add --no-cache gcc musl-dev
    - psql -U "$POSTGRES_USER" -h postgres < ./src/init.sql
    - cd src/backend
    - go mod download
    # set env variables
    - export DB_USER=$POSTGRES_USER
    - export DB_PASSWORD=$POSTGRES_PASSWORD
    - export DB_NAME=$POSTGRES_DB
  script:
    - go test -v ./...
