gitlab与jenkins的自动化部署(经过webhook与ansilble)java
一、部署介绍nginx
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端口git
二、gitlab安装 web
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安装包,而且安装
方法一、增长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
方法二、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.shell
其实只要下载到gitlab ce包(ce是社区版,不用验证的),直接安装就好了,启动命令为gitlab-ctl reconfigure安全
三、jenkins安装 bash
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) 注册用户
先开启用户注册按钮,用户注册完毕之后,关闭用户注册网络
四、配置jenkins拉取gitlab项目的帐号的sshkey架构
因为两个服务运行在同一台服务器上面,所以,为了保证jenkins服务器能拉取到 gitlab的代码,把root的用户的公钥分别上传到gitlab admin用户跟jenkins admin用户当中去。步骤我就不写了!
五、配置jenkins项目
一、登陆192.168.1.49:8180
点击新建:
依次填写相关信息:(注意,项目安全这块是在平常管理中才会用到,若是简单测试能够忽略)
源码管理:
触发器:
我这里选择得比较多,有push触发,也有merge触发
构建:(经过shell来构建)
我这里是经过简单的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来链接二者的有关系。
六、gitlab设置webhook
在对应的gitlab项目设置当中,设置webhooks
固然,这里也能够设置triger事件:
保存后会在底部,生成webhook,点击test,看jenkins界面是否触发了!
登陆jenkins界面:192.168.1.49:8180
而后大功告成!