pipeline + Ansible 部署应用
Pipeline是什么?
从某种抽象层次上讲,部署 流水线( Deployment pipeline)是指从软件版本控制库到用户手中这一 过程 的 自动化 表现 形式。
按《 持续 交付》 中的 定义, Jenkins 本来就支持 pipeline(通常会把部署流水线简称为pipeline), 只是 一 开始不叫pipeline, 而叫任务。
Pipeline
pipeline{ agent{ label "master" } environment { mvn_goals = "clean install" } parameters { choice(name:'BUILD_BRANCH', choices: 'develop\nmaster', description:'请选择分支:') choice(name: 'project_profile', choices: 'test\ndev', description: '选择配置文件') choice(name: 'mvn_goals', choices: 'clean install\nclean', description: 'mvn命令') } stages{ stage("获取代码"){ steps{ script { // checkout([$class: 'GitSCM', branches: [[name: '${params.BUILD_BRANCH}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'f6491ca0-5731-4b14-88fd-3de8e2d7bbf0', url: 'git@code.gome.inc:IM-CD/gmkq.git']]]) def result = sh script: "rm -r ${WORKSPACE}/gmkq", returnStatus: true if (result == 0){ sh "git clone -b ${params.BUILD_BRANCH} git@code.gome.inc:IM-CD/gmkq.git" } } } } stage('MVN编译'){ steps{ script{ sh script: "/usr/local/maven3.5/bin/mvn ${params.mvn_goals} -q -e -U -P ${params.project_profile} -Dmaven.test.skip=true" } // sh "/usr/local/maven3.5/bin/mvn ${params.mvn_goals} -q -e -U -P ${params.project_profile} -Dmaven.test.skip=true" } } stage(ansible部署){ steps{ ansiblePlaybook installation: 'ansible-playbook', inventory: '/etc/ansible/gome-kq/hosts', playbook: '/etc/ansible/gome-kq/site.yml' } } } post{ success{ echo "========本次构建成功 ========" } failure{ echo "========本次构建失败========" } } }
Ansible 部署逻辑代码
项目结构:
[root@kvm-10-115-88-123 gome-kq]# tree . ├── hosts ├── roles │ └── kq │ ├── tasks │ │ ├── gome_kq │ │ │ └── main.yml │ │ └── main.yml │ └── vars │ └── main.yml └── site.yml
hosts部分, 发项目为分组
[root@kvm-10-115-88-123 gome-kq]# cat hosts [gome-kq] 10.115.78.1 [gome-kq:vars] dev=kq [gome-ehm] 10.115.78.2 [gome-ehm:vars] dev=ehm
tasks 主任务入口:
[root@kvm-10-115-88-123 kq]# cat tasks/main.yml --- - name: deploy gome_kq include: gome_kq/main.yml when: dev is defined and dev=='kq'
tasks 项目逻辑入口
[root@kvm-10-115-88-123 kq]# cat tasks/gome_kq/main.yml --- - name: Copy app to remote host copy: src={{ local_app_name }} dest={{ remote_dir }} - name: stop prod app shell: ps -ef | grep {{item}} | grep -v grep | awk '{print $2}' | xargs kill -9 ignore_errors: True register: result with_items: "{{tomcat_kq}}" - name: Backup prod package shell: mv {{ remote_dir }}/{{ item }}/webapps/gomekq.war {{ remote_dir }}/backup/gomekq.war_`date +%Y%m%d%H%M%S` with_items: "{{tomcat_kq}}" - name: Copy app to tomcat home shell: cp {{ remote_dir }}/gomekq.war {{ remote_dir }}/{{ item }}/webapps with_items: "{{tomcat_kq}}" - name: start tomcat shell: "{{ remote_dir }}/{{item}}/bin/startup.sh" with_items: "{{tomcat_kq}}"
vars变量入口
[root@kvm-10-115-88-123 kq]# cat vars/main.yml local_app_name: "/var/lib/jenkins/workspace/kq-pipline/target/gomekq.war" remote_dir: "/home/work" #remote_dir_tomcat: "/home/work/{{ item }}/webapps" #start_tomcat: "/home/work/{{item}}/bin" tomcat_kq: - tomcat-kq-8080 - tomcat-kq-8081 - tomcat-kq-8082 #with_items: "{{ tomcat_kq }}" process_name: - tomcat-kq-8080 - tomcat-kq-8081 - tomcat-kq-8082
效果如下: