美文网首页DevOps
jekins构建微服务

jekins构建微服务

作者: 小李飞刀_lql | 来源:发表于2022-01-11 09:08 被阅读0次

playbook验证

host

vi /etc/ansible/hosts

[web2]
192.168.153.17 ansible_ssh_user=root ansible_ssh_pass='root'
192.168.153.18 ansible_ssh_user=root ansible_ssh_pass='root'

yaml

[root@devops1 playbook]# vi ms.yaml 
- hosts: web2
  gather_facts: no

  tasks:
  - name: 推送构建文件
    copy:
      src=/root/simple-microservice/eureka-service/target/eureka-service.jar
      dest=/data/ms/eureka
  - name: 重启服务
    systemd: name=eureka state=restarted

命令执行

[root@devops1 playbook]# ansible-playbook ms.yaml 

所需界面

1635341847432.png

创建选择参数

分支参数

1635317582426.png
parameters {
  gitParameter branch: '', branchFilter: '.*', defaultValue: 'master', description: '请选择要发布的分支', name: 'Branch', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH'
}

服务多选参数

1635318197068.png
parameters {
  extendedChoice description: '请选择要发布的分支', multiSelectDelimiter: ',', name: 'Service', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_CHECKBOX', value: 'portal,gateway,product,order,stock', visibleItemCount: 5
}

选择要发布的服务器组

1635318540936.png
parameters {
  choice choices: ['webservers1', 'webservers2'], description: '请选择要发布的服务器组', name: 'Servers'
}

代码拉取

Gitlab创建项目

【你的群组】【选择群组】【新建项目】
1635319118620.png

源码推送

[root@devops1 simple-microservice]# git config --global user.name "lql"
[root@devops1 simple-microservice]# git config --global user.email "lql_h@163.com"

[root@devops1 simple-microservice]# git init
初始化空的 Git 版本库于 /root/git/simple-microservice/.git/
[root@devops1 simple-microservice]# git remote add origin http://192.168.153.18/test/ms.git  
[root@devops1 simple-microservice]# git add .
[root@devops1 simple-microservice]# git commit -m "Initial commit"
[root@devops1 simple-microservice]# git push -u origin master

创建拉取代码脚本

1635320423504.png
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '711e362f-2255-4c8c-bb4a-a81353e2ee29', url: 'http://192.168.153.18/test/ms.git']]])

完整脚本

pipeline {
    agent {
        label 'web1'
    }
    parameters {
      gitParameter branch: '', branchFilter: '.*', defaultValue: 'master', description: '请选择要发布的分支', name: 'Branch', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH'
      extendedChoice description: '请选择要发布的分支', multiSelectDelimiter: ',', name: 'Service', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_CHECKBOX', value: 'portal,gateway,product,order,stock', visibleItemCount: 5
      choice choices: ['webservers1', 'webservers2'], description: '请选择要发布的服务器组', name: 'Servers'
    }
    stages {
        stage('拉取代码') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: "$Branch"]], extensions: [], userRemoteConfigs: [[credentialsId: '711e362f-2255-4c8c-bb4a-a81353e2ee29', url: 'http://192.168.153.18/test/ms.git']]])
            }
        }
        stage('编译构建') {
            steps {
                sh 'mvn clean package -Dmaven.test.skip=true'
            }
        }
        
        stage('部署') {
            steps {
                //将用户选择的微服务的构建文件复制到临时目录
                //$Service=portal,gateway,product,stock,order
                script{
                    def tmp_dir="$WORKSPACE/tmp"
                    def services="$Service".split(",")
                    for(name in services){
                        sh """
                        echo $name
                        pwd
                        ls
                        mkdir $tmp_dir |true
                        cd $name-service
                        if ls |grep biz &>/dev/null;then
                            cd $name-service-biz
                        fi
                        mv target/*.jar $tmp_dir
                        ls $tmp_dir
                        """
                    }
                }
                //生成主机清单文件和playbook
                sh """
cat > /tmp/jenkins_home/.hosts <<EOF
[webservers1]
192.168.153.17 ansible_ssh_user=root ansible_ssh_pass='root'
192.168.153.18 ansible_ssh_user=root ansible_ssh_pass='root'
[webservers2]
192.168.153.19 ansible_ssh_user=root ansible_ssh_pass='root'
EOF
cat > /tmp/jenkins_home/.playbook <<EOF
- hosts: $Servers
  gather_facts: no
  vars:
    workspace: $WORKSPACE
    work_dir: "/data/ms"
    service_name: $Service
  tasks:
  - name: 推送构建文件
    copy:
      src={{item}}
      dest={{work_dir}}/{{item.split("/")[-1].split("-")[0]}}
    with_fileglob:
      - "{{workspace}}/tmp/*.jar"
  - name: 重启服务
    systemd: name={{item}} state=restarted
    loop: "{{service_name.split(',')}}"
EOF

                """
                ansiblePlaybook(
                    playbook: "/tmp/jenkins_home/.playbook",
                    inventory: "/tmp/jenkins_home/.hosts",
                    credentialsId: ""
                )
                
            }
        }
        
    }
}

查看效果

1635341451702.png

相关文章

网友评论

    本文标题:jekins构建微服务

    本文链接:https://www.haomeiwen.com/subject/dbnmcrtx.html