Jenkins是一個非常出色的持續內建伺服器,本文主要介紹在CentOS系統中Jenkins的基本安裝配置方法,供參考。
一. 軟體包:
1. 下載下傳apache-maven-2.2.1-bin.tar
http://www.apache.org/dyn/closer.cgi/maven/binaries/apache-maven-2.2.1-bin.tar.gz
2. 下載下傳jenkins:http://mirrors.jenkins-ci.org/war/ 列出了jenkins的所有release版本,請選擇下載下傳
二. 安裝
1.安裝jdk
# chmod 755 jdk-8u51-linux-x64.tar.gz
# tar zxvf jdk-8u51-linux-x64.tar.gz -C /app/zpy/
# chown -R root.root jdk1.8.0_51/
# vim /etc/profile
JAVA_HOME=/app/zpy/jdk1.8.0_51
JAVA_BIN=/app/zpy/jdk1.8.0_51/bin
PATH=$PATH:$JAVA_BIN:
CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME JAVA_BIN PATH CLASSPATH
2.安裝tomcat
# tar zxvf apache-tomcat-6.0.41.tar.gz -C /app/zpy/
# mv apache-tomcat-6.0.41/ tomcat_jenkins
修改tomcat配置檔案
# vim tomcat_jenkins/conf/server.xml
修改8080端口的Connector節點,增加如下配置
URIEncoding="UTF-8"
3.安裝maven
1)解壓
# tar -zvxf apache-maven-2.2.1-bin.tar.gz
2)移動到其他目錄
# mv apache-maven-3.3.9/ maven
3)配置環境變量
SVN_HOME=/app/zpy/subversion/bin
GIT_HOME=/app/zpy/git
GIT_BIN=/app/zpy/git/bin
MAVEN_HOME=/app/zpy/maven
JENKINS_HOME=/app/zpy/tomcat_jenkins/webapps/jenkins
PATH=$PATH:$JAVA_BIN:$MAVEN_HOME/bin:$GIT_BIN:$SVN_HOME
export MAVEN_HOME JENKINS_HOME JAVA_HOME JAVA_BIN PATH CLASSPATH
# . /etc/profile
4) 驗證是否安裝成功
# mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: /app/zpy/maven
Java version: 1.8.0_51, vendor: Oracle Corporation
Java home: /app/zpy/jdk1.8.0_51/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "2.6.32-431.el6.x86_64", arch: "amd64", family: "unix"
5) 配置maven
編輯maven的配置檔案
# cd maven/conf
# vi settings.xml
修改配置檔案示例如下:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>/data/jenkins</localRepository>
<servers>
<server>
<username>admin</username>
<password>password</password>
</server>
</servers>
<mirrors>
<mirror>
<!-- This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>central</mirrorOf>
<url>http://10.0.70.242:8081/nexus/content/repositories/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<!-- Enable snapshots for the built in central repo to direct -->
<!-- all requests to nexus via the mirror -->
<repositories>
<repository>
<id>central</id>
<url>http://10.0.70.242:8081/nexus/content/repositories/public</url>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</releases>
<snapshots>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<url>http://192.169.10.109:8081/nexus/content/repositories/public</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<!-- make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
<pluginGroups>
<!-- define the sonatype plugin group, so the nexus plugins will work without
typing the groupId -->
<pluginGroup>org.sonatype.plugins</pluginGroup>
</pluginGroups>
</settings>
其中localRepository節點表示私服中下載下傳的jar包存放路徑,根據實際存放的路徑修改。
所有url節點中包含的位址:http://192.169.10.109:8081/nexus/content/repositories/public,表示nexus私服的位址,請根據實際的位址修改。由研發提供,沒有的話,使用預設的setting.xml配置檔案,走公有的庫,建構任務時下載下傳會慢一些。
username、password節點表示登入nexus私服的使用者名及密碼。
4、安裝jenkins
1) 複制到tomcat webapps目錄
# cp jenkins.war /app/zpy/tomcat_jenkins/webapps/
2)配置環境變量
3) 啟動tomcat,使用http://localhost:8080/jenkins通路jenkins
4) 輸入/app/zpy/tomcat_jenkins/webapps/jenkins/secrets/initialAdminPassword中的密碼

