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

include:
  - project: 'ghsc/hazdev/pipeline-build-template'
    ref: '1.1.3'
    file: 'templates/library.yml'

stages:
  - node-image
  - test
  - publish

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

####
# Template: GitLab runner tags
####
.dev-tags:
  tags:
    - development

####
# Template: Run Node
####

.node:
  image: ${NODE_IMAGE_NAME}
  extends:
    - .tags::development
  before_script:

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

Build Node Image:
  extends:
    - .dind
    - .tags::build
  script:
    - |
      docker build \
        --build-arg FROM_IMAGE=${DEVOPS_REGISTRY}usgs/node:latest \
        --file "Dockerfile" \
        --pull \
        --tag ${NODE_IMAGE_NAME} \
        .
    - docker push ${NODE_IMAGE_NAME}
  stage: node-image

####
# Stage: test
####

Build Angular:
  stage: test
  extends: .node
  script:
    - cd example
    - npm i
    - npm run build

Lint:
  stage: test
  extends: .node
  script:
    - npm run tslint

####
# Stage: publish
####

Publish npm:
  stage: publish
  image: ${DEVOPS_REGISTRY}usgs/node:12
  extends: .dev-tags
  only:
    - tags@ghsc/nshmp/disagg-d3
  before_script:
    - echo '//code.usgs.gov/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}' >> .npmrc
    - npm ci
    - git config user.email "${GITLAB_USER_EMAIL}"
    - git config user.name "${GITLAB_USER_NAME}"
  script:
    - npx standard-version --release-as ${CI_COMMIT_TAG} --skip.commit --skip.changelog
    - npm publish --access public
