#  Template NodeJS Web Application for Azure Web App service

#  This template allows you to deploy your NodeJS web application to Azure Web App Service.
#  The workflow allows running tests, code linting and security scans on feature branches (as well as master).
#  The NodeJS application will be validated after the code is merged to master and deployment can be triggered manually.

# Prerequisites: $$AZURE_APP_ID, $AZURE_PASSWORD, $AZURE_TENANT_ID, $AZURE_RESOURCE_GROUP, $AZURE_APP_NAME setup in the Deployment variables.
# For advanced cases, please, follow examples from the pipe's README https://bitbucket.org/microsoft/azure-web-apps-deploy/src/1.0.3/README.md


image: node:16

definitions:
  steps:
    - step: &webhook-fail
        after-script:
          - BUILD_STATUS="successful"
          - if [[ BITBUCKET_EXIT_CODE -eq 0 ]]; then exit 0; else BUILD_STATUS="failed"; fi
          - pipe: atlassian/slack-notify:2.0.0
            variables:
              WEBHOOK_URL: $WEBHOOK_URL
              MESSAGE: '*$BITBUCKET_REPO_SLUG* - *$BITBUCKET_BRANCH* $BUILD_NAME has exited with status "*${BUILD_STATUS}*"'
    - step: &start-deploy
        name: Start deploy
        script:
          - pipe: atlassian/slack-notify:2.0.0
            variables:
              WEBHOOK_URL: $WEBHOOK_URL
              MESSAGE: '*$BITBUCKET_REPO_SLUG* - *$BITBUCKET_BRANCH* Start Deploy'
    - step: &build
        <<: *webhook-fail
        name: Build
        caches:
          - node
        script:
          - npm install
          - npm run build:ts
          - BUILD_NAME="Build"
          - curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
          - chmod +x ./kubectl
        artifacts:
          - dist/**
          - kubectl
    - step: &docker-build
        <<: *webhook-fail
        name: Docker Build
        services:
          - docker
        caches:
          - node
        script:
          - docker build -t "$REPO_NAME:$REPO_TAG" --build-arg SSH_KEY="$KEY_GITHUB" .
          - docker tag "$REPO_NAME:$REPO_TAG" "docker.adaoncloud.com/$REPO_NAME:$REPO_TAG"
          - mkdir my_auth && echo $DOCKER_REPO_AUTH > ./my_auth/config.json
          - docker --config ./my_auth push "docker.adaoncloud.com/$REPO_NAME:$REPO_TAG"
          - BUILD_NAME="Docker Build"
    - step: &security-scan
        <<: *webhook-fail
        name: Security Scan
        script:
          # Run a security scan for sensitive data.
          # See more security tools at https://bitbucket.org/product/features/pipelines/integrations?&category=security
          - BUILD_NAME="Security Scan"
          - pipe: atlassian/git-secrets-scan:0.5.1
    - step: &deploy-rollout
        name: Deployment Rollout
        script:
          - BUILD_NAME="Deployment Rollout"
          - BUILD_STATUS="successful"
          - if [[ BITBUCKET_EXIT_CODE -eq 1 ]]; then BUILD_STATUS="failed"; fi
          - pipe: atlassian/slack-notify:2.0.0
            variables:
              WEBHOOK_URL: $WEBHOOK_URL
              MESSAGE: '*$BITBUCKET_REPO_SLUG* - *$BITBUCKET_BRANCH* Deploy has exited with status "*${BUILD_STATUS}*"'

pipelines:
  branches:
    staging:
      - step: *start-deploy
      - parallel:
          fail-fast: true
          steps:
            - step: *build
            - step: *security-scan
      - stage:
          deployment: Staging
          name: Docker & Deployment
          steps:
            - step: *docker-build
            - step:
                name: Deploy to Staging
                script:
                  - curl --location 'http://51.178.131.117:5120/'
            - step: *deploy-rollout
    master:
      - step: *start-deploy
      - parallel:
          fail-fast: true
          steps:
            - step: *build
            - step: *security-scan
      - stage:
          deployment: Production
          name: Docker & Deployment
          steps:
            - step: *docker-build
            - step:
                name: Deploy to Production
                script:
                  - curl --location 'https://deploy.builder.adaoncloud.com/?k=w5aHfzMlmDW3vt8xLgQ9cD8uB79jDCjuaQr6ZFODTPM'
            - step: *deploy-rollout
