天天看点

手把手教你玩jenkins

前言:本文示例所涉及的工具配置为jenkins+tomcat8+jdk8+maven3.6.1+git

一.安装

这里以jenkins.war为实例,官方也提供了mac,rpm,windows等版本,对后台开发来说,可能war包方式比较亲切。

官方文档见:https://jenkins.io/zh/download/

下载:wget http://mirrors.jenkins.io/war/latest/jenkins.war

下载完毕后,把它丢到tomcat里启动就行了,对于部署而言,暂时不需要做任何更改

我们启动后访问 http://yourhost:ip/jenkins,第一次开打开会出现如下提示信息:

【为了确保管理员安全地安装 Jenkins,密码已写入到日志中(不知道在哪里?)该文件在服务器上:/root/.jenkins/secrets/initialAdminPassword】

照做找到管理员密码登录即可。

进去后会提示选择插件,该步骤暂时不用理他,jenkins里面有个【Manage Plugins】的模块,到时需要什么插件该模块下载安装即可。

登录成功后看到主界面:

手把手教你玩jenkins

(1)进来后,先建立个用户,或者修改admin密码,方便登录,菜单Manage Jenkins->Manage Users(略过)

(2)进入菜单Manage Jenkins->ConfigureSystem,设置jenkins所在的ip:port

手把手教你玩jenkins

(3)进入菜单Manage Jenkins->Configure Global Security,以下设置一定要钩上,不然一堆奇葩的问题!!!

手把手教你玩jenkins

(4) 设置jenkins所在服务器的jdk路径,建议使用oracle的jdk,jenkins也提供了自动下载安装的选项,也可以手工安装配置

手把手教你玩jenkins

 (5)设置jenkins所在服务器上maven的路径

手把手教你玩jenkins

 (6)jenkins通过maven构建项目,默认安装情况下是没有安装jenkins的maven插件的,需要手工安装,进入菜单Manage Jenkins->Manage Plugins,搜索Maven Integration plugin,安装即可;(如果需要通过SSH传输文件之类的还需要添加【Publish Over SSH Plugin】)

jenkins可以自动从版本管理工具(如git,svn等)下载代码然后通过maven构建,默认情况也是没有该插件,需要下载插件GIT Integration plugin,当构建完毕,我们想jenkins自动部署到远程服务器tomcat上,还需要下载个【Deploy to container plugin】插件

(7)后续我们需要登录tomcat,登录git之类的,需要用户密码,jenkins提供了一个模块来管理这些凭据,进入管理凭据,设置后续需要用到的各平台的账号密码

手把手教你玩jenkins

二:开始构建

前面准备活动已经完成,现在开始构建我们的第一个应用

 回到主菜单,点击add item->构建maven项目,名称为test,然后进去到项目配置页面

(1)配置git地址

手把手教你玩jenkins

(2) 配置远程tomcat

手把手教你玩jenkins

(这里需要注意红框选项如果没有安装deploy tocontainer插件是不会出现的)这里选择该选项

手把手教你玩jenkins

配置tomcat ip地址时,最后不要加斜杠,否则报错,正确示例:http://127.0.0.1:8080

(3)其他触发器之类的设置这里不做介绍。 

(4)开始构建

(自动构建,持续集成,要求tomcat必须处于启动状态!否则无法自动发布)

手把手教你玩jenkins

 点击完右边按钮后点击项目名称test,进入构建详情页面,然后点击Console Output,这里会实时打印出构建的日志

这个时候你会发现可能控制台上下面等错误

org.codehaus.cargo.container.tomcat.internal.TomcatManagerException: The username
 you provided is not allowed to use the text-based Tomcat Manager (error 403)
           
ava.io.FileNotFoundException: http://88.88.88.88:8080/manager/text/list
           

 第一个是说明jenkins调用tomcat 的文本接口时认证,进而无法执行一些指令(详情请了解tomcat manager模块)

第二个是说明tomcat manager模块的文件不存在(很可能是你下载完tomcat后,你把webapps下的manager文件夹等删除掉了,切记,不能删!!!)

解决方法:

进入tomcat,修改/conf/tomcat-user.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">
<!--
  NOTE:  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary. It is
  strongly recommended that you do NOT use one of the users in the commented out
  section below since they are intended for use with the examples web
  application.
-->
<!--
  NOTE:  The sample user and role entries below are intended for use with the
  examples web application. They are wrapped in a comment and thus are ignored
  when reading this file. If you wish to configure these users for use with the
  examples web application, do not forget to remove the <!.. ..> that surrounds
  them. You will also need to set the passwords to something appropriate.
-->
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
  <user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
  <user username="role1" password="<must-be-changed>" roles="role1"/>
-->
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<role rolename="manager-script"/>
 <user username="jianlejun" password="123456" roles="manager-script,admin-gui,manager-gui"/>
