Gitlab + Jenkins + Ansible 实现自动部署-小案例

完整版安装过程请参考Gitlab+Jenkins+Ansible安装过程(完整版)on Centos7

在webservers客户机安装nginx (on ubuntu1804)

future@future:~$ apt update
future@future:~$ sudo apt install nginx
future@future:~$ sudo ufw status
Status: inactive
# 如果防火墙已经关闭,则下列操作可以不用
future@future:~$ sudo ufw app list
Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH
future@future:~$ sudo ufw allow 'Nginx HTTP'
# 这条命令选用
future@future:~$ netstat -nplt

future@future:~$ systemctl  status nginx.service
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: en
   Active: active (running) since Sun 2020-01-19 15:59:56 UTC; 3min 53s ago
     Docs: man:nginx(8)
  Process: 5747 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code
  Process: 5738 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process
 Main PID: 5748 (nginx)
    Tasks: 7 (limit: 4915)
   CGroup: /system.slice/nginx.service
           ├─5748 nginx: master process /usr/sbin/nginx -g daemon on; master_pro
           ├─5751 nginx: worker process
           ├─5752 nginx: worker process
           ├─5754 nginx: worker process
           ├─5756 nginx: worker process
           ├─5758 nginx: worker process
           └─5759 nginx: worker process
              
    
future@future:~$ sudo vim /etc/nginx/sites-enabled/default
# root /var/www/html;
  root /usr/share/nginx/html;

future@future:~$ sudo sh -c "echo nginx default index file > /usr/share/nginx/html/index.html"
future@future:/etc/nginx/sites-enabled$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
future@future:/etc/nginx/sites-enabled$ sudo nginx -s reload
# 查看内容是否已经更改
future@future:/etc/nginx/sites-enabled$ curl http://xx.xx.xx.xx
nginx default index file

新建GitLab仓库

在这里插入图片描述

到已经安装Jenkins服务器上初始化update_nginx_file项目

[root@gitlab ~]# mkdir -p /data/update_nginx_file
[root@gitlab ~]# cd /data/update_nginx_file/
[root@gitlab update_nginx_file]# git clone [email protected]:Meiyan/update_nginx_file.git code
Cloning into 'code'...
warning: You appear to have cloned an empty repository.
[root@gitlab update_nginx_file]# cd code
[root@gitlab code]# echo "This is a jenkins+gitlab+ansible testing file" > index.html
[root@gitlab code]# git add .
[root@gitlab code]# git commit -m 'new index.html'
[master (root-commit) fe13400] new index.html
 1 file changed, 1 insertion(+)
 create mode 100644 index.html
[root@gitlab code]# git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:Meiyan/update_nginx_file.git
 * [new branch]      master -> master
# 创建ansible playbook
[root@gitlab ~]# cd /data/Ansible-playbook/
[root@gitlab Ansible-playbook]# vim update_nginx_file.yml
---
- hosts: webservers
  gather_facts: no


  tasks:
    - name: update_nginx_index_file
      copy: src=/data/update_nginx_file/code/index.html  dest=/usr/share/nginx/html/index.html

配置Jenkins

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

配置Gitlab web钩子

在这里插入图片描述

在这里插入图片描述

提交一个Push测试Gitlab + Jenkins + Ansible 自动部署

[root@gitlab ~]# cd /data/update_nginx_file/code/
[root@gitlab code]# echo "Gitlab+Jenkins+Ansible-playboot update index html test"  > index.html
[root@gitlab code]# git add .
[root@gitlab code]# git commit -m 'auto update index html'
[master 6ab8604] auto update index html
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@gitlab code]#  git push origin master
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 306 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:Meiyan/update_nginx_file.git
   5723535..6ab8604  master -> master
[root@gitlab code]# curl http://xx.xx.xx.xx
Gitlab+Jenkins+Ansible-playboot update index html test

在这里插入图片描述

在这里插入图片描述

异常参考

报错:-bash: /usr/share/nginx/html/index.html: Permission denied

在这里插入图片描述
在这里插入图片描述


报错环境说明:期间使用sudo echo "nginx default index file" > /usr/share/nginx/html/index.html
原因:bash 拒绝这么做,提示权限不够,是因为重定向符号 > 也是 bash 的命令。sudo 只是让 echo 命令具有了 root 权限,
但是没有让 > 命令也具有root 权限,所以 bash 会认为这个命令没有写入信息的权限。

解决方法:
"sh -c" 命令,它可以让 bash 将一个字串作为完整的命令来执行
 sudo sh -c "echo nginx default index file > /usr/share/nginx/html/index.html"
或者
future@future:~$ echo 'nginx default index file' sudo tee -'nginx default index file' /usr/share/nginx/html/index.html

nginx配置不生效,页面一直是默认页面welcome to nginx的解决办法

# ubuntu下nginx的配置文件所在目录:
vim /etc/nginx/nginx.conf
# include /etc/nginx/conf.d/*.conf;
# include /etc/nginx/sites-enabled/*;
我们对nginx的配置主要写在nginx.conf文件里,这个目录下还有conf.d和sites-enabled两个文件夹,里面为默认的配置文件。
相应的,在配置nginx,编辑nginx.conf文件时,需要把这两行注释掉,否则nginx.conf不会生效。

# 检查配置文件是否正确
nginx -t 
# 重启nginx使配置生效
nginx -s reload
# 若出现以下错误
nginx:[error] open() "/run/nginx.pid" failed (2: No such file or directory)
# 使用以下命令即可解决
nginx -c /etc/nginx/nginx.conf
nginx -s reload
# 其他可选命令:停止nginx服务
nginx -s stop

报错:Destination /usr/share/nginx/html not writable

报错环境:push代码触发Jenkins构建时
原因:文件夹没有执行权限
解决方法:修改webservers客户机 /usr/share/nginx/html 的权限
sudo chmod -R 777 /usr/share/nginx/html
发布了44 篇原创文章 · 获赞 12 · 访问量 8360

猜你喜欢

转载自blog.csdn.net/GongMeiyan/article/details/104048339
今日推荐