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

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

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

default:
  tags:
    - nshmp

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

Init:
  artifacts:
    paths:
      - node_modules
  image: ${DEVOPS_REGISTRY}usgs/node:16
  script:
    - npm ci
  stage: init

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

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

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

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

Build Angular:
  image: ${DEVOPS_REGISTRY}usgs/node:16
  needs:
    - Init
  script:
    - cd example
    - npm i
    - npm run build
  stage: build

Lint Project:
  image: ${DEVOPS_REGISTRY}usgs/node:16
  needs:
    - Init
  script:
    - npm run lint
  stage: build

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

Publish npm:
  image: ${NODE_IMAGE_NAME}
  only:
    - tags
  needs:
    - Audit
    - 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