jenkins freestyle job实战

1、Freestyle Job实现静态网站部署交付

流程:

三剑客平台初始环境构建;
编写ansible playbook脚本实现静态网页远程部署;
将playbook部署脚本提交到GitLab仓库;
构建Freestyle Job任务框架;
Jenkins集成Anisble与Gitlab实现静态网页的自动化部署;


(1)环境在前面章节都已经搭建完成了;


(2)然后:

在gitlab中创建一个项目:ansible-playbook-repo  并把前面在ansible章节中创建的“test_playbooks”目录上传到gitlab项目中:

image


然后把ansible-playbook-repo 克隆到windows本地,我们把test_playbooks当做模板,需要的时候复制一份,做修改:

这里用的Git Bash,可以在windows中执行linux命令,这里将test_playbooks复制一份为nginx_playbooks作为部署静态网页的playbook框架:

image


(3)修改nginx_playbooks中的部分内容:

cd nginx_playbooks/

-------
vim deploy.yml  #内容如下
- hosts: "nginx"
  gather_facts: true
  remote_user: root
  roles:
    - nginx

-------
cd inventory/
cp testenv prod
mv testenv dev

-------
vim prod  #内容如下
[nginx]
test.example.com

[nginx:vars]
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

-------
cd ..

cd roles/

mv testbox nginx

cd nginx/

cd files/

-------
rm foo.sh && vim health_check.sh  #内容如下
#!/bin/bash
URL=$1
curl -Is http://$URL > /dev/null && echo "The remote side is healthy" || echo "The remote side is failed,please check"

-------
echo "This is my first website" > index.html

-------
cd ..

cd tasks/

-------
vim main.yml   #内容如下
- name: Disable system firewall
  service: name=firewalld state=stopped

- name: Disable SELINUX
  selinux: state=disabled

- name: setup nginx yum source
  yum: pkg=epel-release 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'

- name: restart nginx service
  service: name=nginx state=restarted

- name: run the health check locally
  shell: "sh roles/nginx/files/health_check.sh {{ server_name }}"
  delegate_to: localhost
  register: health_status

- debug: msg="{{ health_status.stdout }}"

-------
cd ../../..   #此时在nginx_playbooks目录下

#把nginx_playbooks提交到gitlab的ansible-playbook-repo 项目中
git add .
git commit -m"This is my first commit"
git push origin master

image


(4)在jenkins中创建任务

image

image

image

image


参数化构建:

image

应用&&保存


构建:

image


构建完成后,验证:

image

windows中添加hosts记录:

image

posted @ 2020-04-13 22:40  米兰的小铁將  阅读(391)  评论(0编辑  收藏  举报