git+jenkins配置

来源:互联网 发布:数据库镜像与备份 编辑:程序博客网 时间:2024/06/01 07:59

一、git、jenkins及mvn的介绍

git是一个版本控制工具,相比于svn是一种分布式的工具。Git在每个用户都有一个完整的服务器(即本地服务),然后在有一个中央服务器,用户可以先将代码提交到本地,没有网络也可以先提交到本地,然后在有网络的时候再提交到中央服务器,这样就大大方便了开发者,而相比CVS和SVN都是集中式的版本控制系统,工作的时候需要先从中央服务器获取最新的代码,改完之后需要提交,如果是一个比较大的文件则需要足够快的网络才能快速提交完成,而使用分布式的版本控制系统,每个用户都是一个完整的版本库,即使没有中央服务器也可以提交代码或者回滚,最终再把改好的代码提交至中央服务器进行合并即可。

jenkins是一个持续集成工具,持续集成就是说可以不断的把开发提交的代码持续的进行打包、发布,过程呈现为自动化方式。

maven是java的构建工具,主要是打包。

二、服务的架构及实现目的

git服务器  10.2.3.32   jenkins服务器  10.2.3.39

搭建gitlab和jenkins,实现jenkins可以连接git拉取代码,在jenkins服务器上使用mvn进行编译,然后通过脚本部署到远端服务器上。

三、git部署

以下操作在git服务器10.2.3.32上进行

1、gitlab部署过程

# yum install vim gccgcc-c++wget net-toolslrzsziotoplsofiotop bash-completion -y

# yum install curl policycoreutilsopenssh-serveropenssh-clients postfix -y
# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# vim /etc/yum.repos.d/gitlib.repo    ##配置yum源
[gitlab-ce]
name=gitlab-ce
baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key
# yum install gitlab-ce       ##至此,安装完成,下面是配置文件的修改
# vim /etc/gitlab/gitlab.rb
external_url ‘http://10.2.3.32‘  #服务器IP地址,这里一定要修改,因为在git上新增用户是没有密码的,因此该新用户在第一次登陆时需要要发邮件重设密码,链接中会显示这个
#gitlab-ctl reconfigure        #配置文件修改之后都需要执行这一条
#gitlab-ctl start    #启动服务,若出现80和8080端口说明成功(gitlab在初始化时会默认占用80和8080端口,因此,这两个端口事先不能被占用)

至此,gitlab部署完成。

2、git客户端安装

#yum -y install git     

#which git    #查看git命名的位置
3、gitlab的汉化

(1)确认当前版本

cat /opt/gitlab/embedded/service/gitlab-rails/VERSION      ##当前安装版本是10.1.4,因此中文补丁需要打10.1.4版本。

(2)克隆 GitLab 汉化版的源码仓库

git clone https://gitlab.com/xhang/gitlab.git          ##原来的https://gitlab.com/larryli/gitlab.git作者已经停止维护,停留在8.8版本

(3)生成补丁文件
cd /root/gitlab/      ##先切换到克隆好的gitlab目录

git diff origin/8-17-stable 8-17-stable-zh > /tmp/8.17.diff     ##8.17 版本的汉化补丁(8-17-stable是英文稳定版,8-17-stable-zh是中文版,两个 diff 结果便是汉化补丁)

(4)打上汉化补丁
sudo gitlab-ctl stop           # 停止 gitlab
cd /opt/gitlab/embedded/service/gitlab-rails 
git apply /tmp/8.17.diff      # 应用汉化补丁
sudo gitlab-ctl start            # 启动gitlab
执行完毕后,稍等片刻,打开网站,汉化完成。
4、部署过程中的问题

(1)在将注册入口关闭后,重新登录时出现“no authentication methods configured”,此时的解决办法为

#gitlab-psql gitlabhq_production

打开注册

#UPDATE application_settings set signup_enabled=true;

打开登录

#update application_settings set password_authentication_enabled = true
\q

重新启动gitlab    

#gitlab-ctl restar

(2)


5、gitlab-ctl的常见命令

sudo gitlab-ctl start                #启动所有 gitlab 组件
sudo gitlab-ctl stop                   #停止所有 gitlab 组件
sudo gitlab-ctl restart                    #重启所有 itlab 组件
sudo gitlab-ctl status                    #查看服务状态
sudo gitlab-ctl reconfigure                #启动服务
sudo vim /etc/gitlab/gitlab.rb                #修改默认的配置文件
gitlab-rake gitlab:check SANITIZE=true --trace  #检查gitlab


4、gitlab使用需知

(1)群组的概念

