jenkins持續集成(一): 在Linux下的安裝與配置


前提:

  • 安裝了JDK,並配置好環境變量:JAVA_HOME
  • 安裝了Maven,並配置好環境變量:MAVEN_HOME
  • 如果JDK、Maven的安裝有疑問,可以參考我之前的博客!

環境:

  • 操作系統:CentOS 7.4 64位
  • JDK:jdk-8u152-linux-x64.tar.gz
  • Maven:apache-maven-3.5.2-bin.tar.gz

      具體安裝目錄如下,大家可以根據需要進行安裝,因為后面配置Jenkins需要這些路徑,所以展示下,省的有其他路徑的疑問!

[root@aliyun-abxiao software]# java -version java version "1.8.0_152" Java(TM) SE Runtime Environment (build 1.8.0_152-b16) Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode) [root@aliyun-abxiao software]# mvn -version Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T15:58:13+08:00) Maven home: /kevin/software/apache-maven-3.5.2 Java version: 1.8.0_152, vendor: Oracle Corporation Java home: /usr/java/jdk1.8.0_152/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "3.10.0-693.5.2.el7.x86_64", arch: "amd64", family: "unix"

 

Jenkins的安裝與配置步驟:

      1.添加Jenkins的源(repository)

[root@centos7.4 software]# wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo --2017-11-21 11:08:41-- http://pkg.jenkins-ci.org/redhat/jenkins.repo Resolving pkg.jenkins-ci.org (pkg.jenkins-ci.org)... 52.202.51.185 Connecting to pkg.jenkins-ci.org (pkg.jenkins-ci.org)|52.202.51.185|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 71 Saving to: ‘/etc/yum.repos.d/jenkins.repo’ 100%[=======================================================================================================================================================================================>] 71 --.-K/s in 0s 2017-11-21 11:08:42 (12.5 MB/s) - ‘/etc/yum.repos.d/jenkins.repo’ saved [71/71] [root@centos7.4 software]# rpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key

      2.如果你沒有安裝JDK,則需要先安裝JDK,如果已經安裝了,請忽略這一步!

[root@centos7.4 software]# sudo yum install java-1.6.0-openjdk

      3.安裝Jenkins

[root@centos7.4 software]# sudo yum install jenkins Loaded plugins: fastestmirror jenkins | 2.9 kB 00:00:00 jenkins/primary_db | 104 kB 00:00:13 Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package jenkins.noarch 0:2.91-1.1 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================================================================================================================================================================= Package Arch Version Repository Size ================================================================================================================================================================================================================================= Installing: jenkins noarch 2.91-1.1 jenkins 71 M Transaction Summary ================================================================================================================================================================================================================================= Install 1 Package Total download size: 71 M Installed size: 71 M Is this ok [y/d/N]: y Downloading packages: jenkins-2.91-1.1.noarch.rpm | 71 MB 00:00:08 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : jenkins-2.91-1.1.noarch 1/1 Verifying : jenkins-2.91-1.1.noarch 1/1 Installed: jenkins.noarch 0:2.91-1.1 Complete!

      Jenkins安裝完成之后,有如下目錄:

      3.1./usr/lib/jenkins/:jenkins安裝目錄,WAR包會放在這里。

[root@aliyun-abxiao software]# cd /usr/lib/jenkins/ [root@aliyun-abxiao jenkins]# ll total 72556 -rw-r--r-- 1 root root 74296449 Nov 21 00:50 jenkins.war

      3.1./etc/sysconfig/jenkins:jenkins配置文件,“端口”,“JENKINS_HOME”等都可以在這里配置。內容如下:

