设为首页 收藏本站
查看: 1598|回复: 0

[经验分享] gitlab与jenkins的自动化部署(通过webhook与ansible)

[复制链接]

尚未签到

发表于 2017-11-22 15:02:05 | 显示全部楼层 |阅读模式
  gitlab与jenkins的自动化部署(通过webhook与ansilble)

  1、部署介绍
  gitlab服务器:192.168.1.49:80
jenkins服务器:192.168.1.49:8180
ps:由于两个服务运行在同一台服务器上面,因此,为了保证jenkins服务器能拉取到 gitlab的代码,把root的用户的公钥分别上传到gitlab admin用户跟jenkins admin用户当中去。jenkins我没有使用默认的端口8080,主要是我公司的gerrit架构会用到这个8080端口,所以为了防止冲突,改为8180端口

  2、gitlab安装
  a) 第一步:安装相应的在依赖包(postfix或者sendmail),其中选择internet site这种网络smtp邮件服务器这种方式来发送邮件,其他一些依赖包:curl \openssh-server\ ca-certificates\ postfix

If you install Postfix to send email please select 'Internet Site' during setup. Instead of using Postfix you can also use Sendmail or configure a custom SMTP server . If you wish to use Exim, please configure it as an SMTP server.
On Centos 6 and 7, the commands below will also open HTTP and SSH access in the system firewall.

操作命令:
sudo apt-get install curl openssh-server ca-certificates postfix


b) 第二步:Add the GitLab package server and install the package. 下载gitlab安装包,并且安装
方法1、增加yum源链接,使用sduo apt-get install gitlab-ee
Apt-get安装Curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh|sudo bash
操作命令:
sudo apt-get install gitlab-ce

If you are not comfortable installing the repository through a piped script, you can find the entire script here .
Alternatively you can select and download the package manually and install using
方法2、deb包安装下载deb包,然后安装

下载: sudo  wget https://packages.gitlab.com/gitlab/gitlab-ee
安装:     sudo dpkg -i gitlab-ce_8.1.2-ee.0_amd64.deb

建议一般下载后再装

c)第三步,启用配置,并且启动 GitLab
操作命令:
sudo gitlab-ctl reconfigure

d) 第四步:打开网页,进行注册,使用默认密码
http://192.168.0.177:80
Username: rootPassword: 5iveL!fe

e) 第五步:2.1.6. Upload your license(EE版本需要认证,CE)      这步我操作的时候是没有的
Go toAdmin > Licenseand upload your.gitlab-licensefile.
Don't have a license yet? Get a subscription.
  其实只要下载到gitlab ce包(ce是社区版,不用验证的),直接安装就行了,启动命令为gitlab-ctl reconfigure

  3、jenkins安装
  a) Install Jenkins
wget -q -O - https://jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins

b) Starting Jenkins
  
nohup java -Dorg.jenkinsci.plugins.gitclient.Git.timeOut=60  -Dfile.encoding=UTF-8 -Dhudson.DNSMultiCast.disabled=true -jar /usr/share/jenkins/jenkins.war --httpPort=8180    > /var/log/jenkins_log 2>&1  &

(注意--httpPort=8180放在后面)

c) 开启                                                             jenkins页面为:192.168.0.179:8180
http://xxxxx:8180/

d) 插件安装
Git Plugin
SSH Plugin
URL Trigger Plugin
Gerrit Trigger Plugin
Gitlab Hook
Gitlab Merge Request Builder
GitLab Plugin

e) 注册用户
先开启用户注册按钮,用户注册完毕以后,关闭用户注册

  4、配置jenkins拉取gitlab项目的账号的sshkey
  由于两个服务运行在同一台服务器上面,因此,为了保证jenkins服务器能拉取到 gitlab的代码,把root的用户的公钥分别上传到gitlab admin用户跟jenkins admin用户当中去。步骤我就不写了!

  5、配置jenkins项目
  1、登录192.168.1.49:8180
点击新建:
DSC0000.png

  

依次填写相关信息:(注意,项目安全这块是在日常管理中才会用到,如果简单测试可以忽略)
DSC0001.png

源码管理:
DSC0002.png

  
触发器:
  我这里选择得比较多,有push触发,也有merge触发
DSC0003.png
  构建:(通过shell来构建)
DSC0004.png


  我这里是通过简单的ansible来控制web服务器的压缩包scp,tar解压,还有指定软联接
#变量WORKSPACE、GIT_URL是jenkins默认内置的变量



#!/bin/bash
#变量WORKSPACE,GIT_URL是jenkins默认提供的变量
cd $WORKSPACE
#截取giturl中的产品
product=`echo $GIT_URL| awk -F"/" '{print $NF}'|sed 's/\.git//g'`
#截取代码当前的前七位sha值
sha=`cat .git/HEAD|cut -b 1-7`
#指定日期
d=`date +"%Y%m%d"`
#指定压缩包的路径与包全称,包名(不带后缀)
p_pathname=/tmp/"$product"_"$d"_"$sha".tar.gz
p_name="$product"_"$d"_"$sha".tar.gz
p_productname="$product"_"$d"_"$sha"
#本地压缩
tar -cf $p_pathname  $WORKSPACE/*
#指定web服务器的包的存放位置
p_store=/var/prod/"$product"/"$d"
#指定web服务器的包解压位置
p_target=$p_store/$p_productname
#指定nginx网页代码位置
www=/mnt/www/launcher.szprize.cn
#dev代表web服务器,确保包解压路径存在
ansible dev  -m shell -a "mkdir -p $p_target "
#dev代表web服务器,ansible copy模块远程复制
ansible dev -m copy -a "src=$p_pathname dest=$p_store/"
#ansible 远程解压
ansible dev -m shell -a "tar -xf $p_store/$p_name -C $p_target"
#ansible 软联接
ansible dev -m file -a "src=$p_target dest=$www state=link"#!/bin/bash
  jenkins这边就配置完了

但是注意:jenkins跟gitlab怎么通信触发呢?

大家请留意触发器那块有一句话:
Build when a change is pushed to GitLab. GitLab CI Service URL: http://192.168.1.49:8180/project/launcher
它的意思的,gitlab ci接口可以通过http://192.168.1.49:8180/project/launcher来连接两者的有关系。




  6、gitlab设置webhook
  在对应的gitlab项目设置当中,设置webhooks
DSC0005.png
  当然,这里也可以设置triger事件:
DSC0006.png
  保存后会在底部,生成webhook,点击test,看jenkins界面是否触发了!
  
DSC0007.png


登录jenkins界面:192.168.1.49:8180
DSC0008.png
  然后大功告成!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-409530-1-1.html 上篇帖子: 从git到lnmp代码发布 下篇帖子: git服务器搭建
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表