kind: pipeline
name: default
type: docker
steps:
# ============ RESTORE CACHE =============
  - name: restore node_modules cache
    image: appleboy/drone-sftp-cache
    settings:
      server:
        from_secret: cache_server
      port: 22
      username:
        from_secret: cache_server_user
      key:
        from_secret: cache_server_key
      path: /var/cache/drone
      restore: true
      mount:
        - node_modules

# ============ BUILD STEPS ===============
  - name: build-prod
    image: node:14
    commands:
      - npm i
      - ./node_modules/.bin/ionic build --prod
      - ./node_modules/.bin/ngsw-config ./www/ ./ngsw-config.json /i
      - tar czf build.tar.gz www

# =============== REBULD CACHE ==============
  - name: rebuild node_modules cache
    image: appleboy/drone-sftp-cache
    settings:
      server:
        from_secret: cache_server
      port: 22
      username:
        from_secret: cache_server_user
      key:
        from_secret: cache_server_key
      path: /var/cache/drone
      rebuild: true
      mount:
        - node_modules

# =============== RELEASE ===============
  - name: release
    image: plugins/gitea-release
    settings:
      api_key:
        from_secret: gitea_api_key
      base_url: https://code.garrettmills.dev
      files: build.tar.gz
      title: ${DRONE_TAG}
    when:
      event: tag

# =============== DEPLOY TO DEV SITE ===============
  - name: copy artifacts to staging site
    image: appleboy/drone-scp
    settings:
      host:
        from_secret: dev_site_host
      username:
        from_secret: dev_site_user
      key:
        from_secret: dev_site_key
      port: 22
      source: build.tar.gz
      target: /etc/glmdev/noded
    when:
      event: promote
      target: staging
  - name: deploy artifacts on staging site
    image: appleboy/drone-ssh
    settings:
      host:
        from_secret: dev_site_host
      username:
        from_secret: dev_site_user
      key:
        from_secret: dev_site_key
      port: 22
      script:
        - cd /etc/glmdev/noded
        - rm -rf www
        - tar xzf build.tar.gz
        - cd www
        - echo -n "$(uuidgen)" | sudo tee version.html
    when:
      event: promote
      target: staging

# =============== DEPLOY TO PROD SITE ===============
  - name: copy artifacts to production site
    image: appleboy/drone-scp
    settings:
      host:
        from_secret: prod_site_host
      username:
        from_secret: prod_site_user
      key:
        from_secret: prod_site_key
      port: 22
      source: build.tar.gz
      target: /etc/glmdev/noded-prod
    when:
      event: promote
      target: production
  - name: deploy artifacts on production site
    image: appleboy/drone-ssh
    settings:
      host:
        from_secret: prod_site_host
      username:
        from_secret: prod_site_user
      key:
        from_secret: prod_site_key
      port: 22
      script:
        - cd /etc/glmdev/noded-prod
        - rm -rf www
        - tar xzf build.tar.gz
        - cd www
        - echo -n "$(uuidgen)" | sudo tee version.html
    when:
      event: promote
      target: production

# =============== BUILD NOTIFICATIONS ===============
  - name: send build success notifications
    image: plugins/webhook
    settings:
      urls:
        from_secret: notify_webhook_url
      content_type: application/json
      template: |
        {
          "title": "Drone-CI [noded-frontend @ ${DRONE_BUILD_NUMBER}]",
          "message": "Build completed successfully. (Changes by ${DRONE_COMMIT_AUTHOR})",
          "priority": 4
        }
    when:
      status: success
      event:
        exclude:
          - promote
  - name: send build error notifications
    image: plugins/webhook
    settings:
      urls:
        from_secret: notify_webhook_url
      content_type: application/json
      template: |
        {
          "title": "Drone-CI [noded-frontend @ ${DRONE_BUILD_NUMBER}]",
          "message": "Build failed! (Changes by ${DRONE_COMMIT_AUTHOR})",
          "priority": 6
        }
    when:
      status: failure
      event:
        exclude:
          - promote

# =============== DEPLOY NOTIFICATIONS ===============
  - name: send deploy success notifications
    image: plugins/webhook
    settings:
      urls:
        from_secret: notify_webhook_url
      content_type: application/json
      template: |
        {
          "title": "Drone-Deploy [noded-frontend @ ${DRONE_BUILD_NUMBER} to ${DRONE_DEPLOY_TO}]",
          "message": "Deployment completed successfully. (Changes by ${DRONE_COMMIT_AUTHOR})",
          "priority": 4
        }
    when:
      status: success
      event:
        - promote
  - name: send deploy error notifications
    image: plugins/webhook
    settings:
      urls:
        from_secret: notify_webhook_url
      content_type: application/json
      template: |
        {
          "title": "Drone-Deploy [noded-frontend @ ${DRONE_BUILD_NUMBER} to ${DRONE_DEPLOY_TO}]",
          "message": "Deployment failed! (Changes by ${DRONE_COMMIT_AUTHOR})",
          "priority": 6
        }
    when:
      status: failure
      event:
        - promote