[root@centos7.4 software]# vi /etc/sysconfig/jenkins ## Path: Development/Jenkins ## Description: Jenkins Automation Server ## Type: string ## Default: "/var/lib/jenkins" ## ServiceRestart: jenkins # # Directory where Jenkins store its configuration and working # files (checkouts, build reports, artifacts, ...). # JENKINS_HOME="/var/lib/jenkins" ## Type: string ## Default: "" ## ServiceRestart: jenkins # # Java executable to run Jenkins # When left empty, we'll try to find the suitable Java. # JENKINS_JAVA_CMD="" ## Type: string ## Default: "jenkins" ## ServiceRestart: jenkins # # Unix user account that runs the Jenkins daemon # JENKINS_USER="jenkins" ## Type: string ## Default: "false" ## ServiceRestart: jenkins # # Whether to skip potentially long-running chown at the # $JENKINS_HOME location. Do not enable this, "true", unless # you know what you're doing. See JENKINS-23273. # #JENKINS_INSTALL_SKIP_CHOWN="false" ## Type: string ## Default: "-Djava.awt.headless=true" ## ServiceRestart: jenkins # # Options to pass to java when running Jenkins. # JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true" ## Type: integer(0:65535) ## Default: 8080 ## ServiceRestart: jenkins # # Port Jenkins is listening on. # Set to -1 to disable # JENKINS_PORT="8080" ## Type: string ## Default: "" ## ServiceRestart: jenkins # # IP address Jenkins listens on for HTTP requests. # Default is all interfaces (0.0.0.0). # JENKINS_LISTEN_ADDRESS="" ## Type: integer(0:65535) ## Default: "" ## ServiceRestart: jenkins # # HTTPS port Jenkins is listening on. # Default is disabled. # JENKINS_HTTPS_PORT="" ## Type: string ## Default: "" ## ServiceRestart: jenkins # # Path to the keystore in JKS format (as created by the JDK 'keytool'). # Default is disabled. # JENKINS_HTTPS_KEYSTORE="" ## Type: string ## Default: "" ## ServiceRestart: jenkins # # Password to access the keystore defined in JENKINS_HTTPS_KEYSTORE. # Default is disabled. # JENKINS_HTTPS_KEYSTORE_PASSWORD="" ## Type: string ## Default: "" ## ServiceRestart: jenkins # # IP address Jenkins listens on for HTTPS requests. # Default is disabled. # JENKINS_HTTPS_LISTEN_ADDRESS="" ## Type: integer(1:9) ## Default: 5 ## ServiceRestart: jenkins # # Debug level for logs -- the higher the value, the more verbose. # 5 is INFO. # JENKINS_DEBUG_LEVEL="5" ## Type: yesno ## Default: no ## ServiceRestart: jenkins # # Whether to enable access logging or not. # JENKINS_ENABLE_ACCESS_LOG="no" ## Type: integer ## Default: 100 ## ServiceRestart: jenkins # # Maximum number of HTTP worker threads. # JENKINS_HANDLER_MAX="100" ## Type: integer ## Default: 20 ## ServiceRestart: jenkins # # Maximum number of idle HTTP worker threads. # JENKINS_HANDLER_IDLE="20" ## Type: string ## Default: "" ## ServiceRestart: jenkins # # Pass arbitrary arguments to Jenkins. # Full option list: java -jar jenkins.war --help # JENKINS_ARGS=""

      3.3./var/lib/jenkins/:默認的JENKINS_HOME。

      3.4./var/log/jenkins/jenkins.log:Jenkins日志文件。

      4.啟動Jenkins

[root@centos7.4 software]# sudo service jenkins start 或者# systemctl start jenkins.service 

      5.啟動Jenkins時,可能是會出現錯誤的,因為java的安裝路徑和Jenkins服務啟動默認的java路徑可能是不一樣的,所以,會出現錯誤!比如:

[root@aliyun-abxiao jenkins]# sudo service jenkins start Starting jenkins (via systemctl): Job for jenkins.service failed because the control process exited with error code. See "systemctl status jenkins.service" and "journalctl -xe" for details. [FAILED] [root@aliyun-abxiao jenkins]# systemctl status jenkins.service ● jenkins.service - LSB: Jenkins Automation Server Loaded: loaded (/etc/rc.d/init.d/jenkins; bad; vendor preset: disabled) Active: failed (Result: exit-code) since Tue 2017-11-21 11:42:30 CST; 1min 39s ago Docs: man:systemd-sysv-generator(8) Process: 14949 ExecStart=/etc/rc.d/init.d/jenkins start (code=exited, status=1/FAILURE) Nov 21 11:42:30 aliyun-abxiao systemd[1]: Starting LSB: Jenkins Automation Server... Nov 21 11:42:30 aliyun-abxiao runuser[14954]: pam_unix(runuser:session): session opened for user jenkins by (uid=0) Nov 21 11:42:30 aliyun-abxiao jenkins[14949]: Starting Jenkins bash: /usr/bin/java: No such file or directory Nov 21 11:42:30 aliyun-abxiao systemd[1]: jenkins.service: control process exited, code=exited status=1 Nov 21 11:42:30 aliyun-abxiao jenkins[14949]: [FAILED] Nov 21 11:42:30 aliyun-abxiao systemd[1]: Failed to start LSB: Jenkins Automation Server. Nov 21 11:42:30 aliyun-abxiao systemd[1]: Unit jenkins.service entered failed state. Nov 21 11:42:30 aliyun-abxiao systemd[1]: jenkins.service failed.

      可以看到上面的錯誤提示,找不到或者沒有/usr/bin/java這個目錄,就可以確定是Jenkins啟動服務的java路徑還是默認的,並不是我們自己安裝jdk是的路徑,所以,我們需要把文件/etc/init.d/jenkins中的路徑修改過來:我們只需要將