群组group是一个权限的概念,是一个集合。设置完群组之后,在创建项目project时,项目的路径是“IP+群组or用户+项目名称”的构成方式,也即这个群组内的成员对项目有权限,具体的单个用户的权限是在增加该用户时设置的,分为“owner、、、”等。

可知,一个群组下可有多个项目,一般的命名群组也以公司内部的一个经营单元来命名,比如P2P项目,B2B商城项目等。

(2)项目的概念

项目project是指一个具体的开发工程,例如web-mobile、service-mobile等。对于开发过程,这里尤其注意的是,在创建时,其权限应该设置为“public”,这样是为了使持续集成工具(例如jenkins)拉取代码时可进行直接远程拉取。

四、jenkins部署

jenkins是一个war包,只需要在服务器上配置好java环境,然后以一个tomcat运行即可。需要说明的几点在于启动后的几点

1、插件安装

由于是和git连接,需要安装Gitlab Plugin和Gitlab HOOK  Plugin插件,所以这两个插件必须有。此外还需要Maven Invoker plugin和 Multiple SCMs插件,一个是maven命令,一个是支持多git仓库的。

2、创建job

job可以被理解为打包为一个war或者jar的任务,里面可能会有多个git上的工程仓库路径,经编译后达成一个包。这里需要说明的是git上的仓库地址有两种,一种为http,一种为ssh。在job的仓库地址中,因为在git上项目都是public的,因此这里的地址可以写成“ssh://git@10.2.3.32:22/root/test.git”,注意在git上直接粘贴过来的地址为“git@10.2.3.32:root/test.git ”,这里是把冒号:改为斜线/(若使用的端口不是默认的,还要写端口号)。

PS:可以看到,不需要进行任何秘钥对的配对和安装,这已经是应用层的拉取。

五、maven的安装及配置

maven的安装及配置只是要在jenkins的服务器上有maven命令即可,因此比较简单

(1)安装

解压包 apache-maven-3.0.5-bin.tar.gz即可

(2)配置

vim  /etc/profile    增加以下内容,配置环境变量

##maven env
export MAVEN_HOME=/usr/local/maven-3.0.5
export PATH=$PATH:$MAVEN_HOME/bin

source  /etc/profile   使得变量生效

(3)配置conf/下的settings.xml

此配置文件主要配置私有库地址,私有库登录用户(用于从私有库下载依赖包到本地库),插件库地址等;

这里不做任何配置,因为对于一般的小的开发项目,仅仅是使用maven的clean、install,且install也是install到本地库,不会往私有库上传jar包的。因此这里只需要在jenkins服务器上获得maven的命令即可。

(4)一个maven项目的编译过程(以下为实际项目中构建的过程,可以看到比较清晰的过程)

