gitlab+jenkins+ansible集成持续发布

1.Gitlab安装

gitlab 10.0.1安装使用

 

内存最少2G否则系统安装完会502报错

 

安装

如想使用Postfix来发送邮件,在安装期间请选择’Internet Site’. 您也可以用sendmai或者 配置SMTP服务  使用SMTP发送邮件.

Centos 6 系统上, 下面的命令将在系统防火墙里面开放HTTP和SSH端口. 

sudo yum install curl openssh-server openssh-clients postfix cronie policycoreutils-python git -y

sudo service postfix start

sudo chkconfig postfix on

sudo lokkit -s http -s ssh

 

添加GitLab仓库,并安装到服务器上(相对慢)

curl -sS http://packages.gitlab.cc/install/gitlab-ce/script.rpm.sh | sudo bash

sudo yum install gitlab-ce

直接下载rpm包

安装包地址

centos 6xx

https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el6/

 

centos7xx

https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/

 

汉化地址

https://gitlab.com/xhang

 

rpm -ivh gitlab-ce-10.0.1-ce.0.el6.x86_64.rpm

gitlab-ctl reconfigure

 

gitlab本身采用80端口,如安装前服务器有启用80,安装完访问会报错。需更改gitlab的默认端口。

修改vim /etc/gitlab/gitlab.rb:

external_url 'http://localhost:90'

 

unicorn本身采用8080端口,如果你那里没有8080使用,可以后边不用修改了。如安装前服务器有启用8080,安装完访问会报错。需更改unicorn的默认端口:

修改 /etc/gitlab/gitlab.rb:

unicorn['listen'] = '127.0.0.1'

unicorn['port'] = 3000

gitlab中文汉化

 

git clone https://gitlab.com/xhang/gitlab.git(下载有时候超级慢)下载相应的汉化程序

git diff v10.0.1 v10.0.1-zh > ../10.0.1-zh.diff

若以前下载的gitlab程序可以执行git pull更新到最新在执行

git diff v10.0.1 v10.0.1-zh > ../10.0.1-zh.diff

 

yum install patch

sudo patch -d /opt/gitlab/embedded/service/gitlab-rails -p1 < ../10.0.1-zh.diff

sudo gitlab-ctl start

sudo gitlab-ctl reconfigure

如果汉化中出现问题,请重新安装 GitLab(注意备份数据)。

 

2.Ansible安装

ansible

yum install epel-release -y

yum install ansible -y

创建公私钥

ssh-keygen -t rsa

导出公钥到指定控制端

ssh-copy-id -i [email protected]

编辑指定控制端,服务

vi/etc/ansible/hosts

 

Ansible内置了一些关于连接主机的变量,设置以下变量控制ansible与远程主机:

hosts文件内部支持的一些特定指令(inventory参数):

ansible_ssh_host:指定主机别名对应的真实IP,如:

ansible_ssh_host=183.60.41.251,随后连接该主机无须指定完整IP,只需指定251就行

ansible_ssh_port:指定连接到这个主机的ssh端口,默认22

ansible_ssh_user:连接到该主机的ssh用户

ansible_ssh_pass:连接到该主机的ssh密码(连-k选项都省了),安全考虑还是建议使用私钥或在命令行指定-k选项输入

ansible_sudo_pass:sudo密码

ansible_sudo_exe(v1.8+的新特性):sudo命令路径

ansible_connection:连接类型,可以是local、ssh或paramiko,ansible1.2之前默认为paramiko

ansible_ssh_private_key_file:私钥文件路径

ansible_shell_type:目标系统的shell类型,默认为sh,如果设置csh/fish,那么命令需要遵循它们语法

ansible_python_interpreter:python解释器路径,默认是/usr/bin/python,但是如要要连*BSD系统的话,就需要该指令修改python路径

ansible_*_interpreter:这里的"*"可以是ruby或perl或其他语言的解释器,作用和ansible_python_interpreter类似

配置例子

 

3.Jenkins安装

Tomcat把jenkins.war放进Tomcat web目录 执行http://192.168.253.154:8080

Jenkins安装插件Ansible pluginGit pluginGitLab PluginGitlab Hook PluginBuild Authorization Token Root Plugin 。

 

4.配置

配置jenkins链接gitlab服务的账号 此处用证书

图片.png 

新建一个自由风格的test项目

图片.png

填写gitlab项目托管地址

图片.png

生成随机token:openssl rand -hex 12

图片.png

构建执行脚本

或远程执行脚本结合ansible

ansible 192.168.253.154 -m shell -a "sh /var/xxx.sh"

图片.png 

配置gitlab

钩子地址

http://jenkins服务器地址:8080/buildByToken/build?job=项目名&token=token值

图片.png

测试显示值201为正确

图片.png

Jenkins生成出构建信息

图片.png


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章