#  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* Install rClone has exited with status "*${BUILD_STATUS}*"'
    - step: &build
        <<: *webhook-fail
        name: Build
        caches:
          - node
        script:
          - npm install
          - npm run build
        artifacts:
          - build/**
    - step: &install-rclone
        <<: *webhook-fail
        name: Install rClone
        caches:
          - node
        script:
          - apt update && apt install -y curl unzip
          - ./install-rclone.sh
        artifacts:
          - rclone/**
    - 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
          - pipe: atlassian/git-secrets-scan:0.5.1
    - step: &deploy-step
        name: Deploy to Production
        deployment: Production
        script:
          - ./rclone/rclone --config="./rclone/rclone.conf" config update $OVHOBJSTORAGE access_key_id=$ACCESSKEYID secret_access_key=$SECRETACCESSKEY --non-interactive
          - ./rclone/rclone --config="./rclone/rclone.conf" sync -v ./build/static $OVHOBJSTORAGE:$OVHOBJPATH/gallery
          - curl --location --request POST "${ASSETS_URL}/events/fe-static-update" --header 'Content-Type:application/json' --data-raw '{ "action":"remoteDirs", "remoteDirs":["gallery"]}'
        after-script:
          - 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:
      - parallel:
          fail-fast: true
          steps:
            - step: *security-scan

      - step:
          <<: *deploy-step
          name: Deploy to Staging
          deployment: Staging
          script:
            - curl --location 'http://51.178.131.117:5120/'
    master:
      - parallel:
          fail-fast: true
          steps:
              # - step: *build quello reale da utilizzare in produzione, al momento sbloccato per test...
            - step:
                <<: *build
                script:
                  - npm install
                  - CI= npm run build
            - step: *install-rclone
            - step: *security-scan

      - step: *deploy-step
