---
stages:
  - tests
  - build

run tests:
  stage: tests
  image: golang:alpine
  cache:
    - key:
        files:
          - go.sum
      paths:
        - .go-cache/
    - key:
        files:
          - console/frontend/pnpm-lock.yaml
      paths:
        - .pnpm-store
  script:
    # Tests on GitLab do not include functional tests. This was the case in the
    # past but this is a slight burden to maintain in addition to GitHub CI.
    # Check commit ceaa6ebf8ef6 for the last version supporting functional
    # tests.
    - time apk add --no-cache git make gcc musl-dev shared-mime-info pnpm curl zip
    - export GOMODCACHE=$PWD/.go-cache
    - pnpm config set store-dir .pnpm-store
    - time go mod download
    - time make && ./bin/akvorado version
    - time make test-race
    - time make test-js
    - time make test-coverage
  coverage: '/Code coverage: [0-9.]+/'
  # Coverage reports were removed

# To execute manually:
#  gitlab-runner exec docker "run tests"

build docker image:
  stage: build
  image: docker:stable
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
    - if: $CI_MERGE_REQUEST_ID
  script:
    - docker info
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build -f docker/Dockerfile -t $CI_REGISTRY_IMAGE:$CI_COMMIT_BRANCH$CI_MERGE_REQUEST_ID .
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_BRANCH$CI_MERGE_REQUEST_ID
