Jenkins是一個很是出色的持續集成服務器,本文主要介紹在CentOS系統中Jenkins的基本安裝配置方法,供參考。java
一. 軟件包:linux
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版本,請選擇下載git
二. 安裝web
1.安裝jdkapache
# chmod 755 jdk-8u51-linux-x64.tar.gzvim
# tar zxvf jdk-8u51-linux-x64.tar.gz -C /app/zpy/tomcat
# chown -R root.root jdk1.8.0_51/服務器
# vim /etc/profileapp
JAVA_HOME=/app/zpy/jdk1.8.0_51ssh
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)配置環境變量
# vim /etc/profile
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
JAVA_HOME=/app/zpy/jdk1.8.0_51
JAVA_BIN=/app/zpy/jdk1.8.0_51/bin
PATH=$PATH:$JAVA_BIN:$MAVEN_HOME/bin:$GIT_BIN:$SVN_HOME
CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
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>
<id>nexus</id>
<!-- 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>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://192.169.10.109:8081/nexus/content/repositories/public</url>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</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)配置環境變量
# vim /etc/profile
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
JAVA_HOME=/app/zpy/jdk1.8.0_51
JAVA_BIN=/app/zpy/jdk1.8.0_51/bin
PATH=$PATH:$JAVA_BIN:$MAVEN_HOME/bin:$GIT_BIN:$SVN_HOME
CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export MAVEN_HOME JENKINS_HOME JAVA_HOME JAVA_BIN PATH CLASSPATH
# . /etc/profile
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)新建應用任務
I. svn配置,版本庫地址、用戶名和密碼
注意去掉勾選,若是選上,svn若是更新就自動發包。
II. (注意Global Tool Configuration中已配置maven)
完成點擊當即構建
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
點擊當即構建,完成!!!