</tomcat-users>
           

增加 manager-script角色的用户(这个是一定必须要的)没有它就调不了tomcat对外的文本接口

tomcat8还需要在修改webapps/manager/META-INF/context.xml的内容

<Context antiResourceLocking="false" privileged="true" >
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="^.*$" />
  <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>
           

(这里留意allow属性,默认是只允许本地访问manage模块)

此时再点击构建,即可成功构建并自动发布的远程tomcat服务器上,并自动启动tomcat

最终的页面如下:

Started by user zhoudada
Running as SYSTEM
Building in workspace /root/.jenkins/workspace/test
using credential b4584322-f92f-486e-992e-0ec5d5de1f4d
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://gitee.com/jianlejun/WMS.git # timeout=10
Fetching upstream changes from https://gitee.com/jianlejun/WMS.git
 > git --version # timeout=10
using GIT_ASKPASS to set credentials git login
 > git fetch --tags --progress https://gitee.com/jianlejun/WMS.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 3828ceb6ed0b5a37ddfe3d649889689673c86f71 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 3828ceb6ed0b5a37ddfe3d649889689673c86f71
Commit message: "."
 > git rev-list --no-walk 3828ceb6ed0b5a37ddfe3d649889689673c86f71 # timeout=10
Parsing POMs
Established TCP socket on 43597
[test] $ /root/.jenkins/tools/hudson.model.JDK/sunjdk1.8/bin/java -cp /root/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven35-agent-1.13.jar:/usr/local/software/maven/boot/plexus-classworlds-2.6.0.jar:/usr/local/software/maven/conf/logging jenkins.maven3.agent.Maven35Main /usr/local/software/maven/ /usr/local/software/tmp_test/tomcat2/webapps/jenkins/WEB-INF/lib/remoting-3.33.jar /root/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven35-interceptor-1.13.jar /root/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.13.jar 43597
<===[JENKINS REMOTING CAPACITY]===>channel started
Executing Maven:  -B -f /root/.jenkins/workspace/test/pom.xml install
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------------< com.ken:WMS >-----------------------------
[INFO] Building WMS 1.0
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ WMS ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 7 resources
[INFO] Copying 14 resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ WMS ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ WMS ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /root/.jenkins/workspace/test/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ WMS ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /root/.jenkins/workspace/test/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ WMS ---
[JENKINS] Recording test results
[INFO] 
[INFO] --- maven-war-plugin:2.2:war (default-war) @ WMS ---
[INFO] Packaging webapp
[INFO] Assembling webapp [WMS] in [/root/.jenkins/workspace/test/target/WMS]
[INFO] Processing war project
[INFO] Copying webapp resources [/root/.jenkins/workspace/test/src/main/webapp]
[INFO] Webapp assembled in [493 msecs]
[INFO] Building war: /root/.jenkins/workspace/test/target/WMS.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ WMS ---
[INFO] Installing /root/.jenkins/workspace/test/target/WMS.war to /root/.m2/repository/com/ken/WMS/1.0/WMS-1.0.war
[INFO] Installing /root/.jenkins/workspace/test/pom.xml to /root/.m2/repository/com/ken/WMS/1.0/WMS-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  12.710 s
[INFO] Finished at: 2019-06-27T23:37:02+08:00
[INFO] ------------------------------------------------------------------------
Waiting for Jenkins to finish collecting data
[JENKINS] Archiving /root/.jenkins/workspace/test/pom.xml to com.ken/WMS/1.0/WMS-1.0.pom
[JENKINS] Archiving /root/.jenkins/workspace/test/target/WMS.war to com.ken/WMS/1.0/WMS-1.0.war
channel stopped
Deploying /root/.jenkins/workspace/test/target/WMS.war to container Tomcat 8.x Remote with context /WMS
  [/root/.jenkins/workspace/test/target/WMS.war] is not deployed. Doing a fresh deployment.
  Deploying [/root/.jenkins/workspace/test/target/WMS.war]
Finished: SUCCESS
           

拓展:部署到docker容器篇

基本配置与上面一致,唯一区别就是构建后的操作

SSH 构建配置:

手把手教你玩jenkins

SSH Server配置:

菜单:Manage Jenkins->Configure System

手把手教你玩jenkins

最终构建成功:

SSH: Connecting from host [vultr.guest]
SSH: Connecting with configuration [vultrVPS] ...
SSH: EXEC: STDOUT/STDERR from command [docker cp /usr/local/software/jenkin2docker/demo.war tomcat8:/usr/local/tomcat/webapps/
docker stop tomcat8
sleep 10
docker start tomcat8] ...
tomcat8
tomcat8
SSH: EXEC: completed after 17,605 ms
SSH: Disconnecting configuration [vultrVPS] ...
SSH: Transferred 1 file(s)
Finished: SUCCESS