# For this pipeline to work the following parameters must be defined in the repository scope on bitbucket:
# NPM_TOKEN, Example "npm_SCuYMLXXXXXXXXXXXX" (The name that will be printed in the logs)
image: node:20

definitions:
    services:
        docker:
            memory: 3072
            type: docker
    steps:
        - step: &lint
              name: Linting
              caches:
                  - node
              script:
                  - echo "Linting Started..."
                  - npm install
                  - npm run lint:fix
                  - echo "Linting for all .js files in repository completed"

        - step: &test
              name: Testing
              caches:
                  - node
              script:
                  - echo "Testing Started..."
                  - apt-get update && apt-get install -y
                  - apt-get install -yq gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libgdk-pixbuf2.0-dev libglib2.0-0 libgtk-3-0 libgtk-3-dev libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libnss3 libnss3-dev libxss-dev lsb-release xdg-utils wget
                  - npm install
                  - npm i @rollup/rollup-linux-x64-gnu
                  - git add .
                  - git commit -m "Update from CICD [skip ci]"
                  - git push
                  - git status
                  - npx puppeteer browsers install chrome
                  - npm run test
                  - echo "Testing completed"

        - step: &build-storybook
              name: Build Storybook
              caches:
                  - node
              script:
                  - echo "NPM build started..."
                  - npm install
                  - npm run build:bundle
                  - npm i @storybook/react react react-dom
                  - npm run build:storybook
                  - ls -lR dist # identify all files inside dist directory
                  - echo "NPM package building completed"
              artifacts:
                  - .storybook/**
                  - dist/**
                  - src/**
                  - package.json
                  - package-lock.json

        - step: &build-package
              name: Build NPM Package
              caches:
                  - node
              script:
                  - echo "NPM build started..."
                  - npm install
                  - git status
                  - npm version patch -m "Upgrade package version to %s [skip ci]"
                  - git push
                  - npm run build:bundle
                  - npm run build:types
                  - npm run build:documentation
                  - npm i @storybook/react react react-dom
                  - npm run build:storybook
                  - ls -lR dist # identify all files inside dist directory
                  - echo "NPM package building completed"
              artifacts:
                  - .storybook/**
                  - dist/**
                  - src/**
                  - package.json
                  - package-lock.json

        - step: &build-test
              name: Build bundle & Test
              caches:
                  - node
              script:
                  - echo "NPM build and test step started..."
                  - npm install
                  - npm run build:bundle
                  - apt-get update && apt-get install -y
                  - apt-get install -yq gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libgdk-pixbuf2.0-dev libglib2.0-0 libgtk-3-0 libgtk-3-dev libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libnss3 libnss3-dev libxss-dev lsb-release xdg-utils wget
                  - npx puppeteer browsers install chrome
                  - npm run test
                  - echo "Building and testing completed"

        - step: &build-image
              name: Build Docker Image
              script:
                  - docker build -t dockerimage -f Dockerfile .
                  - docker save --output tmp-image.docker dockerimage
              services:
                  - docker
              artifacts:
                  - tmp-image.docker

        - step: &push-gcp
              name: Push to GCP artifact registry
              image: google/cloud-sdk:alpine
              script:
                  - source build.env
                  - echo ${TAG}
                  - docker load --input ./tmp-image.docker
                  # Authentication using key file
                  - echo $GCLOUD_API_KEYFILE | base64 -d > ./gcloud-api-key.json
                  - gcloud auth activate-service-account --key-file gcloud-api-key.json
                  - gcloud config set project $GCLOUD_PROJECT_ID
                  - export IMAGE_NAME=${GCP_REGISTRY_HOSTNAME}/${GCP_PROJECT_NAME}/${GCP_REPOSITORY_NAME}/${GCP_IMAGE_NAME}:${TAG}
                  - docker tag dockerimage ${IMAGE_NAME}
                  # Login and push the image
                  - cat ./gcloud-api-key.json | docker login -u _json_key --password-stdin https://${GCP_REGISTRY_HOSTNAME}
                  - docker push ${IMAGE_NAME}
              services:
                  - docker

        - step: &deploy-gcp
              name: Deploy to GCP Cloud Run
              image: google/cloud-sdk:alpine
              script:
                  - source build.env
                  - echo ${TAG}
                  # Authentication using key file
                  - echo $GCLOUD_API_KEYFILE | base64 -d > ./gcloud-api-key.json
                  - gcloud auth activate-service-account --key-file gcloud-api-key.json
                  - gcloud config set project $GCLOUD_PROJECT_ID
                  - export IMAGE_NAME=${GCP_REGISTRY_HOSTNAME}/${GCP_PROJECT_NAME}/${GCP_REPOSITORY_NAME}/${GCP_IMAGE_NAME}:${TAG}
                  # Deploy instance. TAG should only be "staging" or "production" at this point.
                  # We have to set the project flag to the project ID rather than the project number.
                  - export INSTANCE_NAME=cloudrun-${TAG}-${GCP_IMAGE_NAME}
                  - gcloud run deploy $INSTANCE_NAME --project $GCP_PROJECT_NAME --region $GCP_REGION --image $IMAGE_NAME

        - step: &publish-package
              name: Publish npm package
              caches:
                  - node
              deployment: production
              script:
                  - pipe: atlassian/npm-publish:0.3.2
                    variables:
                        NPM_TOKEN: ${NPM_TOKEN}

pipelines:
    pull-requests:
        "**":
            # - step: *lint
            - step: *build-test

    branches:
        master:
            #           - step: *lint
            - step: *build-package
            - step: *build-test
            - step: *publish-package

    custom:
        deploy-to-staging:
            - step:
                  name: "Configure Build Variables"
                  script:
                      #- echo "export TAG=\"$(npm run version --silent)\"" >> build.env
                      - echo "export TAG=\"staging\"" >> build.env
                  artifacts:
                      - build.env
            #           - step: *lint
            - step: *build-image
            - step: *push-gcp
            - step: *deploy-gcp
