kind: pipeline
type: docker
name: pipeline-example-maven

trigger:
  branch:
    - develop

clone:
  disable: true # 禁用自带的clone

  # clone代码
steps:
  - name: clone
    image: alpine/git
    volumes: # 将容器内目录挂载到宿主机，仓库需要开启Trusted设置
      - name: host-mount
        path: /data # 将maven下载依赖的目录挂载出来，防止重复下载
    commands:
      - git clone http://124.222.247.184:3000/root/courtBooking.git
      # 把这里改成了自己的仓库地址
      # http://124.222.247.184:3000/wteamYD/promotesai-background.git .
      - git checkout develop
      - cp *.sh /data
    when:
      event:
        - push # 当代码 push 时才重新拉取

  # 传输shell脚本到目标主机
  - name: scp files
    image: ryjer/drone-scp
    volumes: #将主机的shell脚本与容器共享
      - name: host-mount
        path: /data
    settings:
      host: 42.194.177.220
      username: root
      password:
        from_secret: 42.194.177.220_password # 从Secret中读取SSH密码
      port: 22
      rm: true
      source:
        - /data
      target:
        - /data/sh/
    environment:
      AUTO_CREATE_DIR: "true" # 自动创建目标目录
    when:
      event:
        - push # 当代码 push 时才重新上传

  # 启动shell脚本
  - name: spring-start 部署并重启
    image: appleboy/drone-ssh # SSH工具镜像
    settings:
      host: 42.194.177.220 # 远程连接地址
      username: root # 远程连接账号
      password:
        from_secret: 42.194.177.220_password # 从Secret中读取SSH密码
      port: 22 # 远程连接端口
      command_timeout: 5m # 远程执行命令超时时间
      script:
        - cd /data/sh # 进入宿主机构建目录
        - chmod +x develop.sh # 更改为可执行脚本
        - ./develop.sh  # 运行脚本打包应用镜像并运行
    when:
      event:
        - push # 当代码 push 时才重新上传

volumes: # 定义流水线挂载目录，用于共享shell脚本
  - name: host-mount
    host:
      path: /mydata/sh/ # 从宿主机中挂载的目录