stages:
  - test
  - quality_check
  - build

variables:
  NAMESPACE: "default"
  REGISTRY_GCP: europe-west1-docker.pkg.dev/ops-shared-e3afff9d/gitlab-pipeline
  IMAGE_NAME: "${REGISTRY_GCP}/${APPLICATION_NAME}"

.test:
  stage: test
  tags:
    - docker
  image: eu.gcr.io/docker-images-237413/prod/node-gcloud-components-image:Node.10.19-Gcloud.155.0.0
  variables:
    JWT_PUB_KEY: ${JWT_PUB_CONTENT}
    # MIGRATIONS 
    MYSQL_DATABASE: test_db
    MYSQL_ROOT_PASSWORD: rootpass
    MYSQL_USER: test_db_usr
    MYSQL_PASSWORD: test_db_usr_pass
  script:
    - git clone https://${CI_TEMPLATES_GIT_USER}:${CI_TEMPLATES_GIT_PASSWORD}@${CI_TEMPLATES_GIT_PROJECT_URL}
    - cd ci-templates
    - scripts/notify-slack-start-pipeline.sh
    - cd ../
    - export GOOGLE_APPLICATION_CREDENTIALS=.gcpserviceaccount.json
    - echo MARCO_API_PARENT_URL = ${MARCO_API_PARENT_URL}
    - node -p "require('./package.json').version" 2>/dev/null || echo "0.0.0"
    - npm install yarn@1.21.1
    - echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc
    - npm run presetup
    - npm start test.unit & npm start test.integration && npm start test.e2e
  artifacts:
    paths:
      - coverage/

quality_check:
  image: europe-west1-docker.pkg.dev/ops-shared-e3afff9d/sync-images/sonarsource/sonar-scanner-cli:stable
  stage: quality_check
  tags:
    - docker
  script:
    - apk add --update bash git curl gettext nodejs npm
    - TAG_VERSION=$(node -p "require('./package.json').version" 2>/dev/null || echo "0.0.0")
    - echo TAG_VERSION=${TAG_VERSION}
    - >
      sonar-scanner
      -Dsonar.projectKey=${APPLICATION_NAME}
      -Dsonar.projectVersion=${TAG_VERSION}
      -Dsonar.host.url=${SONAR_ENDPOINT}
      -Dsonar.login=${SONAR_TOKEN}
      -Dsonar.scm.provider=git
      -Dsonar.qualitygate.wait=true
      -Dsonar.javascript.lcov.reportPaths=coverage/lcov.info
  allow_failure: false

.build:
  stage: build
  tags:
    - docker
  image: europe-west1-docker.pkg.dev/ops-shared-e3afff9d/sync-images/finboot-custom/docker-git:stable
  script:
    - apk add gettext git curl
    - apk add --update nodejs npm
    - TAG_VERSION=$(node -p "require('./package.json').version" 2>/dev/null || echo "0.0.0")
    #- TAG_IMAGE=$(echo ${TAG} | sed 's/\//-/g;s/_/-/g')
    - docker login -u _json_key -p "$SA_GCR" https://europe-west1-docker.pkg.dev
    - cat Dockerfile.subst | envsubst > Dockerfile | docker build --no-cache -t "${IMAGE_NAME}:${TAG_VERSION}-${CI_COMMIT_SHORT_SHA}" -t "${IMAGE_NAME}:latest" -f Dockerfile .
    - docker push "${IMAGE_NAME}:${TAG_VERSION}-${CI_COMMIT_SHORT_SHA}"
    - docker push "${IMAGE_NAME}:latest"
    - EXIT_STATUS=$?
    # CREATE TAG
    - git config user.email "${GITLAB_USER_EMAIL}"
    - git config user.name "${GITLAB_USER_NAME}"
    - echo "Debug gitlab.com/${CI_PROJECT_PATH}"
    - git tag
    - git remote set-url origin https://oauth2:${GITLAB_ACCESS_TOKEN}@gitlab.com/${CI_PROJECT_PATH}
    # Check if not exist this tag
    - |
      if git rev-parse "${TAG_VERSION}" >/dev/null 2>&1;
      then
        echo "Tag Version $VERSION already exists"
      else
        git tag -a "${TAG_VERSION}" -m "Version created by gitlab-ci Build"
        git push origin "${TAG_VERSION}"
      fi
    - echo Image uploaded "${IMAGE_NAME}:${TAG_VERSION}-${CI_COMMIT_SHORT_SHA}"
    - echo Image uploaded "${IMAGE_NAME}:latest"
    - git clone https://${CI_TEMPLATES_GIT_USER}:${CI_TEMPLATES_GIT_PASSWORD}@${CI_TEMPLATES_GIT_PROJECT_URL}
    - cd ci-templates
    - scripts/notify-slack-deploy.sh ${EXIT_STATUS}

build_staging:
  stage: build
  extends: .build_staging_main

build_production:
  stage: build
  extends: .build_production_main

.build_staging_main:
  stage: build
  extends: .build
  environment:
    name: staging
  except:
    - master
    - main

.build_production_main:
  stage: build
  extends: .build
  environment:
    name: production
  only:
    - master
    - main

.test_staging_main:
  stage: test
  extends: .test
  environment:
    name: staging
  variables:
    MARCO_API_PARENT_URL: https://api-marco.finboot-test.com
  except:
    - master
    - main

.test_production_main:
  stage: test
  extends: .test
  environment:
    name: production
  variables:
    MARCO_API_PARENT_URL: https://api-marco.finboot.com
  only:
    - master
    - main
