gitlab+jenkins的搭建

2017-01-08 08:26:16来源:CSDN作者:sinat_25306771人点击

搭建环境:两台centos6.7 64位

一 gitlab的安装

  gitlab代码管理仓库:

  1、按照gitlab 官方提供的文档,安装如下包:

[root@node02 ~ 10:13:35]#yum install curl policycoreutils openssh-server openssh-clients postfix


2、启动postfix 和sshd 服务:

[root@node02 ~ 10:50:18]#service postfix start 


[root@node02 ~ 10:50:37]#service sshd start



3、受长城防火墙的影响,我们去清华大学提供的源上下载gitlab-8.6.5 的 rpm 包:

[root@node02 ~ 10:52:26]#wget -O /usr/local/src/gitlab-ce-8.6.5-ce.0.el7.x86_64.rpm https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-8.6.5-ce.0.el7.x86_64.rpm



4、安装下载好的包:

[root@node02 ~ 10:53:27]#rpm -ivh /usr/local/src/gitlab-ce-8.6.5-ce.0.el7.x86_64.rpm 

Preparing...                    ################################# [100%]

Updating / installing...

1:gitlab-ce-8.6.5-ce.0.el7        ################################# [100%]

gitlab: Thank you for installing GitLab!

gitlab: To configure and start GitLab, RUN THE FOLLOWING COMMAND:

sudo gitlab-ctl reconfigure

gitlab: GitLab should be reachable at http://node02.com

gitlab: Otherwise configure GitLab for your system by editing /etc/gitlab/gitlab.rb file

gitlab: And running reconfigure again.

gitlab: 

gitlab: For a comprehensive list of configuration options please see the Omnibus GitLab readme

gitlab: https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md

gitlab: 

It looks like GitLab has not been configured yet; skipping the upgrade script.



5、在执行初始化配置之前,建议修改gitlab 仓库路径,否则后期修改比较麻烦:

[root@node02 ~ 10:54:47]#vim /etc/gitlab/gitlab.rb

git_data_dir "/data/gitlab"

external_url 'http://ip'

gitlab_rails['gravatar_plain_url'] = 'http://www.gravatar.com/avatar/%{hash}?s=%{size}&d=identicon'

6、执行gitlab-ctl reconfig初始化gitlab:

[root@node02 ~ 10:55:48]#gitlab-ctl reconfigure

Running handlers:

Running handlers complete

Chef Client finished, 220/307 resources updated in 02 minutes 03 seconds

gitlab Reconfigured!


在windows 的浏览器使用gitlab 服务器的IP 访问测试,是否能打开gitlab 主页:

初次访问,要求更改管理员 root 的登录密码(gitlab-7.x 不同),更改后登陆:


登陆后什么都没有,默认进入当前用户的项目页面:

二 Jenkins:持续集成最关键的服务。

1、从官方下载jdk-8 到 /usr/local/src 目录下:

[root@node03 ~ 11:07:00]#wget -O /usr/local/src/server-jre-8u77-linux-x64.tar.gz http://download.oracle.com/otn-pub/java/jdk/8u77-b03/server-jre-8u77-linux-x64.tar.gz?AuthParam=1460646516_dc62b6fbccd46c5f5a038f6bfd7ef428



2、解压到/usr/local/目录下:

[root@node03 ~ 11:07:49]#tar -zxf /usr/local/src/server-jre-8u77-linux-x64.tar.gz -C /usr/local/

下载Maven 到本地的/usr/loca/src 目录,并解压。

[root@node03 ~ 18:08:53]#wget -O /usr/local/src/apache-maven-3.3.9.tar.gz http://www.linuser.com/share/soft/apache-maven-3.3.9-bin.tar.gz


[root@node03 ~ 18:10:14]#tar -zxf /usr/local/src/apache-maven-3.3.9.tar.gz -C /usr/local/src/

将maven 解压目录移动到 /usr/local 目录下并重命名为maven:

[root@node03 ~ 18:10:28]#mv /usr/local/src/apache-maven-3.3.9 /usr/local/maven


3、同时,配置其系统环境变量:

[root@node03 ~ 18:11:59]#vim  /etc/profile

JRE_HOME=/usr/local/jdk1.8.0_77/jre

JAVA_HOME=/usr/local/jdk1.8.0_77

CLASSPATH=/usr/local/jdk1.8.0_77/jre/lib:/usr/local/jdk1.8.0_77/lib:/usr/local/jdk1.8.0_77/lib/tools.jar