Building in workspace /root/.jenkins/workspace/jht-web-mobile > /usr/local/git/bin/git rev-parse --is-inside-work-tree # timeout=10Fetching changes from the remote Git repository > /usr/local/git/bin/git config remote.origin.url ssh://git@10.108.0.99:222/jht/jht-web-mobile.git # timeout=10Fetching upstream changes from ssh://git@10.108.0.99:222/jht/jht-web-mobile.git > /usr/local/git/bin/git --version # timeout=10 > /usr/local/git/bin/git fetch --tags --progress ssh://git@10.108.0.99:222/jht/jht-web-mobile.git +refs/heads/*:refs/remotes/origin/* > /usr/local/git/bin/git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > /usr/local/git/bin/git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10Checking out Revision 306f1dfaa9a01e9b792b983141a7a2be19e315bd (refs/remotes/origin/master) > /usr/local/git/bin/git config core.sparsecheckout # timeout=10 > /usr/local/git/bin/git checkout -f 306f1dfaa9a01e9b792b983141a7a2be19e315bd > /usr/local/git/bin/git rev-list 306f1dfaa9a01e9b792b983141a7a2be19e315bd # timeout=10[jht-web-mobile] $ /usr/local/maven-3.0.5/bin/mvn clean install    ##开始执行mvn clean install[INFO] Scanning for projects...[WARNING] [WARNING] Some problems were encountered while building the effective model for com.jinhuitong.web:jht-web-mobile:war:1.0-SNAPSHOT[WARNING] 'version' contains an expression but should be a constant. @ com.jinhuitong.web:jht-web-mobile:${jht-web-mobile.version}, /root/.jenkins/workspace/jht-web-mobile/pom.xml, line 12, column 11[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.springframework:spring-test:jar -> duplicate declaration of version (?) @ com.jinhuitong.web:jht-web-mobile:${jht-web-mobile.version}, /root/.jenkins/workspace/jht-web-mobile/pom.xml, line 347, column 15[WARNING] 'dependencyManagement.dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.springframework:spring-test:jar -> duplicate declaration of version ${spring-version} @ com.jinhuitong.common:jht-common-parent:1.0-SNAPSHOT, /root/.m2/repository/com/jinhuitong/common/jht-common-parent/1.0-SNAPSHOT/jht-common-parent-1.0-SNAPSHOT.pom, line 514, column 16[WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.[WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.[WARNING] [INFO]                                                                         [INFO] ------------------------------------------------------------------------[INFO] Building jht-web-mobile 1.0-SNAPSHOT[INFO] ------------------------------------------------------------------------[WARNING] The POM for commons-logging:commons-logging-adapters:jar:1.1 is missing, no dependency information available[INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ jht-web-mobile ---[INFO] Deleting /root/.jenkins/workspace/jht-web-mobile/target              ##删除target目录[INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ jht-web-mobile ---[debug] execute contextualize[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] Copying 20 resources[INFO] [INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ jht-web-mobile ---[INFO] Changes detected - recompiling the module![INFO] Compiling 41 source files to /root/.jenkins/workspace/jht-web-mobile/target/classes[INFO] [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ jht-web-mobile ---[debug] execute contextualize[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] skip non existing resourceDirectory /root/.jenkins/workspace/jht-web-mobile/src/test/resources[INFO] [INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ jht-web-mobile ---[INFO] Changes detected - recompiling the module![INFO] Compiling 1 source file to /root/.jenkins/workspace/jht-web-mobile/target/test-classes[INFO] [INFO] --- maven-surefire-plugin:2.10:test (default-test) @ jht-web-mobile ---[INFO] Surefire report directory: /root/.jenkins/workspace/jht-web-mobile/target/surefire-reports------------------------------------------------------- T E S T S-------------------------------------------------------Results :Tests run: 0, Failures: 0, Errors: 0, Skipped: 0            ###没有进行代码测试(说明代码中没有配置代码测试)[INFO] [INFO] --- maven-war-plugin:2.1.1:war (default-war) @ jht-web-mobile ---[INFO] Packaging webapp[INFO] Assembling webapp [jht-web-mobile] in [/root/.jenkins/workspace/jht-web-mobile/target/jht-web-mobile-1.0-SNAPSHOT][INFO] Processing war project[INFO] Copying webapp resources [/root/.jenkins/workspace/jht-web-mobile/src/main/webapp][INFO] Webapp assembled in [711 msecs][INFO] Building war: /root/.jenkins/workspace/jht-web-mobile/target/jht-web-mobile-1.0-SNAPSHOT.war[INFO] WEB-INF/web.xml already added, skipping[INFO] [INFO] >>> maven-source-plugin:2.1.2:jar (attach-sources) @ jht-web-mobile >>>[INFO] [INFO] <<< maven-source-plugin:2.1.2:jar (attach-sources) @ jht-web-mobile <<<[INFO] [INFO] --- maven-source-plugin:2.1.2:jar (attach-sources) @ jht-web-mobile ---[INFO] Building jar: /root/.jenkins/workspace/jht-web-mobile/target/jht-web-mobile-1.0-SNAPSHOT-sources.jar      ##构建jar包[INFO] [INFO] --- maven-install-plugin:2.3.1:install (default-install) @ jht-web-mobile ---[INFO] Installing /root/.jenkins/workspace/jht-web-mobile/target/jht-web-mobile-1.0-SNAPSHOT.war to /root/.m2/repository/com/jinhuitong/web/jht-web-mobile/1.0-SNAPSHOT/jht-web-mobile-1.0-SNAPSHOT.war   ##安装到maven私有库,这里可以看到私有库的路径[INFO] Installing /root/.jenkins/workspace/jht-web-mobile/pom.xml to /root/.m2/repository/com/jinhuitong/web/jht-web-mobile/1.0-SNAPSHOT/jht-web-mobile-1.0-SNAPSHOT.pom              ##安装到maven私有库[INFO] Installing /root/.jenkins/workspace/jht-web-mobile/target/jht-web-mobile-1.0-SNAPSHOT-sources.jar to /root/.m2/repository/com/jinhuitong/web/jht-web-mobile/1.0-SNAPSHOT/jht-web-mobile-1.0-SNAPSHOT-sources.jar[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 9.953s[INFO] Finished at: Thu Nov 16 17:05:31 CST 2017[INFO] Final Memory: 18M/163M[INFO] ------------------------------------------------------------------------





原创粉丝点击