candidates="
/etc/alternatives/java
/usr/lib/jvm/java-1.8.0/bin/java
/usr/lib/jvm/jre-1.8.0/bin/java
/usr/lib/jvm/java-1.7.0/bin/java
/usr/lib/jvm/jre-1.7.0/bin/java
/usr/java/jdk1.8.0_152/bin/java  這個是修改后的(修改前是/usr/bin/java)
"

的最后一行中的路徑修改成你jdk的安裝路徑即可

[root@centos7.4 lib]# vi /etc/init.d/jenkins #!/bin/sh # # SUSE system statup script for Jenkins # Copyright (C) 2007 Pascal Bleser # # This library is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or (at # your option) any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, # USA. # ### BEGIN INIT INFO # Provides: jenkins # Required-Start: $local_fs $remote_fs $network $time $named # Should-Start: $time sendmail # Required-Stop: $local_fs $remote_fs $network $time $named # Should-Stop: $time sendmail # Default-Start: 3 5 # Default-Stop: 0 1 2 6 # Short-Description: Jenkins Automation Server # Description: Jenkins Automation Server ### END INIT INFO # Check for missing binaries (stale symlinks should not happen) JENKINS_WAR="/usr/lib/jenkins/jenkins.war" test -r "$JENKINS_WAR" || { echo "$JENKINS_WAR not installed"; if [ "$1" = "stop" ]; then exit 0; else exit 5; fi; } # Check for existence of needed config file and read it JENKINS_CONFIG=/etc/sysconfig/jenkins test -e "$JENKINS_CONFIG" || { echo "$JENKINS_CONFIG not existing"; if [ "$1" = "stop" ]; then exit 0; else exit 6; fi; } test -r "$JENKINS_CONFIG" || { echo "$JENKINS_CONFIG not readable. Perhaps you forgot 'sudo'?"; if [ "$1" = "stop" ]; then exit 0; else exit 6; fi; } JENKINS_PID_FILE="/var/run/jenkins.pid" # Source function library. . /etc/init.d/functions # Read config [ -f "$JENKINS_CONFIG" ] && . "$JENKINS_CONFIG" # Set up environment accordingly to the configuration settings [ -n "$JENKINS_HOME" ] || { echo "JENKINS_HOME not configured in $JENKINS_CONFIG"; if [ "$1" = "stop" ]; then exit 0; else exit 6; fi; } [ -d "$JENKINS_HOME" ] || { echo "JENKINS_HOME directory does not exist: $JENKINS_HOME"; if [ "$1" = "stop" ]; then exit 0; else exit 1; fi; } # Search usable Java as /usr/bin/java might not point to minimal version required by Jenkins. # see http://www.nabble.com/guinea-pigs-wanted-----Hudson-RPM-for-RedHat-Linux-td25673707.html candidates=" /etc/alternatives/java /usr/lib/jvm/java-1.8.0/bin/java /usr/lib/jvm/jre-1.8.0/bin/java /usr/lib/jvm/java-1.7.0/bin/java /usr/lib/jvm/jre-1.7.0/bin/java /usr/java/jdk1.8.0_152/bin/java " for candidate in $candidates do [ -x "$JENKINS_JAVA_CMD" ] && break JENKINS_JAVA_CMD="$candidate" done JAVA_CMD="$JENKINS_JAVA_CMD $JENKINS_JAVA_OPTIONS -DJENKINS_HOME=$JENKINS_HOME -jar $JENKINS_WAR" PARAMS="--logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon" [ -n "$JENKINS_PORT" ] && PARAMS="$PARAMS --httpPort=$JENKINS_PORT" [ -n "$JENKINS_LISTEN_ADDRESS" ] && PARAMS="$PARAMS --httpListenAddress=$JENKINS_LISTEN_ADDRESS" [ -n "$JENKINS_HTTPS_PORT" ] && PARAMS="$PARAMS --httpsPort=$JENKINS_HTTPS_PORT" [ -n "$JENKINS_HTTPS_KEYSTORE" ] && PARAMS="$PARAMS --httpsKeyStore=$JENKINS_HTTPS_KEYSTORE" [ -n "$JENKINS_HTTPS_KEYSTORE_PASSWORD" ] && PARAMS="$PARAMS --httpsKeyStorePassword='$JENKINS_HTTPS_KEYSTORE_PASSWORD'" [ -n "$JENKINS_HTTPS_LISTEN_ADDRESS" ] && PARAMS="$PARAMS --httpsListenAddress=$JENKINS_HTTPS_LISTEN_ADDRESS" [ -n "$JENKINS_DEBUG_LEVEL" ] && PARAMS="$PARAMS --debug=$JENKINS_DEBUG_LEVEL" [ -n "$JENKINS_HANDLER_STARTUP" ] && PARAMS="$PARAMS --handlerCountStartup=$JENKINS_HANDLER_STARTUP" [ -n "$JENKINS_HANDLER_MAX" ] && PARAMS="$PARAMS --handlerCountMax=$JENKINS_HANDLER_MAX" [ -n "$JENKINS_HANDLER_IDLE" ] && PARAMS="$PARAMS --handlerCountMaxIdle=$JENKINS_HANDLER_IDLE" [ -n "$JENKINS_ARGS" ] && PARAMS="$PARAMS $JENKINS_ARGS" if [ "$JENKINS_ENABLE_ACCESS_LOG" = "yes" ]; then PARAMS="$PARAMS --accessLoggerClassName=winstone.accesslog.SimpleAccessLogger --simpleAccessLogger.format=combined --simpleAccessLogger.file=/var/log/jenkins/access_log" fi RETVAL=0 case "$1" in start) echo -n "Starting Jenkins " daemon --user "$JENKINS_USER" --pidfile "$JENKINS_PID_FILE" $JAVA_CMD $PARAMS > /dev/null RETVAL=$? if [ $RETVAL = 0 ]; then success echo > "$JENKINS_PID_FILE" # just in case we fail to find it MY_SESSION_ID=`/bin/ps h -o sess -p $$` # get PID /bin/ps hww -u "$JENKINS_USER" -o sess,ppid,pid,cmd | \ while read sess ppid pid cmd; do [ "$ppid" = 1 ] || continue # this test doesn't work because Jenkins sets a new Session ID # [ "$sess" = "$MY_SESSION_ID" ] || continue echo "$cmd" | grep $JENKINS_WAR > /dev/null [ $? = 0 ] || continue # found a PID echo $pid > "$JENKINS_PID_FILE" done else failure fi echo ;; stop) echo -n "Shutting down Jenkins " killproc jenkins RETVAL=$? echo ;; try-restart|condrestart) if test "$1" = "condrestart"; then echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}" fi $0 status if test $? = 0; then $0 restart else : # Not running is not a failure. fi ;; restart) $0 stop $0 start ;; force-reload) echo -n "Reload service Jenkins " $0 try-restart ;; reload) $0 restart ;; status) status jenkins RETVAL=$? ;; probe) ## Optional: Probe for the necessity of a reload, print out the ## argument to this init script which is required for a reload. ## Note: probe is not (yet) part of LSB (as of 1.9) test "$JENKINS_CONFIG" -nt "$JENKINS_PID_FILE" && echo reload ;; *) echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}" exit 1 ;; esac exit $RETVAL

      注意:出現錯誤可能是端口占用,我們可以先檢查Jenkins的默認8080啟動端口是否被占用,然后再殺死這個進程(不推薦),或者選擇將/etc/sysconfig/jenkins配置文件中的啟動端口8080改成其他的端口號!

