kind: pipeline
type: docker
name: default

steps:
  - name: rsync
    image: drillster/drone-rsync
    settings:
      hosts: 
        - 172.31.0.34
      user: root
      port: 22
      key: 
        from_secret: rsync_key_master
      secret: [ rsync_key_master ]
      source: ./html                    # 资源文件夹
      target: /root/js-file_tmp         # 目标文件夹
      recursive: true                   # 递归复制
      delete: true                      # 在copy前删除目标文件夹
      args: "--no-o --no-g -v"
      script:                           # rsync发生之后执行的命令
        - /var/anyview/deploy_js_file.sh js-file

  - name: send-success
    image: plugins/webhook
    settings:
      urls: https://open.feishu.cn/open-apis/bot/v2/hook/58efb74f-989a-4e08-adef-d81f880c06c7
      content_type: application/json
      template: |
        {
          "msg_type":"text",
          "content":{
            "text":"更新js资源文件-执行成功"
          }
        }        
    depends_on: [ rsync ]
    when:
      status:
        - success

  - name: send-failure
    image: plugins/webhook
    settings:
      urls: https://open.feishu.cn/open-apis/bot/v2/hook/58efb74f-989a-4e08-adef-d81f880c06c7
      content_type: application/json
      template: |
        {
          "msg_type":"text",
          "content":{
            "text":"更新js资源文件-执行失败"
          }
        }        
    depends_on: [ rsync ]
    when:
      status:
        - failure

  - name: post-deploy
    image: appleboy/drone-ssh
    settings:
      host:
        from_secret: ssh_host_master
      username:
        from_secret: ssh_username_master
      key:
        from_secret: rsync_key_master
      port:
        from_secret: ssh_port_master
      command_timeout: 15m
      script:
        - echo ----------部署失败，回滚中----------
        - cd /var/anyview
        - ./rollback_js_file.sh js-file fe_js/
    depends_on: [ send-failure ]
    when:
      status:
        - failure
      branch:
        - master
      event:
        - push