stages:
  - test-build
  - Static analysis
  - build
  - deploy
  
variables:
  GIT_DEPTH: 0

test build:
  stage: test-build
  image: node:14
  script:
    - npm install
    - npm run build
  except:
    - master
    - develop
    - ^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$

build testing docker image:
  stage: build
  only:
    - develop
  tags:
    - build_docker
  variables:
    DOCKER_TAG: testing
  script:
    - cat $DOCKER_PASSWORD | docker login --username $DOCKER_LOGIN --password-stdin
    - docker-compose build geocontrib-front
    - docker-compose push geocontrib-front
    - echo Image docker neogeo/geocontrib-front:${DOCKER_TAG} livrée

deploy testing docker image:
  stage: deploy
  only:
    - develop
  tags:
    - build
  image:
    name: curlimages/curl
  script:
    - curl -X POST      -F token=$TRIGGER_TOKEN -F ref=main      https://git.neogeo.fr/api/v4/projects/226/trigger/pipeline

build stable docker image:
  stage: build
  only:
    - master
  tags:
    - build_docker
  variables:
    DOCKER_TAG: latest
  script:
    - cat $DOCKER_PASSWORD | docker login --username $DOCKER_LOGIN --password-stdin
    - docker-compose build geocontrib-front
    - docker-compose push geocontrib-front
    - echo Image docker neogeo/geocontrib-front:${DOCKER_TAG} livrée

build tagged docker image:
  stage: build
  only:
    - tags
  tags:
    - build_docker
  variables:
    DOCKER_TAG: $CI_COMMIT_TAG
  script:
    # Don't build tag id package.json as wrong version
    - grep "\"version\":.\"$CI_COMMIT_TAG\"" package.json
    - cat $DOCKER_PASSWORD | docker login --username $DOCKER_LOGIN --password-stdin
    - docker-compose build geocontrib-front
    - docker-compose push geocontrib-front
    - echo Image docker neogeo/geocontrib-front:${DOCKER_TAG} livrée

sonarqube-check:
  image:
    name: sonarsource/sonar-scanner-cli:latest
    entrypoint: [""]
  only:
    - develop
  stage: Static analysis
  script:
    - sonar-scanner -Dsonar.qualitygate.wait=true -Dsonar.projectKey=id-$CI_PROJECT_ID -Dsonar.projectName="$CI_PROJECT_PATH" -Dsonar.projectVersion=$CI_COMMIT_BRANCH -Dsonar.coverage.exclusions=**/src/**/*