[root@centos7.4 software]# netstat -apn 查看當前運行的所有進程的端口使用情況 [root@centos7.4 software]# netstat -apn | grep 端口號 查看指定端口使用情況 [root@centos7.4 software]# kill 指定端口的pid號 殺死指定進程(端口號對應的pid)

      6.再次運行Jenkins就可以成功運行了!

[root@centos7.4 software]# systemctl start jenkins.service Warning: jenkins.service changed on disk. Run 'systemctl daemon-reload' to reload units. [root@centos7.4 software]# systemctl daemon-reload [root@centos7.4 software]# systemctl start jenkins.service

      7.這時就可以訪問IP:8080,就可以訪問Jenkins的主頁了!這個時候我們去查這個目錄下查看管理員密碼/var/lib/jenkins/secrets/initialAdminPassword,並輸入到輸入框中!

      

[root@centos7.4 software]# vi /var/lib/jenkins/secrets/initialAdminPassword

      8.輸入管理員密碼,點擊Continute進入下一步,需要選擇是安裝推薦的插件還是自定義選擇安裝的插件,我這里選擇默認推薦的,然后繼續下一步:

      

      9.然后就開始下載插件了,如圖:

      

      10.等上圖中所有的插件下載並安裝完成,進入下一步,創建第一個管理員用戶(根據自己喜歡創建即可),保存完成:

      

      11.點擊保存完成后,可以看到Jenkins安裝已經完成,可以開始使用Jenkins:

      

      12.點擊開始使用就可以進入Jenkin的管理頁面了,並且默認使用剛剛創建的第一個管理員用戶登錄的:

      

      到此,Jenkins的安裝和基本的設置就完成了!其他設置可查看本人其他相關博文!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM