gitlab与jenkins的自动化部署(通过webhook与ansible)
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
点击新建:
依次填写相关信息:(注意,项目安全这块是在日常管理中才会用到,如果简单测试可以忽略)
源码管理:
触发器:
我这里选择得比较多,有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来连接两者的有关系。
6、gitlab设置webhook
在对应的gitlab项目设置当中,设置webhooks
当然,这里也可以设置triger事件:
保存后会在底部,生成webhook,点击test,看jenkins界面是否触发了!
登录jenkins界面:192.168.1.49:8180
然后大功告成!
转载于:https://www.cnblogs.com/to-be-rich/p/7655838.html
gitlab与jenkins的自动化部署(通过webhook与ansible)相关推荐
- 基于gitlab和jenkins的自动化部署实例
gitlab+jenkins服务简述: GitLab是一个代码仓库,用来管理代码.Jenkins是一个自动化服务器,可以运行各种自动化构建.测试或部署任务. 所以这两者结合起来,就可以实现开发者提交代 ...
- springboot 使用Jenkins实现自动化部署
springboot 使用Jenkins实现自动化部署 简介 Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持 ...
- Docker + GitLab CI/CD 实现自动化部署
使用Docker+gitlab ci/cd 实现自动化部署 文章目录 使用Docker+gitlab ci/cd 实现自动化部署 1. Docker 1.1 Docker是什么? 1.2 Docker ...
- [小题大做] Github + Jenkins 实现自动化部署 hexo 博客静态文件
使用jenkins来部署hexo简直就是小题大做,但是偶尔这样小题大做还真有折腾的乐趣 背景 jianyujianyu.com之前是使用typecho做博客的. 原因很简单,wordpress太重,h ...
- linux下jenkins实现自动化部署(超详细)
本文参照:https://blog.csdn.net/achuDk/article/details/78925081 但是有给出我在搭建中遇到的问题,和我解决的方法.现在我的配置是需要手动一键点击部署 ...
- 基于Docker、Jenkins实现自动化部署
概述 最近在写一个基于SpringBoot的小项目,由于每次部署到服务器都需要经历几个繁琐的过程,随在自己的服务器上安装了Jenkins和Docker实现自动化部署.至此,将整个过程记录如下. 安装与 ...
- 使用Jenkins实现自动化部署
一.原理 开发人员提交完代码至版本控制器后,由Jenkins自动获取远程代码,自动打包,然后再自动发布的过程. 二.使用Docker安装Jenkins 前提是linux中已经安装Docker,再执行以 ...
- 搭建Jenkins实现自动化部署环境
大家好,我是阿飞云 怕什么真理无穷,进一步有近一步的欢喜 本文是团队内小兄弟的学习笔记整理而来.看完本文大体了解为什么要使用Jenkins,部署Jenkins依赖的环境,以及如何用Jenkins进 ...
- 分布式电商项目——16.基于Docker+Jenkins实现自动化部署
参考文档:https://blog.csdn.net/ming19951224/article/details/80958761 https://blog.csdn.net/cdszdd8/artic ...
最新文章
- 加解密基础——(对称加密、非对称加密和混合加密)
- 10万奖金:天池OCR大赛来了!
- python 搭建登陆系统_Django 搭建CMDB系统完整[1](用户登录)
- balanced binary search tree
- Python学习之路 (六)爬虫(五)爬取拉勾网招聘信息
- AutoML:机器学习的下一波浪潮
- MySQL高级 - SQL优化 - 子查询优化
- 【HTML/CSS】margin塌陷和合并问题
- Linux系统:centos7下搭建ElasticSearch中间件,常用接口演示
- knime二次开发节点的项目结构
- 使用 windows命令和iconv.exe批量转换文件编码
- GRE零基础50-60天出分随感--V159 Q170
- 三维GIS+电力巡检,开启电缆通道数字化巡检新局面
- 软件测试培训费多少钱?贵吗?靠谱吗?
- 网络电台mms地址大全(一)
- UEFI电脑安装Win7并激活
- react.js 原生文字下划线标注功能开发
- ANT下载与安装--windows
- (poj 2377)Kruskal算法 最大生成树
- android dialog隐藏虚拟按键,Android应用开发之Android dialog 去除虚拟按键的解决方法...