MAVEN_HOME=/usr/local/maven

PATH=$PATH:/usr/local/jdk1.8.0_77/bin:/usr/local/jdk1.8.0_77/jre/bin:/usr/local/maven/bin


[root@node03 ~ 18:11:59]#source /etc/profile

验证:

[root@node03 ~ 18:17:13]#mvn -version

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T11:41:47-05:00)

Maven home: /usr/local/maven

Java version: 1.8.0_77, vendor: Oracle Corporation

4、下载jenkins 官方提供的稳定版的源:

[root@node03 ~ 11:08:33]#wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo


5、导入官方key:

[root@node03 ~ 11:09:27]#rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key


6、安装jenkins:

[root@node03 ~ 11:09:45]#yum install -y jenkins


7、编辑jenkins 的主配置文件,修改jenkins 家目录及配置使用内存:

[root@node03 ~ 11:13:30]#vim /etc/sysconfig/jenkins

JENKINS_HOME="/data/jenkins"


8、编辑jenkins 启动脚本文件,在脚本文件的candidates 中定义java 的路径:

[root@node03 ~ 11:16:59]#vim /etc/init.d/jenkins

candidates="

/usr/local/jdk1.8.0_77/bin/java   #自定义的java 路径

/etc/alternatives/java

/usr/lib/jvm/java-1.6.0/bin/java

/usr/lib/jvm/jre-1.6.0/bin/java

/usr/lib/jvm/java-1.7.0/bin/java

/usr/lib/jvm/jre-1.7.0/bin/java

/usr/lib/jvm/java-1.8.0/bin/java

/usr/lib/jvm/jre-1.8.0/bin/java

/usr/bin/java”


9、创建jenkins 的家目录:

[root@node03 ~ 11:15:44]#mkdir -p /data/jenkins


10、修改家目录的所属主和组都为jenkins

[root@node03 ~ 11:15:57]#chown -R jenkins:jenkins /data/jenkins


11、要真正修改jenkins 家目录,必须执行usermod:

[root@node03 ~ 11:16:11]#usermod -d /data/jenkins jenkins


12、启动jenkins:

[root@node03 ~ 11:18:45]# service  jenkins start

13、利用Jenkins IP+8080 端口访问Jenkins 首页:

三  windows git客户端的安装及生成密钥对


因为前面已经使用了gitlab 来做代码托管,那么我们研发就需要使用git 客户端工具来pull |push 代码,又因大部分程序员都是在windows 下,所以这里我们使用GIT GUI 来做客户端(当然,首推git 乌龟工具):

1、电脑安装git-base 工具,下载地址https://git-scm.com/download/ ,安装步骤如下:

2、运行git 后,执行ssh-keygen:


3、查看生成的密钥对公钥的内容,后面再创建项目的时候会用上:

$ cat ~/.ssh/id_rsa.pub

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4z4hBu5pUWQIYu+AU1Hsr3yPlT08L+Xyw3kUnC1bYg1TvHfy6B8lzKD9vCBPoJAa01iYp868PeMcojuXBfg7x0+6ZZaI/x7kJnaU4kClPf3hwPVNppdl+mTmoghotA5NbTo9eZs68qj8MIsyWnsX0ev/TqgfzwfXCXLNDHYUnKdZEIdK9zfmCIpJq5rJZ/4rVChQSXombtH/GNZtzNkbkhUZj7daj1/KlQTW2RZHH0YNTIreHJnbM715Ay8I7WdEIVATBto19OjtKJY9wfOxgqcbMjI7WhmOF554iDuDJE2NJIUpMbjV26c8uAJV92rCkemHQvg94On2ScJ9FeiFj Linuser@DESKTOP-K8HE3J4




四 Jenkins 安全配置

    

1、 点击jenkins web 页的Manage Jenkins,然后点击 Configre Global Security,进行安全配置:

2、启用安全设置,使用Jenkins 自带数据库,只允许登录用户做任何操作:

3、点击保存后,要求设置管理员账号信息:

4、点击注册后,会自动跳转登入Jenkins:

五 Jenkins 插件升级及安装

      1、Jenkins 主页,点击Manager Jenkins---Manage Plugins:

 

    2、进入后,我们可以看到有很多插件可以升级,点击最下面的all,全部选中,然后点击Download now and install after restart:

选中前:


进入安装插件页面(这里因为Jenkins 默认是ping 谷歌来检测你的网络连通性,所以才会有上面的提醒):


3、所有的插件都下载完成,先别急着勾选Restart Jenkins when installation is complete and no jobs are running。点击左侧的Manage Pluagins---Manage Plugins,回到插件管理页面,点击Available,在Filter 中输入如下插件并选中:GitLab Plugin、Git client plugin、GitLab Hook Plugin、Git plugin、SonarQube Plugin、Pubish Over SSH(批量发布登录插件)。最后点击 Download now and install after restart:



4、此时可以看到回到了上面升级插件的页面继续下载刚刚选中要安装的插件,这一次,我们就勾选 Restart Jenkins when installation is complete and no jobs are running:


等所有插件都下载完成,Jenkins 将会自动重启!

六 系统基础配置

      1、回到系统Jenkins 管理页,点击Manage Jenkins---Global Tool Configuration,进入系统配置属性页:


          2、找到Maven Configuration 配置项,在默认配置项中选择 Setting file in filesystem,在 file path 中输入Maven 所在的路径:


      3、JDK 属性配置:


4、在配置git 前,请先登录服务器,使用yum 命令安装git 工具:

[root@node03 ~ 18:17:46]#yum install -y git



安装完成,找到git 可执行命令所在的路径:

[root@node03 ~ 10:24:21]#whereis git

git: /usr/bin/git /usr/share/man/man1/git.1.gz


Git 属性配置:


      5、找到下面的Git plugin,配置git 客户端全局配置。点击最下面的Save 保存上面的配置:


七 创建第一个gitlab 仓库
1、由于是新装的GitLab,所以登录后就直接在欢迎页面,直接点击+New Project ,开始创建第一个项目:


2、自定义项目名称,这里以project-test 为名,创建一个私有项目。然后点击下面的Create project:


3、项目创建好了,提示没有ssh key。点击add an SSH key,添加公钥(下面创建)内容:


4、点击上面的add an SSH key,进入密钥添加页。在Key 下面粘贴公钥的内容(注意:内容必须一致),Titile 随便自定义,最后点击Add key:


5、密钥添加完成,会看到:


6、    测试在git 工具中将上面建的项目拉去到本地,看是否需要密码验证。现在windows 的E 盘下创建一个test 的文件夹,然后在git 工具中切换到test 目录:


测试前,请确保你电脑有做本地hosts 解析,否则请将下面的域名替换成你gitlab 服务器的IP地址


测试将项目克隆到test 目录:

  1. Linuser@DESKTOP-K8HE3J4 MINGW64 /e/test

  2. $ git clone git@node02.com:root/project-test.git

  3. Cloning into 'project-test'...

  4. warning: You appear to have cloned an empty repository.

  5. Checking connectivity... done.


  6. Linuser@DESKTOP-K8HE3J4 MINGW64 /e/test

  7. $ ls

  8. project-test/

八 git 客户端配置

      1、继上面,我们进入克隆下来的project-test 目录:

Linuser@DESKTOP-K8HE3J4 MINGW64 /e/test

$ cd project-test/

2、配置全局用户及email:

Linuser@DESKTOP-K8HE3J4 MINGW64 /e/test/project-test (master)

$ git config --global user.name "qiuli"


Linuser@DESKTOP-K8HE3J4 MINGW64 /e/test/project-test (master)

$ git config --global user.email "qiul@homeking365.com"


3、创建测试文件,添加至仓库,提交并推送至远程仓库:

Linuser@DESKTOP-K8HE3J4 MINGW64 /e/test/project-test (master)

$ touch 1.txt


Linuser@DESKTOP-K8HE3J4 MINGW64 /e/test/project-test (master)

$ git add ./


Linuser@DESKTOP-K8HE3J4 MINGW64 /e/test/project-test (master)

$ git commit -m 'add 1.txt'

[master (root-commit) 3fdf410] add 1.txt

1 file changed, 0 insertions(+), 0 deletions(-)

create mode 100644 1.txt


Linuser@DESKTOP-K8HE3J4 MINGW64 /e/test/project-test (master)

$ git push

Counting objects: 3, done.

Writing objects: 100% (3/3), 204 bytes | 0 bytes/s, done.

Total 3 (delta 0), reused 0 (delta 0)

To git@node02.com:root/project-test.git

* [new branch]      master -> master


4、登录GitLab web 端,进入项目,可以看到提交记录:

