Freestyle Job實現靜態網站部署交付(接Jenkins+Gitlab+Ansible自動化部署(四)http://www.javashuo.com/article/p-suybpulk-g.html)html
首先肯定本身的環境已經準備完畢。python
登陸gitlab查看linux
登陸Jenkins首頁nginx
登陸Jenkins主機查看Ansible2.5+python 3.6虛擬環境git
$ ssh root@192.168.244.131 Last login: Wed Jan 9 13:40:48 2019 from 192.168.244.1 [root@jenkins ~]# su - deploy Last login: Wed Jan 9 20:24:43 CST 2019 on pts/0 [deploy@jenkins ~]$ source /home/deploy/.py3-a2.5-env/bin/activate (.py3-a2.5-env) [deploy@jenkins ~]$ source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q (.py3-a2.5-env) [deploy@jenkins ~]$ ansible --version ansible 2.5.14 (stable-2.5 c748512c4c) last updated 2019/01/09 20:03:39 (GMT +800) config file = None configured module search path = ['/home/deploy/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /home/deploy/.py3-a2.5-env/ansible/lib/ansible executable location = /home/deploy/.py3-a2.5-env/ansible/bin/ansible python version = 3.6.8 (default, Jan 9 2019, 19:44:42) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] (.py3-a2.5-env) [deploy@jenkins ~]$
準備就緒,開始編寫ansible playbook腳本實現靜態網頁遠程部署,打開Git bash命令行,將以前建立好的ansible-playbook-repo倉庫clone到本地:web
xueji@xueji MINGW64 ~ $ git config --global http.sslVerify false xueji@xueji MINGW64 ~ $ cd Desktop/repo/ $ git clone https://gitlab.example.com/root/ansible-playbook-repo.git Cloning into 'ansible-playbook-repo'... remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo (master) $ ll total 9 -rw-r--r-- 1 xueji 1049089 0 1月 10 11:57 ansible-playbook.txt drwxr-xr-x 1 xueji 1049089 0 1月 10 12:02 nginx_playbooks/ -rw-r--r-- 1 xueji 1049089 312 7月 15 08:33 nginx-freestyle-job.sh drwxr-xr-x 1 xueji 1049089 0 1月 10 12:02 test_playbooks/
開始配置shell
xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master) $ ll total 2 -rw-r--r-- 1 xueji 1049089 17 7月 17 21:06 deploy.retry -rw-r--r-- 1 xueji 1049089 80 7月 17 21:06 deploy.yml drwxr-xr-x 1 xueji 1049089 0 1月 10 12:02 inventory/ drwxr-xr-x 1 xueji 1049089 0 1月 10 12:02 roles/ xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master) $ vim deploy.yml - hosts: "nginx" gather_facts: true remote_user: root roles: - nginx xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master) $ vim inventory/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 xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master) $ vim inventory/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 xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master) $ vim roles/nginx/files/health_check.sh #!/bin/sh URL=$1 curl -Is http://$URL > /dev/null && echo "The remote side is healthy" || echo "The remote side is failed, please check" xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master) $ cat roles/nginx/files/index.html This is my first website xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master) $ vim roles/nginx/templates/nginx.conf.j2 # For more information on configuration, see: user {{ user }}; worker_processes {{ worker_processes }}; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections {{ max_open_file }}; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; # Load config files from the /etc/nginx/conf.d directory # The default server is in conf.d/default.conf #include /etc/nginx/conf.d/*.conf; server { listen {{ port }} default_server; server_name {{ server_name }}; #charset koi8-r; #access_log logs/host.access.log main; location / { root {{ root }}; index index.html index.htm; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } } xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master) $ vim roles/nginx/tasks/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 }}"
將配置好的文件,提交到遠程gitlabvim
xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master) $ cd ~/Desktop/repo/ansible-playbook-repo/ xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo (master) $ git add . xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo (master) $ git commit -m"First commit" [master 06431dc] First commit 8 files changed, 122 deletions(-) delete mode 100644 test_playbooks/deploy.retry delete mode 100644 test_playbooks/deploy.yml delete mode 100644 test_playbooks/inventory/dev delete mode 100644 test_playbooks/inventory/prod delete mode 100644 test_playbooks/roles/nginx/files/health_check.sh delete mode 100644 test_playbooks/roles/nginx/files/index.html delete mode 100644 test_playbooks/roles/nginx/tasks/main.yml delete mode 100644 test_playbooks/roles/nginx/templates/nginx.conf.j2 xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo (master) $ git push origin master Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Delta compression using up to 4 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 212 bytes | 212.00 KiB/s, done. Total 2 (delta 1), reused 0 (delta 0) To https://gitlab.example.com/root/ansible-playbook-repo.git 169dec7..06431dc master -> master
返回到Jenkins web管理頁,點擊「New 任務」windows
添加描述瀏覽器
添加Git倉庫(該倉庫地址即爲上述配置的ansible-playbook-repo的倉庫地址)
參數化構建過程
添加構建步驟
在執行shell彈出的輸入框內輸入如下內容
#/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 ansible --version ansible-playbook --version ansible-playbook -i inventory/$deploy_env ./deploy.yml -e project=nginx -e branch=$branch -e env=$deploy_env
點擊「Save」,而後點擊「Build with Parameters」,在右側下拉列表中選擇dev,點擊「Build」
查看輸出信息
驗證目標主機是否部署成功,在瀏覽器輸入test.exmaple.com查看
前提是保證本地windows主機下的C:\Windows\System32\drivers\etc\hosts文件中末尾有以下對應關係
192.168.244.130 gitlab.example.com 192.168.244.131 jenkins.example.com 192.168.244.132 ansible.example.com 192.168.244.133 test.example.com
能夠看到已經成功部署~