include:
  - project: 'airwallex-web/web-sdk-doc'
    ref: master
    file: '/.gitlab-ci-domain-template.yml'

variables:
  SONAR_LOGIN: $SONAR_TOKEN
  PUSH_REPO: "https://gitlab.awx.im/acquiring/airwallex-payment-elements.git"
  GITLAB_ONLY_REPO: "true"
  DOC_PATH_PREFIX: "payment-elements"
  GCS_UPLOAD_TAG: "acquiring"
  ADDITIONAL_TAG: "nonpci"
  REMOTE_FILE_NAME: "payments.js"
  REMOTE_FILE_PATH: "./docs"
cache:
  key:
    files:
      - yarn.lock
      - package.json
  paths:
    - .yarn
    - node_modules

.upload_types_script: &upload_types_script
  - gsutil cp dist/types.d.ts gs://${BUCKET_NAME}/temp/types/types.d.ts
  - gsutil -m setmeta -r -h "Cache-Control:no-store,no-cache,max-age=0" gs://${BUCKET_NAME}/temp/types/types.d.ts
  - gsutil -m rsync -r gs://${BUCKET_NAME}/temp/ gs://${BUCKET_NAME}/
  - gsutil -m rm -r gs://${BUCKET_NAME}/temp


stages:
  - test
  - build
  - deploy-doc-dev
  - deploy-doc-staging
  - publish-gitlab-package
  - build-types
  - upload-types-staging
  - approve
  - deploy-doc-demo
  - deploy-doc-prod
  - publish-npm-package
  - upload-types-prod

# Unit Test
test:
  coverage: /All\sfiles.*?\s+(\d+.\d+)/
  image: node:18-alpine
  variables:
    KUBERNETES_MEMORY_REQUEST: 8Gi
    KUBERNETES_MEMORY_LIMIT: 8Gi
    KUBERNETES_CPU_REQUEST: 8
    KUBERNETES_CPU_LIMIT: 8
  stage: test
  rules:
    - if: '$CI_COMMIT_MESSAGE !~ /^chore\(release\):/'
  script:
    - yarn
    - yarn test
  artifacts:
    paths:
      - coverage

build:
  image: node:18-alpine
  stage: build
  script:
    - yarn build
  rules:
    - if: '$CI_COMMIT_MESSAGE !~ /^chore\(release\):/'
  artifacts:
    paths:
      - dist
      - lib
      - public

# Publish github package
publish-gitlab-package:
  image: node:18-alpine
  stage: publish-gitlab-package
  variables:
    GIT_GLOBAL_USER_EMAIL: "devops@airwallex.com"
    GIT_GLOBAL_USER: "Gitlab Runner"
    GIT_REPO: "https://gitlab-ci-token:${GITLAB_TOKEN}@gitlab.awx.im/acquiring/airwallex-payment-elements.git"
  rules:
    - if: '$CI_COMMIT_REF_NAME == "master" && $CI_COMMIT_MESSAGE !~ /^chore\(release\):/'
  when: manual
  before_script:  
    - apk update
    - apk add --no-cache git bash curl perl
    - which ssh-agent || ( apk update && apk add openssh-client )
    - yarn --pure-lockfile --ignore-engines --cache-folder .yarn
    - eval $(ssh-agent -s)
    - echo "${SSH_PRIVATE_KEY}" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - git config --global user.email ${GIT_GLOBAL_USER_EMAIL?}
    - git config --global user.name ${GIT_GLOBAL_USER?}
  script:
    - ls
    - git remote set-url origin "${GIT_REPO}"
    - yarn semantic-release
  needs:
    - job: build

# Publish npm package
publish-npm-package:
  image: node:18-alpine
  stage: publish-npm-package
  when: manual
  needs: []
  only:
    - tags
  script:
    - ls
    - yarn build
    - rm .npmrc
    - sed -i 's/@airwallex\/airwallex-payment-elements/airwallex-payment-elements/g' package.json
    - cat package.json
    - ls
    - echo "always-auth=true" >> .npmrc
    - echo "registry=https://registry.npmjs.org/" >> .npmrc
    - echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >> .npmrc
    - cat .npmrc
    - npm publish

build-types:
  image: node:18-alpine
  stage: build-types
  variables:
    KUBERNETES_CPU_REQUEST: 12
    KUBERNETES_CPU_LIMIT: 12
    KUBERNETES_MEMORY_REQUEST: 12Gi
    KUBERNETES_MEMORY_LIMIT: 12Gi
  only:
    - tags
  script:
    - yarn
    - yarn bundle-types || true
  artifacts:
    paths:
      - dist

upload-types-staging:
  image: gcr.io/google.com/cloudsdktool/cloud-sdk:alpine
  tags:
    - acquiring
    - staging
    - pci
  only:
    - tags
  when: manual
  script:
   - *upload_types_script 
  stage: upload-types-staging
  variables:
    BUCKET_NAME: checkoutui-staging
  needs:
    - job: build-types
      artifacts: true

upload-types-prod:
  image: gcr.io/google.com/cloudsdktool/cloud-sdk:alpine
  tags:
    - acquiring
    - prod
    - pci
  only:
    - tags
  when: manual
  script:
   - *upload_types_script 
  stage: upload-types-prod
  variables:
    BUCKET_NAME: checkoutui-prod
  needs:
    - job: build-types
      artifacts: true