jenkins+git+gitlab+ansible实现持续集成自动化部署静态网站--技术流ken(代码片段)

kenken2018 kenken2018     2023-01-24     519

关键词:

 

前言

 

在之前已经写了关于Git,Gitlab以及Ansible的两篇博客《Git+Gitlab+Ansible剧本实现一键部署Nginx--技术流ken》,《Git+Gitlab+Ansible剧本实现一键部署动态网站(二)--技术流ken》,以及关于jenkins的简单使用《Jenkins持续集成介绍及插件安装版本更新演示(一)--技术流ken》。相信大家也已经完全掌握了这三项工具的使用,也可以使用这几项工具可以部署静态以及动态网站了。

以前的博客可以实现一键部署网站了,但是并没有实现持续化集成部署网站。沉重的工作还是落在了可怜的运维工程师上面。

但是你有没有想过这样一个问题,假如网站全部部署成功了,现在我们的开发程序员隔三差五的修改网站上的某些功能或者修改页面内容,难道都需要运维人员再手动执行命令吗?有没有一种方法使得程序员修改完成代码之后可以自己测试、部署上线哪?

回答是有的!jenkins就可以完成上述的工作了。

本篇博客将使用git+gitlab+ansible+jenkins实现真正的持续化一键部署静态网站

 下一篇博客将介绍如何使用git+gitlab+ansible+jenkins部署一套动态的网站。敬请期待。

 

Gitlab创建项目

 

第一步:gitlab的安装即配置

请参考我之前的博客《Gitlab在linux/windows中免密使用(二)--技术流ken

 

第二步:创建项目

如下图,我创建了一个static_web的项目

技术分享图片

Git下载仓库

 

第一步:创建目录并下载仓库

[[email protected] ~]# mkdir /ken
[[email protected] ~]# cd /ken
[[email protected] ken]# git clone http://10.220.5.137/webg1/static_web.git
Cloning into static_web...
Username for http://10.220.5.137: root
Password for http://[email protected]: 
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.

 

Ansible剧本编写

 

第一步:进入到上面下载下来的工作目录中

[[email protected] ken]# ls
static_web
[[email protected] ken]# cd static_web/
[[email protected] static_web]# ls -a
.  ..  .git  README

 

第二步:使用roles来编写剧本

首先需要创建相关的目录

[[email protected] static_web]# mkdir roles/httpd/tasks,vars,files -p

 

第三步:编写tasks文件

[[email protected] static_web]# vim roles/httpd/tasks/main.yml
[[email protected] static_web]# cat roles/httpd/tasks/main.yml
- name: install httpd
  yum: name=httpd state=present
- name: start httpd
  service: name=httpd state=restarted
- name: copy test file to httpd
  copy: src=roles/httpd/files/index.html dest=/var/www/html

 

第四步:编写file下的测试文件

[[email protected] static_web]# vim roles/httpd/files/index.html
[[email protected] static_web]# cat roles/httpd/files/index.html
test for static web

 

第五步:编写主机清单

[[email protected] static_web]# cat inventory/test
[ken]
10.220.5.138

 

第六步:编写剧本文件

[[email protected] static_web]# vim ken.yml
[[email protected] static_web]# cat ken.yml
- hosts: ken
  remote_user: root
  roles:
   - httpd

 

第七步:执行剧本

 注意:在执剧本的时候需要使用-i指定你创建的主机列表清单,否则会找不到需要执行的节点

可以看到剧本执行完成也没有报错

[[email protected] static_web]# ansible-playbook -i inventory/test ken.yml 

PLAY [ken] ***********************************************************************

TASK [Gathering Facts] ***********************************************************
ok: [10.220.5.138]

TASK [httpd : install httpd] *****************************************************
changed: [10.220.5.138]

TASK [httpd : start httpd] *******************************************************
changed: [10.220.5.138]

TASK [httpd : copy test file to httpd] *******************************************
changed: [10.220.5.138]

PLAY RECAP ***********************************************************************
10.220.5.138               : ok=4    changed=3    unreachable=0    failed=0   

 

 第八步:网页浏览

现在就可以登录该节点进行访问了

技术分享图片

 

