kind: pipeline
type: docker
name: default

# 触发分支
trigger:
  branch:
    - master
    - develop

steps:

  # 获取缓存 (首次部署注释掉此处)
  - name: restore-cache
    image: drillster/drone-volume-cache
    settings:
     restore: true
     mount:
       - /root/.m2/repository
    volumes:
     - name: repository
       path: /cache

  # 打包 (prod)
  - name: build-prod
    image: matderosa/maven-3.8-openjdk-17-gcc8.5
    volumes: # 将容器内目录挂载到宿主机，仓库需要开启Trusted设置
      - name: maven-cache
        path: /root/.m2 # 将maven下载依赖的目录挂载出来，防止重复下载
      # (/app/build/ 对应的是 /home/www/project/BackendSys/ 目录)
      - name: maven-build
        path: /app/build # 将应用打包好的Jar和执行脚本挂载出来
    commands:
      - mvn clean package -P prod
      # (此时拷贝的内容是仓库分支拉下来的代码)
      - mkdir -p /app/build/target
      # 将打包好的 Jar包 拷贝到指定目录
      - cp target/backendsys.jar /app/build/target/backendsys.jar
      # 将配置文件从最新的分支 拷贝到目标目录
      - cp Dockerfile /app/build/Dockerfile
      - cp docker-compose.yml /app/build/docker-compose.yml
    when:
      branch:
        master

  # 打包 (develop)
  # - 在同个服务器时，docker-compose.yml 需要修改服务名
  - name: build-dev
    image: matderosa/maven-3.8-openjdk-17-gcc8.5
    volumes: # 将容器内目录挂载到宿主机，仓库需要开启Trusted设置
      - name: maven-cache
        path: /root/.m2 # 将maven下载依赖的目录挂载出来，防止重复下载
      # (/app/build/ 对应的是 /home/BackendSys/ 目录)
      - name: maven-build-dev
        path: /app/build # 将应用打包好的Jar和执行脚本挂载出来
    commands:
      - mvn clean package -P dev
      # (此时拷贝的内容是仓库分支拉下来的代码)
      - mkdir -p /app/build/target
      # 将打包好的 Jar包 拷贝到指定目录
      - cp target/backendsys-dev.jar /app/build/target/backendsys-dev.jar
      # 将配置文件从最新的分支 拷贝到目标目录
      - cp Dockerfile /app/build/Dockerfile
      - cp docker-compose.yml /app/build/docker-compose.yml
    when:
      branch:
        develop

  # # 更新缓存
  # - name: rebuild-cache
  #   image: drillster/drone-volume-cache
  #   settings:
  #     rebuild: true
  #     mount:
  #       - /root/.m2/repository
  #   volumes:
  #     - name: repository
  #       path: /cache

  # 部署 (prod)
  - name: deploy-ssh-prod
    image: appleboy/drone-ssh
    settings:
      port: 22
      host:
        from_secret: ssh_host
      username:
        from_secret: ssh_username
      password:
        from_secret: ssh_password
      command_timeout: 1m
      script:
        # (此时是在 SSH登录服务器 的作用域)
        # Docker 启动
        - cd /home/BackendSys
        # 根据 Dockerfile 配置创建镜像 (名称需要小写)
        - sudo docker build -t backendsys .
        # 停止容器
        - sudo docker-compose down
        # 启动容器 (根据 docker-compose.yml 配置)
        - sudo docker-compose up -d
        - sudo echo "Docker App started."
    when:
      branch: master

  # 部署 (develop)
  - name: deploy-ssh-dev
    image: appleboy/drone-ssh
    settings:
      port: 22
      host:
        from_secret: ssh_host
      username:
        from_secret: ssh_username
      password:
        from_secret: ssh_password
      command_timeout: 1m
      script:
        - cd /home/BackendSysDev
        - sudo docker build -t backendsys-dev .
        - sudo docker-compose down
        - sudo docker-compose up -d
        - sudo echo "Docker App started."
    when:
      branch: develop



  # 清除冗余镜像
  - name: cleanup
    image: docker:dind
    volumes:
      - name: docker-sock
        path: /var/run/docker.sock
    when:
      status: [ success ]
    commands:
      # 删除1小时前创建的未使用镜像
      - docker image prune -f --filter "until=1h"


volumes: # 定义流水线挂载目录，用于共享数据
  - name: maven-build
    host:
      path: /home/BackendSys # 从宿主机中挂载的目录
  - name: maven-build-dev
    host:
      path: /home/BackendSysDev # 从宿主机中挂载的目录
  - name: maven-cache
    host:
      path: /root/docker/maven/cache
  - name: docker-sock
    host:
      path: /var/run/docker.sock