决战!!(1)--Jenkins+ansible+gitlab

mac2024-10-17  52

文章目录

Jenkins,ansible,gitlab实战Jenkins,ansible,gitlab集成环境搭建freestyle job 实现静态网站部署交付Jenkins构建任务

Jenkins,ansible,gitlab实战

Jenkins,ansible,gitlab集成环境搭建

gitlab:gitlab.example.comjenkins+ansible: jenkins.example.com #安装virtualenv,保证Jenkins和ansible共用一个系统用户,保证这两个工具能够协同工作交付给用户的云主机:test.example.com #需要保证我们的产品利用自动化部署交付到这台主机中,并能够根据用户的随后需求,利用我们的集成工具项目组合持续将更新交付到用户手中本地Windows:本地脚本编写,以及系统配置等相关工作 1. 利用Jenkins抓取卡法人员的项目代码,以及日常维护ansible-playbook脚本,到我们的Jenkins job workspace工作区域内 2. 通过Jenkins内嵌的任务格式编写Jenkins freestyle job或者pipeline job并集成我们的ansible部署工具 3. 通过我们从gitlab抓取到的ansible-playbook脚本进行我们随后的部署工作 4. 最后将我们的产品代码按照我们的脚本的部署逻辑远程推送到我们的客户主机 5. 这样我们就打通了一个从抓取代码到集成部署工具到代码交付的自动化部署流水线 6. 无论客户以后有任何更新需求或者项目本身出现bug需要修复都可以利用Jenkins,ansible,gitlab这个组合去一键将我们的项目自动部署到远程客户主机当中解决我们日常持续交付的效率问题,提升我们的自动化部署交付能力,最终让客户满意

freestyle job 实现静态网站部署交付

三剑客平台初始环境构建 #验证环境编写ansible playbook脚本实现静态网页远程部署将playbook部署脚本提交到gitlab仓库构建freestyle job任务框架Jenkins集成ansible与gitlab实现静态网页的自动化部署 静态网页ansible-playbook脚本编写 git clone https://git.example.com/root/ansible-playbook-repo.git git config --global http.sslVerify false #关闭ssl认证 cd ansible-playbook-repo/ ll test_playbooks/ cp -a test_playbooks/ nginx_playbooks ls nginx_playbooks/ deploy.retry deploy.yml inventory/ roles/ 任务入口文件deploy.yml vim deploy.yml hosts: "testservers" gather_facts: true remount_user: root roles: - nginx [root@jenkins nginx_playbooks] cd inventory/ cp testenv prod mv testenv dev 环境文件 vim prod [nginx] test.example.com #目标主机,可以添加多个 [nginx:vars] #nginx组名称变量列表,下面是变量键值对 server_name=test.example.com port=80 user=deploy worker_processes=4 max_open_file=65505 root=/www vim dev [nginx] test.example.com [nginx:vars] server_name=test.example.com port=80 user=deploy worker_processes=4 max_open_file=65505 root=/www [root@jenkins inventory]# ll 总用量 8 -rw-r--r-- 1 root root 137 10月 26 14:55 dev -rw-r--r-- 1 root root 137 10月 26 14:55 prod roles任务列表 [root@jenkins ansible] cd roles/ [root@jenkins roles] ls testbox [root@jenkins roles] mv testbox/ nginx [root@jenkins roles] cd nginx/ [root@jenkins nginx] ll 总用量 0 drwxr-xr-x 2 root root 6 10月 26 14:57 files drwxr-xr-x 2 root root 6 10月 26 14:57 tasks drwxr-xr-x 2 root root 6 10月 26 14:57 templates [root@jenkins nginx] cd files/ [root@jenkins files] vim health_check.sh # 检查静态网页是否部署成功 #!/bin/shURL=$1 curl -Is http://$URL >/dev/null && echo "The remote side is healthy" || echo "The remote side is failed,please check" [root@jenkins files] echo "This is my first website" >index.html [root@jenkins files] ll 总用量 8 -rw-r--r-- 1 root root 135 10月 26 15:03 health_check.sh -rw-r--r-- 1 root root 25 10月 26 15:05 index.html [root@jenkins files] cat index.html This is my first website [root@jenkins nginx] cd templates/ [root@jenkins templates] cat nginx.conf.j2 | egrep -v '^$|#' user {{ user }}; worker_processes {{ worker_processes }}; events { worker_connections {{ max_open_file }}; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen {{ port }}; server_name {{ server_name }}; location / { root {{ root }}; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } cd roles/nginx/tasks/ vim main.yml - name: Disable system firewall service: name=firewall state=stopped - name: Disable SELINUX selinux: state=disabled - name: setup nginx yum source yum: pkg=epel-release state=latest #pkg指定yum源,state=latest 安装最新版本 - name: write then nginx config file template: src=roles/nginx/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf - name: create nginx root folder file: 'path={{ root }} state=directory owner={{ user }} group={{ user }} mode=0755' #创建一个空目录 - name: copy index.html to remote copy: 'remote_src=no src=roles/nginx/files/index.html dest=/www/index.html mode=0755' #将本地playbook事先创建好的index.html传送到目标主机的根目录中,remote_src=no声明将本地文件传送到目标文件,声明我们需要将本地index.html传送到目标主机src=等于这个路径声明本地index.html的路径,dest=声明目标路径,mode=0755更改index.html为0755 - name: restart nginx service service: name=nginx state=restarted #声明需要在目标主机重启nginx服务 - name: run the health check locally shell: "sh roles/nginx/files/health_check.sh {{ server_name }}" #验证是否成功启动服务并加载静态文件 delegate_to: localhost register: health_status #输出内容穿给health_status - debug: msg="{{ health_status.stdout }}" #输出上一个任务注册的参数值并打印到playbook输出中,查看返回值定义健康状况 git add . git commit -m"This is my first commit" git push origin master

Jenkins构建任务

#!/bin/sh set +x source /home/deploy/.py3-a2.5-env/bin/activate source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q cd $WORKSPACE/nginx_playbooks ansibel --version ansibel-playbook --version ansibel-playbook -i inventory/$deploy_env ./deploy.yml -e project=nginx -e branch=$branch -e env=$deploy_env set -x

访问nginx网站
最新回复(0)