文件提交至gitlab

 

经过上面的步骤之后,已经部署完成了一个静态页面

现在把这些文件提交至gitlab

第一步:文件提交至仓库

[[email protected] static_web]# git add .
[[email protected] static_web]# git commit -m "v1"

 

第二步:报错

提交报了这个错

*** Please tell me who you are.

Run

  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

to set your accounts default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got [email protected](none))

直接执行命令

[[email protected] static_web]# git config --global user.email "[email protected]"
[[email protected] static_web]# git config --global user.name "Your Name"

 

第三步:推送至gitlab

[[email protected] static_web]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from matching to simple. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See git help config and search for push.default for further information.
(the simple mode was introduced in Git 1.7.11. Use the similar mode
current instead of simple if you sometimes use older versions of Git)

Username for http://10.220.5.137: root    ####输入连接你的gitlab的用户名
Password for http://[email protected]:    ###输入该用户密码
Counting objects: 12, done.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (11/11), 815 bytes | 0 bytes/s, done.
Total 11 (delta 0), reused 0 (delta 0)
To http://10.220.5.137/webg1/static_web.git
   c47e814..e2ac703  master -> master

 

第四步:web端查看结果

发现v1版已经上传成功

技术分享图片

 

jenkins 实现持续集成

 

经过上面的一些操作之后,我们已经完成了静态网站的部署,已经代码的上传

但是发现还是每次执行需要输入命令等

现在我们就使用jenkins来实现持续化部署

 

第一步:jenkins中创建任务

我创建了一个自由风格的软件项目

项目名称为test_for_static_web

输入完毕之后选择确定即可

技术分享图片

 

 第二步:添加源码管理信息

这里的url就是你的项目的地址

下面的凭证,点击Add输入你的gitlab的密码和账号即可

 技术分享图片

 

第三步:选择构建

增加侯建步骤选择执行shell

技术分享图片

 

第四步:编写shell

第一行:指定bash为解释器,和编写shell一样

第二行:进入到工作目录,这里引用了了一个变量,意思是工作目录,因为我们的剧本里面都是相对路径所以我们需要进入到git拉取下来的文件中进行执行可以点击下面的可用环境列表了解更多

第三行: 即执行剧本

以上步骤完成之后点击下方的保存即可

技术分享图片

 

第五步:查看执行结果

点击立即构建之后,在最下方会出现一个圆圈,这个圆圈红色代表失败,蓝色代表成功,可以鼠标放上去显示的

技术分享图片

第六步:查看失败原因

点击下方的红色圆圈即可查看失败原因

这里的失败原因是因为运行jenkins程序的是jenkins用户,我们连接节点的秘钥是root的,所以现在连接不上

技术分享图片

 

 第七步:更改jenkins的配置文件

把运行jenkins的用户更改为root即可

更改完成之后重启jenkins

[[email protected] static_web]# sed -i s/JENKINS_USER="jenkins"/JENKINS_USER="root"/ /etc/sysconfig/jenkins
[[email protected] static_web]# systemctl restart jenkins

 

第八步:再次执行构建

再次点击构建可以发现现在红色圆圈变成了蓝色的成功圆圈

技术分享图片

点击一下这个成功的圆圈查看运行过程

技术分享图片

 

 第九步:查看工作目录

许多人会疑惑,在这里使用的git clone,它把文件下载到哪里去了那?

其实是放在jenkins的工作目录之下的你的项目名称里面了,还记得我们的项目名称是test_for_static_web吗?

[[email protected] static_web]# ls /var/lib/jenkins/workspace/
 test_for_static_web/ 

查看一下这个目录下面都有什么

看到了吧,从gitlab克隆的回来的文件都放在了这里即jenkins工作目录下>你的任务名称面

[[email protected] static_web]# ls /var/lib/jenkins/workspace/test_for_static_web
inventory  ken.retry  ken.yml  README  roles

 

更改网站数据

 

现在我们的工程师就可以在他的电脑上面更改网站数据,并实现持久集成自动化部署了

第一步:克隆数据

现在我使用的电脑IP 为10.220.5.139,现在假如另外一位工程师的电脑是10.220.5.137上面

