# publish both npm package and docker build

# Publish npm package if commit has a version tag or is production branch.
# Publish only if version changed.
# Publish tagged "beta" if version contains beta (e.g. v2.0.0-beta.0)
#
# The original template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/npm.gitlab-ci.yml

npm-publish:
  image: node:latest
  stage: deploy
  rules:
    - if: $CI_COMMIT_REF_NAME =~ /^v\d+\.\d+\.\d+.*$/ || $API_JOB_NAME == $CI_JOB_NAME
      changes:
        - package.json
  script:
    # Fetch latest client build artifact and unzip
    - apt update && apt install -y unzip
    - curl --location --output artifacts.zip "https://gitlab.com/machina_ex/adaptor_ex/adaptor_ex_client/-/jobs/artifacts/production/download?job=build-site"
    - unzip artifacts.zip
    - mv dist/* public
    - rm artifacts.zip

    # Extract a few values from package.json
    - NPM_PACKAGE_NAME=$(node -p "require('./package.json').name")
    - NPM_PACKAGE_VERSION=$(node -p "require('./package.json').version")

    # set auth token
    - npm config set //registry.npmjs.org/:_authToken ${NPM_TOKEN}

    # Compare the version in package.json to all published versions.
    # If the package.json version has not yet been published, run `npm publish`.
    # If package version is "beta" prerelease publish with "beta" tag otherwise publish latest
    - |
      if [[ "$(npm view ${NPM_PACKAGE_NAME} versions)" != *"'${NPM_PACKAGE_VERSION}'"* ]]; then
        if [[ "$NPM_PACKAGE_VERSION" =~ -beta* ]]; then
          npm publish --tag=beta
        else
          npm publish
        fi
        echo "Successfully published version ${NPM_PACKAGE_VERSION} of ${NPM_PACKAGE_NAME} to GitLab's NPM registry: ${CI_PROJECT_URL}/-/packages"
      else
        echo "Version ${NPM_PACKAGE_VERSION} of ${NPM_PACKAGE_NAME} has already been published, so no new version has been published."
      fi


# Build a Docker image with CI/CD and push to the GitLab registry.
# Docker-in-Docker documentation: https://docs.gitlab.com/ee/ci/docker/using_docker_build.html
#
# This template uses one generic job with conditional builds
# for the default branch and all other (MR) branches.

docker-build:
  # Use the official docker image.
  image: docker:latest
  stage: build
  # Run this job for tags only. If tag is version number, build 'latest' container also (tag like v0.0.0)
  only:
    - tags
  services:
    - docker:dind
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  # tagged with production version number (commit tag)
  script:
    - docker build --pull -t "$CI_REGISTRY_IMAGE" -t "$CI_REGISTRY_IMAGE:${CI_COMMIT_TAG}" .
    - VERSION_PATTERN="^v([0-9]+\.){0,2}(\*|[0-9]+)$"
    - |
      if [[ "$CI_COMMIT_TAG" =~ $VERSION_PATTERN ]]; then
        docker push "$CI_REGISTRY_IMAGE"
        echo "Tag is version number. Build also with tag 'latest'"
      else
        echo "Tag '$CI_COMMIT_TAG' is not a valid version number. Push with tag '$CI_COMMIT_TAG' only"
      fi
    - docker push "$CI_REGISTRY_IMAGE:${CI_COMMIT_TAG}"
