variables:
  GITLAB_TOKEN: '${CI_JOB_TOKEN}'
  NODE_IMAGE_NAME: ${CODE_REGISTRY_IMAGE}/${CI_PROJECT_NAME}:${CI_COMMIT_REF_SLUG}--node

image: ${CI_REGISTRY}/devops/images/usgs/node:lts

# Do not run for merge requests
workflow:
  rules:
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_BRANCH

stages:
  - init
  - node-image
  - build
  - test
  - publish

default:
  tags:
    - nshmp

####
# Stage: init
####

Init:
  artifacts:
    paths:
      - node_modules
      - example/node_modules
  script:
    - npm ci
    - cd example
    - npm ci
  stage: init

####
# Stage: Node Image
####

Build Node Image:
  image: ${CODE_REGISTRY}usgs/docker:20
  needs: []
  script:
    - |
      docker build \
        --build-arg FROM_IMAGE=${CI_REGISTRY}/devops/images/usgs/node:lts \
        --file "Dockerfile" \
        --pull \
        --tag ${NODE_IMAGE_NAME} \
        .
    - docker push ${NODE_IMAGE_NAME}
  services:
    - alias: docker
      name: ${CI_REGISTRY}/devops/images/usgs/docker:20-dind
  stage: node-image
  tags:
    - build
  variables:
    DOCKER_DRIVER: overlay2

####
# Stage: Build
####

Audit:
  allow_failure: true
  needs: []
  script:
    - npm audit
  stage: build

Audit Angular:
  allow_failure: true
  needs: []
  script:
    - cd example
    - npm audit
  stage: build

Build Angular:
  needs:
    - Init
  script:
    - cd example
    - npm run build
  stage: build

Lint Angular:
  needs:
    - Init
  script:
    - cd example
    - npm run lint
  stage: build

Lint Project:
  needs:
    - Init
  script:
    - npm run lint
  stage: build

####
# Stage: Test
####

Angular Unit Tests:
  artifacts:
    paths:
      - example/coverage/example
    reports:
      junit: example/junit.xml
  needs:
    - Init
  script:
    - cd example
    - npm run test
  stage: test

####
# Stage: Publish
####

Publish npm:
  image: ${NODE_IMAGE_NAME}
  only:
    - tags
  needs:
    - Audit
    - Audit Angular
    - Init
    - Build Angular
    - Lint Project
  script:
    - git config user.email "${GITLAB_USER_EMAIL}"
    - git config user.name "${GITLAB_USER_NAME}"
    - |
      cat <<-EO_CONFIG > .npmrc
      @${CI_PROJECT_ROOT_NAMESPACE}:registry=https://${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/
      //${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}
      EO_CONFIG
    - npx standard-version --release-as ${CI_COMMIT_TAG} --skip.commit --skip.changelog
    - npm publish --access public
  stage: publish