[[email protected] tmp]# mkdir p
[[email protected] tmp]# cd p
[[email protected] p]# git clone http://10.220.5.137/webg1/static_web.git
Cloning into static_web...
Username for http://10.220.5.137: root
Password for http://[email protected]: 
remote: Counting objects: 14, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 14 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (14/14), done.
[[email protected] p]# 

 

第二步:更改网站数据

[[email protected] static_web]# echo "this is new data for static web">> roles/httpd/files/index.html

 

第三步:提交

[[email protected] static_web]# git add .
[[email protected] static_web]# git commit -m "v2"
[master e5f5d42] v2
 1 file changed, 1 insertion(+)
[[email protected] static_web]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from matching to simple. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See git help config and search for push.default for further information.
(the simple mode was introduced in Git 1.7.11. Use the similar mode
current instead of simple if you sometimes use older versions of Git)

Username for http://10.220.5.137: root
Password for http://[email protected]: 
Counting objects: 11, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 443 bytes | 0 bytes/s, done.
Total 6 (delta 1), reused 0 (delta 0)
To http://10.220.5.137/webg1/static_web.git
   e2ac703..e5f5d42  master -> master

 

第三步:jenkins持续化部署

点击构建

发现没有报错

技术分享图片

 

第四步:网站查看

发现网站数据已经更新

技术分享图片

 

jenkins+ansible+gitlab自动化部署三剑客--jenkinsgit集成

JenkinsGit集成1.新建项目  2.新建gitlab项目  克隆地址  3.添加git选项  4.build并查看日志  查看详情

jenkins+ansible+gitlab自动化部署三剑客--ansible

Ansible简介Ansible是一个开源部署工具开发语言:python特点:SSH协议通讯,全平台,无需编译,模块化部署管理作用:推送Playbook进行远程节点快速部署Ansible与Chef,Saltstack的区别Chef  Ruby语言编写,C/S架构,配置需要Git依赖,Recipe... 查看详情

gitlab+jenkins日常操作(代码片段)

GitLab+Jenkins日常操作一、Jenkins回滚操作1.通过Git来回滚2.通过Build来回滚二、Jenkins实现邮件报警1.配置邮件报警3.配置项目三、Jenkins远程管理方式1.通过SSHPlugin插件2.通过Shell命令3.通过Ansible一、Jenkins回滚操作注意:Jenkins上... 查看详情

jenkins+gitlab+ansible--playbook代码上线流程(文末有免费视频)(代码片段)

 jenkins构建是支持git选择分支安装GitParameter插件在系统管理中的插件管理然后点击选插件,在右上角输入GitParameter,找到GitParameter后点击左下方的的直接安装。参数化构建过程插件安装成功后,在项目配置中后看到多了一个"... 查看详情

gitlab+jenkins+ansible集成持续发布

1.Gitlab安装gitlab10.0.1安装使用 内存最少2G否则系统安装完会502报错 安装如想使用Postfix来发送邮件,在安装期间请选择’InternetSite’.您也可以用sendmai或者配置SMTP服务 并使用SMTP发送邮件.在Centos6系统上,下面的命令将在... 查看详情

jenkins+gitlab+ansible自动化部署(代码片段)