5) 安裝預設安裝
6)預設插件安裝完成,繼續。(有些插件安裝失敗,也可繼續)
7)不填内容,選擇continue as admin(後面進行使用者管理)
開始使用!
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1)建立使用者
系統管理—管理使用者—填入使用者名、密碼
設定\修改密碼: 系統管理—管理使用者—選擇指定的使用者—設定—到最下面修改密碼
2)插件配置
檢視已安裝的
系統管理—管理插件--進階—浏覽—上傳
4)建立任務
每個任務為部署檔案夾或壓縮包
首先建立一個imp-share的任務。之後部署的應用需要依賴于這個imp-share(都由研發提供,上傳至svn)
需要配置的主要有三個方面:svn maven deploy
I. svn配置,版本庫位址、使用者名和密碼
II. (注意Global Tool Configuration中已配置maven)
III. 完成後點選立即建構。至此依賴包建構完成。
5)建立應用任務
注意去掉勾選,如果選上,svn如果更新就自動發包。
完成點選立即建構
III. 自動遠端部署到tomcat
接下來要完成自動建構war包後,将這些war包上傳到遠端linus的tomcat的webapps目錄,更新項目的war包,并重新開機tomcat服務。
安裝插件
到https://wiki.jenkins-ci.org/display/JENKINS/Publish+Over+SSH+Plugin下載下傳hpi到本地
系統管理--> 管理插件--> 進階--> 上傳.hpi插件
配置ssh内容:
系統管理--> 系統設定--> publish over ssh 進行配置
點選進階
注釋:
如果沒有做ssh等效性,Passphrase、path to key、key處不需要填寫。
Disable exec 是禁止執行指令
SSH Server Name随便設定一個項目名
Hostname 是tomcat伺服器的位址
username 是系統的使用者名
Remote Directory 是登入到的路徑,此後作為根路徑,後面的配置依據于這個路徑
勾選Use password authentication,or use a different key
Passphrase/Password 填寫系統使用者的密碼
進行任務配置
配置imp-im-web任務
建構夠操作--> Sned build artifacts over SSH
SSH Server
Name 為之前ssh配置的項目名稱
Transfers
Source files 是war包的位置。預設根路徑為/app/zpy/tomcat_jenkins/webapps/jenkins/workspace/imp-im-web是以隻從target填寫即可
Remove prefix 去掉的路徑。如果不去掉,傳到tomcat上的包路徑會加上目錄target比如Remote directory設定為/tmp則拷貝過去後成為/tmp/target/imp-im.war
Remote directory 拷貝到tomcat上的路徑,存放war包
Exec command 拷貝war包後 要執行的操作。
deploy.sh 腳本的内容如下:
#!/bin/sh
#defined
source /etc/profile
TOMCAT_HOME="/app/zpy/tomcat"
ID=`ps -ef | grep java | grep tomcat|awk '{print $2}'`
echo $ID
echo "kill tomcat"
kill -9 $ID
echo "remover war file"
cd "$TOMCAT_HOME"/webapps
rm -rf imp-im
rm -rf imp-im.war
echo "copy war to webapp"
cd /tmp
mv imp-im.war "$TOMCAT_HOME"/webapps
cd "$TOMCAT_HOME"/bin
echo "start tomcat"
./startup.sh
至此imp-im-web任務配置完成,點選立即建構。成功會顯示
備注:
對于svn上存放的不是源碼,是編譯完成的war包的情況。
建立任務,選擇自由風格
imp-im.war在imp-im-web-test/target下
選擇Send files or execute commands over SSH after the build runs
點選立即建構,完成!!!