CI/CD - 环境搭建(6) - 实现Salt部署

Goal

利用Salt,从Artifactory上Get Build,并部署。

Prerequisites

Salt Config

  1. 创建Salt State

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    [root@Automation ~]# cd /srv/salt/
    [root@Automation salt]# ls
    artifact.sls file.sls _grains mysql.sls top.sls webserver.sls
    [root@Automation salt]# cat artifact.sls
    spring_module_downloaded:
    artifactory.downloaded:
    - artifact:
    artifactory_url: http://192.168.220.129:8081/artifactory
    repository: 'libs-release-local'
    artifact_id: 'ci-example'
    group_id: 'com.izheyi'
    packaging: 'jar'
    classifier: 'sources'
    version: '1.0.1'
    - target_file: /opt/artifact_project.jar
    [root@Automation salt]# cat top.sls
    base:
    '*':
    - artifact
  2. 执行Command

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    [root@Automation salt]# salt '*' state.highstate
    192.168.220.131:
    ----------

    ID: spring_module_downloaded
    Function: artifactory.downloaded
    Result: True
    Comment: Artifact downloaded from URL: http://192.168.220.129:8081/artifactory/libs-release-local/com/izheyi/ci-example/1.0.1/ci-example-1.0.1.jar
    Started: 02:02:49.649477
    Duration: 860.559 ms
    Changes:
    ----------
    downloaded_file:
    /opt/artifact_project.jar

    Summary
    ------------

    Succeeded: 1 (changed=1)
    Failed: 0
    ------------

    Total states run: 1
    [root@Automation salt]#
  3. 去Minion检查

    1
    2
    3
    [root@Automation opt]# ls
    alfresco-community artifact_project.jar my_new_directory rh
    [root@Automation opt]#
  4. 用Command运行

    1
    [root@Automation salt]# salt '*' cmd.run 'cd /opt; java -jar artifact_project.jar'
  5. 检查成功

    1
    2
    3
    [root@Automation opt]# curl http://192.168.220.131:8080/
    Hello World!
    [root@Automation opt]#

With Jenkins

用Shell:

1
2
3
sshpass -p yongfei.hu ssh -o "StrictHostKeyChecking no"  root@192.168.220.130 "salt '*' state.highstate"

sshpass -p yongfei.hu ssh -o "StrictHostKeyChecking no" root@192.168.220.130 "salt '*' cmd.run 'cd /opt; java -jar artifact_project.jar'"

唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!