Jenkins Jenkins结合GIT Maven持续集成环境配置

Jenkins结合GIT Maven持续集成环境配置

 

by:授客 QQ1033553122

 

安装Git插件 1

安装Git客户端 1

安装JAVA JDKMAVEN 2

Jenkins JDKMAVENGIT配置 2

新建项目与项目配置 3

【全局属性】 4

【源码管理】 4

【构建环境】 6

SSH服务器配置 6

构建环境 7

构建 8

 

测试环境

Jenkins2.32.1

 

CentOS Linux 7

 

git-1.8.3.1.tar.gz

下载地址:https://www.kernel.org/pub/software/scm/git/git-1.8.3.1.tar.gz

 

操作步骤

安装Git插件

插件名称:Git Plugin,安装步骤 略

 

安装Git客户端

jenkins所在服务器安装git客户端

# yum install gcc

 

 

# mkdir /usr/local/git

# tar -xvzf git-1.8.3.1.tar.gz

# cd git-1.8.3.1/

# ./configure --prefix=/usr/local/git

# make && make install

# cd /usr/local/git/bin/

# ls

git            gitk              git-shell           git-upload-pack

git-cvsserver  git-receive-pack  git-upload-archive

 

配置GIT环境变量

# git

usage: git [--version] [--help] [-c name=value]

           [--exec-path[=]] [--html-path] [--man-path] [--info-path]

           [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]

           [--git-dir=] [--work-tree=] [--namespace=]

            []

 

The most commonly used git commands are:

   add        Add file contents to the index

   bisect     Find by binary search the change that introduced a bug

   branch     List, create, or delete branches

   checkout   Checkout a branch or paths to the working tree

   clone      Clone a repository into a new directory

   commit     Record changes to the repository

   diff       Show changes between commits, commit and working tree, etc

   fetch      Download objects and refs from another repository

   grep       Print lines matching a pattern

   init       Create an empty Git repository or reinitialize an existing one

   log        Show commit logs

   merge      Join two or more development histories together

   mv         Move or rename a file, a directory, or a symlink

   pull       Fetch from and merge with another repository or a local branch

   push       Update remote refs along with associated objects

   rebase     Forward-port local commits to the updated upstream head

   reset      Reset current HEAD to the specified state

   rm         Remove files from the working tree and from the index

   show       Show various types of objects

   status     Show the working tree status

   tag        Create, list, delete or verify a tag object signed with GPG

 

 

安装JAVA JDKMAVEN

参考文章:

CentOS7+svn+tomcat9.0+maven3.3+jenkins实现web项目自动构建与远程发布

 

Jenkins JDKMAVENGIT配置

系统管理-Global Tool Configuration

 

新建项目与项目配置

新建时,选择构建一个自由风格的项目

General

点击高级,展开隐藏的选项,勾选使用自定义的工作空间,如下

目录:Jenkin构建时,会根据Git等源码库配置,把代码拷贝到该目录下,这里$DDT_SALE_BUILD_WS为变量,其值取自全局属性中对应的对应的,如下

显示名称:工作空间名称,如果为空,则默认使用项目名称

 

【全局属性】

【系统管理-全局配置】,找到全局属性,勾选“Environment variables”,点击增加”,添加

【源码管理】

选择Git

Jenkins <wbr>Jenkins结合GIT <wbr>Maven持续集成环境配置 

 

说明:

Repostory URL:指定远程Git源码库的URL

 

Credentials:登录凭据,这使用帐号/密码,点击Add按钮,如下使用git帐号/密码的方式登录

Jenkins <wbr>Jenkins结合GIT <wbr>Maven持续集成环境配置

 

 

Branch Specifier (blank for 'any')指定源码库中的分支,具体配置参考官方说明

 

说明:不知道是否这样就可以了,还是说还需要进行其它配置(环境不是我搭的,我操作时就配置了这个就可以了)。

 

【构建环境】

SSH服务器配置

配置之前,先对SSH服务器进行配置,进入【系统管理-全局配置】-> Publish over SSH -> SSH Servers -> 增加,新增界面中正确填写配置信息,填写好后可通过点击下方的 Test Configuration按钮测试配置是否正确。

Jenkins <wbr>Jenkins结合GIT <wbr>Maven持续集成环境配置 

说明:

Name:服务器名称

Hostname:主机地址

Username:用于SSH服务器登录用户名

Passphrase/Password:用于SSH服务器登录密码

Remote Directory:远程目录,可以理解为Username SSH登录后的根目录

 

构建环境

Jenkins <wbr>Jenkins结合GIT <wbr>Maven持续集成环境配置 

这里根据实际需求进行配置,构建之前、构建之后通过SSH发送文件、执行命令。

以勾选“Send files or execute commands over SSH after the build runs”为例Jenkins <wbr>Jenkins结合GIT <wbr>Maven持续集成环境配置

说明:

Name: 通过下拉方式选取的SSH服务器

 

Source files:源文件,位于Jenkins所在服务器,*.war代表以.war结尾的文件,,注意这里填写的为相对路径,实际路径为 工作目录”/Source files(例中,实际路径为:/work/JenkinsWorkspace/DDT_SALE_BUILDWS/ddt-main/target/*.war

Remove prefixSource files输入框中需要移除的前缀部分,通常为文件所在目录的路径,个人理解,用于告诉Jenkins需要传输的具体文件

 

Remote directory: 需要把文件传送到SSH远程服务器上的目标目录。注意,这填写的基于“SSH服务器配置中设置的Remote Directory的相对路径。

 

实际远程目录为: SSH服务器配置的Remote Directory/这里配置的Remote directory

(例中,实际路径为:/app/war/DDT_CORE_JETTY_CNSZ22_WEB_MK_01/webapps

 

Exec command:需要执行的命令。

 

例中,构建后,会把Jenkins本地/work/JenkinsWorkspace/DDT_SALE_BUILDWS/ddt-main/target/xxx.war文件通过SSH传送到远程服务器(10.202.95.85)的/app/war/DDT_CORE_JETTY_CNSZ22_WEB_MK_01/webapps目录下。

 

 

构建

有时候,一些代码工程的构建,会依赖另一些jar包,我们可在构建*.war包之前,通过maven来构建这些被依赖的jar包,如下

注:构建*.war包也是如此,依葫芦画瓢

例:

代码工程ddt-industry-ws的构建,依赖 industry-client-0.0.1-SNAPSHOT.jar,为此需要把ddt-industry-client工程打包为industry-client-0.0.1-SNAPSHOT.jar(为何是ddt-industry-client这个工程,这个是开发指定的,没为何)

Jenkins <wbr>Jenkins结合GIT <wbr>Maven持续集成环境配置

 

那我们可以在jenkins已经拷贝源码后到本地后(依赖【源码管理】处的配置),配置【构建】

 

“增加构建步骤” -> Invoke top-level Maven targets

Jenkins <wbr>Jenkins结合GIT <wbr>Maven持续集成环境配置 

这样,待jenkins构建完成,会在工程/target目录下生成对应的jar包(例中,会在ddt-industry-ws/ddt-industry-client/target目录下生成industry-client-0.0.1-SNAPSHOT.jar

 

原文地址:https://www.cnblogs.com/shouke/p/10157580.html