PipelineJob实现Nginix+MySQL+PHP+Wordpress实现自动化部署交付(Jenkins+Gitlab+Ansible自动化部署(五)https://www.cnblogs.com/zd520pyx1314/p/10249094.html)环境准备编写ansibleplaybook脚本实现Wordpress远程部署将wordpress源码与playbook部署脚本提交到gitlab仓... 查看详情

jenkins+ansible+gitlab自动化部署三剑客

最近一直在学习Ansible的一些playbook的写法,所以一直没有怎么更新,想到目前大家对诸如saltstack,docker, Ansible等自动化部署相关的工具很感兴趣,但又苦于没有可学习的中文实例,这里我就把我这几个月所接触到目前国外比较流行... 查看详情

ci/cd之jenkins结合ansible部署apache(代码片段)

jenkins结合ansible部署apache演示环境部署步骤安装ansible创建新git项目ansible主控端配置jenkins测试配置用户设置jenkins部署部署测试在测试环境测试在生产环境部署建议演示环境server1 192.168.122.11 gitlab端 4G内存server2 192.168.122.12 j... 查看详情

jenkins+ansible+gitlab自动化部署三剑客-gitlab本地搭建

Jenkins+Ansible+Gitlab自动化部署三剑客-gitlab本地搭建实际操作准备linux初始环境关闭防火墙systemctlstopfirewalld开机自己关闭systemctldisablefirewalld设置安全配置为关闭vim/etc/sysconfig/selinux重启虚拟机reboot查看安全策略是否关闭getenforce安装g... 查看详情

ci/cd之jenkins结合ansible部署apache(代码片段)

jenkins结合ansible部署apache演示环境部署步骤安装ansible创建新git项目ansible主控端配置ansible主配置文件编写配置清单编写下载apache的playbook修改apache配置文件最终目录结构如下jenkins测试配置用户设置jenkins部署部署测试在测试环境测... 查看详情

jenkins+ansible+gitlab自动化部署三剑客--jenkins参数集成(代码片段)

Jenkins参数集成1.新建项目  2.添加选项       #!/bin/shecho"Currentdeployenvironmentis$deploy_env"echo"Thebuildis$version"echo"Thepaaswordis$pass"if$boolthenecho"Requestisapproved"elseecho"Requestisrejected"fi  3.BuildwithParameters&nb... 查看详情

jenkins对接gitlab和git

1需要的插件jenkins的git插件和jenkins的gitlab插件。2对接gitlab在系统配置中,随便起一个连接的名字,设置url,可以直接用ip地址,端口号默认是80,不需要写明。证书为gitlabapitoken,即gitlab的privatetoken。配置好之后,testconnection。3... 查看详情

jenkins+ansible+gitlab自动化部署三剑客--jenkinsansible集成(代码片段)

JenkinsAnsible集成准备工作参考https://www.cnblogs.com/bk770466199/p/12259007.html完成以下准备工作需要在jenkins服务器上,配置ansible2.5+python3.6虚拟环境配置jenkins.example.com主机到test.example.com主机的ssh免秘钥认证配置ansible,创建testservers文件1.... 查看详情

Jenkins 和 GitLab 的 Git SSH 签出失败

】Jenkins和GitLab的GitSSH签出失败【英文标题】:GitSSHcheckoutfailswithJenkinsandGitLab【发布时间】:2021-12-0612:28:22【问题描述】:当我尝试构建配置为从GitLab签出的Jenkins作业时,我得到以下错误输出(截断):....................ERROR:Errorfet... 查看详情

SSH 密钥未受保护的 terraform ansible jenkins

】SSH密钥未受保护的terraformansiblejenkins【英文标题】:SSHkeyunprotectedterraformansiblejenkins【发布时间】:2021-10-2010:40:42【问题描述】:我有一个gitlab存储库,其中包含我的ansible和terraform代码来部署我的基础架构。(CI/CD)我将ssh密钥存... 查看详情

ci/cd持续集成与持续交付(上)--------git,gitee远程共有仓库和gitlab私有仓库,jenkins(代码片段)

...结合远程共有仓库github/gitee三、远程私仓库gitlab搭建一、jenkins持续集成1jenkins简介2jenkins安装3.jenkins插件安装4.jenkins项目创建,每隔1分钟看gitlab的变化5.实时看gitlab的变化,g 查看详情

当由 gitlab webhook 调用时,jenkins 管道中的 git 分支名称是啥

】当由gitlabwebhook调用时,jenkins管道中的git分支名称是啥【英文标题】:whatisgitbranchnameinjenkinspipelinewheninvokedbygitlabwebhook当由gitlabwebhook调用时,jenkins管道中的git分支名称是什么【发布时间】:2019-04-0307:07:48【问题描述】:我可以... 查看详情

gitlab集成jenkins(代码片段)

项目:使用git+jenkins实现持续集成开始构建 General 源码管理 我们安装的是Git插件,还可以安装svn插件 我们将git路径存在这里还需要权限认证,否则会出现error  我们添加一个认证 选择一下认证方式(我... 查看详情