stages:
  - build
  - push
  - tag
  - deploy-ci
  - deploy-stage

services:
  - docker:24.0.6-dind

variables:
  GIT_DEPTH: 256
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: ""
  DOCKER_HOST: tcp://docker:2375

workflow:
  rules:
    - if: $CI_COMMIT_TAG
      when: never
    - when: always

.tags: &tags
  tags:
    # - regsoft
    - gitlab-org-docker



build:
  <<: *tags
  artifacts:
    expire_in: 1h
    paths:
      - dist
  stage: build
  image: travissimon/nx-builder
  script:
    - npm install --only-prod --legacy-peer-deps
    - nx reset
    - nx --version
    - echo "typescript version `tsc --version`"
    - echo "Nodejs version `node --version`"
    - mkdir dist
    - sed -i 's/_LOCAL_ENVIRONMENT_/_CI_ENVIRONMENT_/g' libs/server-lib-core/src/lib/config-provider.ts
    - sed -i 's/_LOCAL_ENVIRONMENT_/_CI_ENVIRONMENT_/g' libs/shared-lib-core/src/lib/config-provider.ts
    - cp apps/webapp-roster/src/environments/environment.prod.ts apps/webapp-roster/src/environments/environment.ts
    - export base=`git rev-list -n 1 latest`
    - nx show projects --verbose --affected -p webapp-* service-* worker-* --base=$base --head=HEAD > dist/projects.txt
    - echo "Building:"
    - cat dist/projects.txt
    - |
      cat dist/projects.txt | while read project; do
        echo "building $project"
        nx build $project --configuration=production --verbose
      done

    - mv node_modules dist


push:
  <<: *tags
  artifacts:
    expire_in: 1h
    paths:
      - dist/projects.txt
  stage: push
  image: travissimon/nx-builder
  script:
    - export base=`git rev-list -n 1 latest`
    - export projects=`nx show projects --affected  -p *service dlp admin --base=$base --head=HEAD`
    - echo //registry.npmjs.org/:_authToken=$NPM_TOKEN > .npmrc
    - echo $SERVICE_ACCOUNT_JSON > /tmp/keyfile.json
    - echo $SERVICE_ACCOUNT_JSON | docker login -u _json_key --password-stdin https://australia-southeast1-docker.pkg.dev
    - echo "Pushing the following projects:"
    - cat dist/projects.txt
    - |
      cat dist/projects.txt | while read project; do
        echo "Executing post-build steps for $project"
        bash -c "deployment/$project/post-build.sh"
        echo "Creating container for $project"
        docker build \
           -t australia-southeast1-docker.pkg.dev/rare-ethos-332003/regsoft-repository/$project:$CI_COMMIT_SHA \
           -f deployment/$project/Dockerfile .
        echo "Pusing $project to Google container registry"
        docker push australia-southeast1-docker.pkg.dev/rare-ethos-332003/regsoft-repository/$project:$CI_COMMIT_SHA
      done

tag:
  <<: *tags
  artifacts:
    expire_in: 1h
    paths:
      - dist/projects.txt
  stage: tag
  image: travissimon/nx-builder
  before_script:
    # Deploy key in settings > repository
    - eval $(ssh-agent -s)
    - echo "$GITLAB_RUNNER_PRIVATE_KEY" | ssh-add - > /dev/null
    - mkdir -p ~/.ssh && chmod 700 ~/.ssh
    - echo "$GITLAB_RUNNER_PUBLIC_KEY" >> ~/.ssh/id_rsa.pub && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - ssh-keyscan -H $GITLAB_URL >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
    - git config --global user.email "gitlab-runner@regsoft.com.au"
    - git config --global user.name "Gitlab Runner"
  script:
    - echo "Tagging commit as latest build"
    - ssh git@$GITLAB_URL
    - mkdir tmp_git
    - git clone git@gitlab.com:regsoft/regsoft.git tmp_git && cd tmp_git
    # Push repo changes into current repo
    - git tag -d latest && git tag latest
    - git push origin latest --force


deploy-ci:
  <<: *tags
  artifacts:
    expire_in: 1h
    paths:
      - dist/projects.txt
  stage: deploy-ci
  image: travissimon/nx-builder
  script:
    - echo $SERVICE_ACCOUNT_JSON > /tmp/keyfile.json
    - echo $SERVICE_ACCOUNT_JSON | docker login -u _json_key --password-stdin https://australia-southeast1-docker.pkg.dev
    - export PATH=$PATH:/usr/node/google-cloud-sdk/bin
    - gcloud auth activate-service-account $SERVICE_ACCOUNT_EMAIL --key-file /tmp/keyfile.json --project rare-ethos-332003
    - gcloud container clusters get-credentials dlp-cluster --zone australia-southeast1-b --project rare-ethos-332003
    - export JWT=`echo $RANDOM | md5sum | head -c 20`
    - echo "Deploying the following projects to the CI environment:"
    - cat dist/projects.txt
    - |
      cat dist/projects.txt | while read project; do
        echo "Deploying $project to ci"
        pushd .
        cd deployment/$project/helm-chart/
        helm upgrade --install --namespace ci --set deployNamespace=ci,image.tag=$CI_COMMIT_SHA,commitSha=$CI_COMMIT_SHA,jwt=$JWT,buildTimestamp=`date --iso-8601=seconds` $project $project
        popd
        echo "Deployed $project to ci"
        echo " "
      done

deploy-stage:
  <<: *tags
  artifacts:
    paths:
      - dist/projects.txt
  stage: deploy-stage
  when: manual
  image: travissimon/nx-builder
  script:
    - echo $SERVICE_ACCOUNT_JSON > /tmp/keyfile.json
    - echo $SERVICE_ACCOUNT_JSON | docker login -u _json_key --password-stdin https://australia-southeast1-docker.pkg.dev
    - export PATH=$PATH:/usr/node/google-cloud-sdk/bin
    - gcloud auth activate-service-account $SERVICE_ACCOUNT_EMAIL --key-file /tmp/keyfile.json --project rare-ethos-332003
    - gcloud container clusters get-credentials dlp-cluster --zone australia-southeast1-b --project rare-ethos-332003
    - echo "Deploying the following projects to Staging:"
    - cat dist/projects.txt
    - |
      cat dist/projects.txt | while read project; do
        echo "Deploying $project to ci"
        pushd .
        cd deployment/$project/helm-chart/
        helm upgrade --install --namespace staging --set deployNamespace=staging,image.tag=$CI_COMMIT_SHA,commitSha=$CI_COMMIT_SHA,buildTimestamp=`date --iso-8601=seconds` $project $project
        popd
        echo "Deployed $project to ci"
        echo " "
      done