九 Jenkins 创建一个自由软件项目

      1、回到Jenkins 主页,点击New Item,进入项目创建属性页。在Item name 自定义项目名称,在项目类型中选择Freestyle project:


2、点击Ok 后,会进入项目详细配置页。Description 描述信息无所谓。在Source Code Management 中选中Git,配置GitLab 中的项目地址:


同时点击Credentials 配置gitlab 项目仓库所属主登录用户及密码:


3、配置触发条件。这里选择当有更新就自动构建,如下:


4、配置构建后的操作,这里选择发送邮件。最后点击保存:


5、登录gitlab ,进入构建的项目中,点击右侧的编辑项目:



6、在编辑属性页中点击左侧的webhooks 标签,将上面Jenkins 的地址填入到URL中,最后点击Add Webhook:


7、由上面的gitlab 可知,我们的仓库中已经有了1.txt 这个文件,现在,我们来编辑该文件,添加一些内容:

Linuser@DESKTOP-K8HE3J4 MINGW64 /e/test/project-test (master)

$ echo 'Test Jenkins auto build' > 1.txt



现在我们来提交并推送到gitlab , 看jenkins 会不会自动构建:

Linuser@DESKTOP-K8HE3J4 MINGW64 /e/test/project-test (master)

$ git add .

warning: LF will be replaced by CRLF in 1.txt.

The file will have its original line endings in your working directory.


Linuser@DESKTOP-K8HE3J4 MINGW64 /e/test/project-test (master)

$ git commit -m 'change 1.txt'

[master warning: LF will be replaced by CRLF in 1.txt.

The file will have its original line endings in your working directory.

907b4b9] change 1.txt

warning: LF will be replaced by CRLF in 1.txt.

The file will have its original line endings in your working directory.

1 file changed, 1 insertion(+), 1 deletion(-)


Linuser@DESKTOP-K8HE3J4 MINGW64 /e/test/project-test (master)

$ git push

Counting objects: 3, done.

Delta compression using up to 8 threads.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 281 bytes | 0 bytes/s, done.

Total 3 (delta 0), reused 0 (delta 0)

To git@node02.com:root/project-test.git

   d109723..907b4b9  master -> master



同时在Jenkins 的项目中可以看到有构建了:


点击构建历史,进入看下构建日志(没发邮件,是因为上面勾选了Send e-mail for every unstable build):


登录Jenkins 服务器,查看工作目录下的的1.txt 的内容;

[root@node03 ~ 11:01:48]#cat /data/jenkins/workspace/project-test/1.txt

Test Jenkins auto build


8、更改变更自动构建为定时构建,就是多长时间它自动去构建一次,如下(我们在上面这个项目的基础上将之前的uild when a change is pushed to GitLab 去掉,改选Build periodically,然后做定时):


为了区分构建,这里我把构建后操作发送的邮件取消,这样一旦构建就会发送邮件了


下面,我们在windows 上的仓库目录中1.txt 文件中新增一点内容,然后推送到服务器端:

Linuser@DESKTOP-K8HE3J4 MINGW64 /e/test/project-test (master)

$ echo '10 minute auto build' >> 1.txt


Linuser@DESKTOP-K8HE3J4 MINGW64 /e/test/project-test (master)

$ git add .

warning: LF will be replaced by CRLF in 1.txt.

The file will have its original line endings in your working directory.


Linuser@DESKTOP-K8HE3J4 MINGW64 /e/test/project-test (master)

$ git commit -m 'change'

[master warning: LF will be replaced by CRLF in 1.txt.

The file will have its original line endings in your working directory.

3d86eb7] change

warning: LF will be replaced by CRLF in 1.txt.

The file will have its original line endings in your working directory.

1 file changed, 1 insertion(+)


Linuser@DESKTOP-K8HE3J4 MINGW64 /e/test/project-test (master)

$ git push

Counting objects: 3, done.

Delta compression using up to 8 threads.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 298 bytes | 0 bytes/s, done.

Total 3 (delta 0), reused 0 (delta 0)

To git@node02.com:root/project-test.git

   0e5337b..3d86eb7  master -> master


代码推送后,很明显没有自动构建了:


等待几分钟,会发现:


再次看服务器上的1.txt 文件:

[root@node03 ~ 13:28:21]#cat /data/jenkins/workspace/project-test/1.txt

Test Jenkins auto build

10 minute auto build


如果需要发布代码只要在系统配置里面的管理节点里新建节点,这里就不详细介绍了,java发布的话需要安装私服(maven)


最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台