Jenkins+GitLab+Ansible

2022-08-12 21:49:04 浏览数 (1)

目标

jenkins点击立即构建完成项目的编译部署

方式1: jenkins安装maven插件构建成war 直接构建后的操作发送到远程服务器,并使用脚本重启服务器。(单机版)

方式2: jenkins安装maven插件构建成war 使用ansible将war 复制到各个节点上(多个) 使用脚本重启tomcat

方式3: jenkins自由项目 构建时调用ansible的脚本(拉取代码,maven,构建,tomcat重启)

方式3具体思路描述: jenkins构建时调用ansible的脚本 1.构建时去git拉取代码到代码库 失败会重新拉一次(最多两次)

2.将代码重代码库复制到版本库

3.ansible脚本调用build.sh(mvn clean package)去构建项目

4.将current当前版本的文件夹指向版本库(第3步构建的位置)

5.将配置文件 拷贝过来

6.使用handle去触发supervisor重新加载

环境: 使用ansible 安装java git maven supervisor等 版本库最多放5个版本,其他的会被删除

回滚 1.去版本库找到上一个版本 。

2.将current指向上一个版本。

通过角色安装git

代码语言:javascript复制
//main.yml 
--- 
- include: git.yml 
//git.yml 
- name: Install lasetest git 
yum: 
name: '{{item}}' 
state: present 
with_items: 
- git 
//调用安装git 会自动去扫描 /etc/ansible/roles的角色 
--- 
- hosts: 39.108.231.144 
roles: 
- { role: common} 

安装maven

代码语言:javascript复制
//main.yml 
--- 
- stat: path={{maven_path}}apache-maven-{{maven_version}}/bin/ 
register: mvn_bin 
- include: install.yml 
when: not mvn_bin.stat.exists 
//install.yml 
[root@iZwz9278r1bks3b80puk6fZ tasks]# cat install.yml 
#- name: unarchive for maven 
# unarchive: 
# src: /root/app/apache-maven-{{maven_version}}-bin.tar.gz 
# dest: '{{maven_path}}' 
# copy: yes 
- name: config maven home 
lineinfile: 
path: /etc/profile.d/maven.sh 
create: yes 
state: present 
line: 'export MAVEN_HOME={{maven_path}}apache-maven-{{maven_version}}' 
- name: config maven path 
lineinfile: 
path: /etc/profile.d/maven.sh 
create: yes 
state: present 
line: 'export PATH=${PATH}:${MAVEN_HOME}/bin' 
- name: source profile 
shell: >- 
source /etc/profile 
[root@iZwz9278r1bks3b80puk6fZ maven]# cd defaults/ 
[root@iZwz9278r1bks3b80puk6fZ defaults]# ll 
total 4 
-rw-r--r-- 1 root root 50 Apr 10 17:26 main.yml 
[root@iZwz9278r1bks3b80puk6fZ defaults]# cat main.yml 
--- 
maven_version: 3.6.0 
maven_path: /root/maven/ 
//调用安装maven会自动去扫描 /etc/ansible/roles的角色 
--- 
- hosts: 39.108.231.144 
roles: 
- { role: maven } 

问题 source /etc/profile 不生效 ansible这类ssh远程执行是non-login shell,不会加载etc/profile,~/.bash_profile,而是加载etc/bashrc和~/.bashrc

从git拉取代码

代码语言:javascript复制
--- 
- name: module git test 
hosts: 39.108.231.144 
vars: 
repo_url: git@xxxxxxxxxx.git 
dest_path: /root/playbooks/repo 
tasks: 
- name: checkout repository on the host 
git: repo={{ repo_url }} dest={{ dest_path }} accept_hostkey=yes version=dev force=yes 

blog 例子

代码语言:javascript复制
//blog_update.yml 
--- 
- hosts: blog 
vars: 
app_name: blog 
repo_url: git@github.com:348786639/blog.git 
dest_path: /root/blog/repo 
release_path: /root/blog/release 
version_path: "{{ ansible_date_time.date|replace('-', '') }}{{ ansible_date_time.time|replace(':', '') }}" 
blog_configs: 
- {src: 'config.properties' , dest: 'src/main/resources/config.properties' } 
- {src: 'jdbc.properties', dest: 'src/main/resources/jdbc.properties' } 
environment: 
JAVA_HOME: /usr/local/jdk1.8.0_144/ 
roles: 
- { role: blog } 
// cat /etc/ansible/roles/blog/tasks/main.yml 
--- 
- name: 'debug' 
debug: 
msg: '222222' 
- name: checkout repository on the host 
git: repo={{ repo_url }} dest={{ dest_path }} accept_hostkey=yes version=master force=yes 
- name: create release path 
file: 
path: '{{release_path}}/{{version_path}}' 
state: directory 
mode: 755 
- name: copy config file from template 
template: 
src: ../template/{{ item.src }} 
dest: '{{ dest_path }}/{{item.dest}}' 
force: yes 
with_items: '{{ blog_configs|default([]) }}' 
- name: copy code to release 
shell: 
rsync -r --exclude=.git {{dest_path}}/ {{release_path}}/{{version_path}} 
chdir={{dest_path}} 
- name: build project 
shell: 
/bin/bash build.sh 
chdir={{release_path}}/{{version_path}} 
- name: link to current 
shell: 
ln -sfn ./{{version_path}} ./current 
chdir={{release_path}} 

template目录中有配置文件 jdbc.properties config.properties

0 人点赞