version: 2
name: gitlink-cli-autodeploy
description: "每次push到master时自动在服务器上构建并部署gitlink-cli"
global:
  concurrent: 1

trigger:
  webhook: gitlink@1.0.0
  event:
    - ref: push
      ruleset-operator: AND
      condition:
        param: ref
        operator: include_regex
        value: "^refs/heads/master$"

workflow:
  - ref: start
    name: 开始
    task: start

  # 1. 准备服务器环境
  - ref: prepare_server_0
    name: 准备服务器环境
    task: ssh_cmd@1.1.1
    input:
      ssh_ip: '"121.41.222.0"'
      ssh_port: '"22"'
      ssh_user: '"root"'
      ssh_private_key: ((gitlink_cli.gitlink_cli_deploy_key))
      ssh_cmd: '"/opt/gitlink-deploy/prepare.sh"'
    needs:
      - start

  # 2. 在服务器上构建
  - ref: build_on_server_0
    name: 在服务器上构建
    task: ssh_cmd@1.1.1
    input:
      ssh_ip: '"121.41.222.0"'
      ssh_port: '"22"'
      ssh_user: '"root"'
      ssh_private_key: ((gitlink_cli.gitlink_cli_deploy_key))
      ssh_cmd: '"/opt/gitlink-deploy/build.sh"'
    needs:
      - prepare_server_0

  # 3. 部署到服务器
  - ref: deploy_on_server_0
    name: 部署到服务器
    task: ssh_cmd@1.1.1
    input:
      ssh_ip: '"121.41.222.0"'
      ssh_port: '"22"'
      ssh_user: '"root"'
      ssh_private_key: ((gitlink_cli.gitlink_cli_deploy_key))
      ssh_cmd: '"/opt/gitlink-deploy/deploy.sh"'
    needs:
      - build_on_server_0

  # 4. 验证部署
  - ref: verify_deployment_0
    name: 验证部署
    task: ssh_cmd@1.1.1
    input:
      ssh_ip: '"121.41.222.0"'
      ssh_port: '"22"'
      ssh_user: '"root"'
      ssh_private_key: ((gitlink_cli.gitlink_cli_deploy_key))
      ssh_cmd: '"gitlink-cli version"'
    needs:
      - deploy_on_server_0

  - ref: end
    name: 结束
    task: end
    needs:
      - verify_deployment_0
