Total Pageviews

Wednesday 11 May 2016

Jenkins 基础

前言

Jenkins 是一个现在使用相当广泛的持续集成,持续交付开源工具,网络公司的快速迭代都会使用到此类工具
Jenkins is an award-winning, cross-platform, continuous integration and continuous delivery application that increases your productivity. Use Jenkins to build and test your software projects continuously making it easier for developers to integrate changes to the project, and making it easier for users to obtain a fresh build. It also allows you to continuously deliver your software by providing powerful ways to define your build pipelines and integrating with a large number of testing and deployment technologies.
jenkins 是自动运维的经典代表,下面分享一下它的基础操作,详细可以参阅 官方文档
Tip: 当前的最新版本为 jenkins 1.642

概要


安装

最新版的安装
sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
sudo yum install jenkins
稳定版的安装
sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
sudo yum install jenkins
安装过程
[root@h101 ~]# wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
--2015-12-17 19:42:11--  http://pkg.jenkins-ci.org/redhat/jenkins.repo
Resolving pkg.jenkins-ci.org... 199.193.196.24
Connecting to pkg.jenkins-ci.org|199.193.196.24|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 75 [text/plain]
Saving to: “/etc/yum.repos.d/jenkins.repo”

100%[============================================================================================>] 75          --.-K/s   in 0s      

2015-12-17 19:42:12 (6.28 MB/s) - “/etc/yum.repos.d/jenkins.repo” saved [75/75]

[root@h101 ~]# rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
[root@h101 ~]# echo $?
0
[root@h101 ~]# yum install jenkins
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * epel: mirrors.opencas.cn
 * extras: centos.ustc.edu.cn
 * updates: centos.ustc.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package jenkins.noarch 0:1.642-1.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

======================================================================================================================================
 Package                        Arch                          Version                            Repository                      Size
======================================================================================================================================
Installing:
 jenkins                        noarch                        1.642-1.1                          jenkins                         61 M

Transaction Summary
======================================================================================================================================
Install       1 Package(s)

Total download size: 61 M
Installed size: 61 M
Is this ok [y/N]: y
Downloading Packages:
jenkins-1.642-1.1.noarch.rpm                         57% [========================                  ]  48 kB/s |  35 MB     09:07 ETA 
jenkins-1.642-1.1.noarch.rpm                         57% [======================
jenkins-1.642-1.1.noarch.rpm                                                                                   |  61 MB     13:52     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : jenkins-1.642-1.1.noarch                                                                                           1/1 
  Verifying  : jenkins-1.642-1.1.noarch                                                                                           1/1 

Installed:
  jenkins.noarch 0:1.642-1.1                                                                                                          

Complete!
[root@h101 ~]# 

/etc/yum.repos.d/jenkins.repo 是一个Jenkins RPM 仓库

依赖

Jenkins 的运行需要 Java 环境 ,yum install jenkins 并不检查并且强制要求 java 已经正确安装,正常安装完jenkins并不代表其可以正常运行,使用下面的方式来确认当前环境下java的版本
[root@h101 ~]# java -version
java version "1.7.0_65"
OpenJDK Runtime Environment (rhel-2.5.1.2.el6_5-x86_64 u65-b17)
OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode)
[root@h101 ~]# 
Note: To further make things difficult for CentOS users, the default CentOS version of Java is not compatible with Jenkins. Jenkins typically works best with a Sun implementation of Java, which is not included in CentOS for licensing reasons
CentOS 版本的java 与Jenkins并不兼容
java -version
java version "1.5.0"
gij (GNU libgcj) version 4.4.6 20110731 (Red Hat 4.4.6-3)
EPEL仓库里提供的OpenJDK 可以很好的支持Jenkins
java -version
java version "1.7.0_79"
OpenJDK Runtime Environment (rhel-2.5.5.1.el6_6-x86_64 u79-b14)
OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)
如果出现java的相关报错,可以先尝试找找java版本的原因

防火墙

[root@h101 ~]# vim /etc/sysconfig/iptables
[root@h101 ~]# grep 8080 /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080  -j ACCEPT 
[root@h101 ~]# /etc/init.d/iptables  reload 
iptables: Trying to reload firewall rules:                 [  OK  ]
[root@h101 ~]# iptables -L -nv | grep 8080
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:8080 
[root@h101 ~]# 
默认情况下Jenkins监听在8080端口,所以要打开此端口,否则服务将无法访问

启停

启动

[root@h101 ~]# /etc/init.d/jenkins start 
Starting Jenkins                                           [  OK  ]
[root@h101 ~]# ps faux | grep jenkins
root      4064  0.0  0.0 103256   828 pts/0    S+   20:32   0:00  |       \_ grep jenkins
jenkins   3973 88.6 13.9 2206040 266288 ?      Ssl  20:32   0:17 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon --httpPort=8080 --ajp13Port=8009 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20
[root@h101 ~]# netstat  -ant | grep 8080
tcp        0      0 :::8080                     :::*                        LISTEN      
tcp        0      0 ::ffff:192.168.100.101:8080 ::ffff:192.168.100.1:58628  TIME_WAIT   
[root@h101 ~]# 
  • Jenkins 会作为一个服务在系统后台运行
  • /etc/init.d/jenkins 会提供详细信息,包括实际运行了什么,配置文件的位置
  • 初始配置在 /etc/sysconfig/jenkins
  • 默认情况下Jenkins会监听在 8080 端口(可以使用/etc/sysconfig/jenkins修改),要打开防火墙 , 可以使用本地的浏览器进行访问
  • jenkins 用户会被创建,并且以它的身份运行服务
Note: If you change this to a different user via the config file, you must change the owner of /var/log/jenkins, /var/lib/jenkins, and /var/cache/jenkins
  • 日志会被记到 /var/log/jenkins/jenkins.log

配置

[root@h101 ~]# grep -v "^#" /etc/sysconfig/jenkins  | grep -v "^$"
JENKINS_HOME="/var/lib/jenkins"
JENKINS_JAVA_CMD=""
JENKINS_USER="jenkins"
JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true"
JENKINS_PORT="8080"
JENKINS_LISTEN_ADDRESS=""
JENKINS_HTTPS_PORT=""
JENKINS_HTTPS_KEYSTORE=""
JENKINS_HTTPS_KEYSTORE_PASSWORD=""
JENKINS_HTTPS_LISTEN_ADDRESS=""
JENKINS_AJP_PORT="8009"
JENKINS_AJP_LISTEN_ADDRESS=""
JENKINS_DEBUG_LEVEL="5"
JENKINS_ENABLE_ACCESS_LOG="no"
JENKINS_HANDLER_MAX="100"
JENKINS_HANDLER_IDLE="20"
JENKINS_ARGS=""
[root@h101 ~]# 

操作界面

停止

[root@h101 ~]# /etc/init.d/jenkins stop 
Shutting down Jenkins                                      [  OK  ]
[root@h101 ~]# ps faux | grep jenkins
root      4079  0.0  0.0 103256   828 pts/0    S+   20:34   0:00  |       \_ grep jenkins
[root@h101 ~]# netstat  -ant | grep 8080
[root@h101 ~]#  

开机启动

[root@h101 ~]# chkconfig  --list | grep jenkins
jenkins         0:off 1:off 2:off 3:on 4:off 5:on 6:off
[root@h101 ~]# chkconfig  jenkins on
[root@h101 ~]# chkconfig  --list | grep jenkins
jenkins         0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@h101 ~]# 
Jenkins 会作为一个后台服务在系统启动时启动

命令汇总

  • wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
  • rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
  • yum install jenkins
  • java -version
  • vim /etc/sysconfig/iptables
  • grep 8080 /etc/sysconfig/iptables
  • /etc/init.d/iptables reload
  • iptables -L -nv | grep 8080
  • /etc/init.d/jenkins start
  • grep -v "^#" /etc/sysconfig/jenkins | grep -v "^$"
  • /etc/init.d/jenkins stop
  • chkconfig jenkins on
---------------------------

Jenkins+Maven+Git 自动部署 Java 应用(安装)

介绍

Jenkins 是基于 Java 开发的一种持续集成工具,然而我并不知道持续集成是个什么鬼。总之 Jenkins 可以用来做自动化构建,同时包括编译、发布、自动化测试。 本文写只是讲解如何通过 Jenkins 来做自动化构建与发布,即自动化升级。
以上全是胡说八道当不得真

环境

开发环境

  • Maven 项目管理
  • Git 版本管理
  • log4j 日志输出

Jenkins 环境

  • CentOS 6.x x64
  • 1 核心 1G 内存以上空闲资源
  • JDK 7u80
  • Maven 3.3.9
  • jenkins.war 1.642.1 LTS

下载地址

安装

系统调优略过

依赖

安装必要依赖以及工具
yum install -y wget unzip git

JDK

下载解压 JDK
cd /data/working
wget -c http://download.oracle.com/otn/java/jdk/7u80-b15/jdk-7u80-linux-x64.tar.gz
mkdir /application
cd /application
tar xf /data/working/jdk-7u80-linux-x64.tar.gz
ln -s jdk1.7.0_80 jdk
cd ~
配置 JDK 环境变量
cat >> /etc/profile <<_JDK_
export JAVA_HOME=/application/jdk
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH
_JDK_
source /etc/profile
检查 JDK
java -version
结果
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)

Maven

下载解压 Maven
cd /data/working
wget -c http://mirrors.noc.im/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.zip
cd /application
unzip /data/working/apache-maven-3.3.9-bin.zip
mv apache-maven-3.3.9 maven-3.3.9
ln -s maven-3.3.9 maven
cd ~
配置 Maven 环境变量
sed -i 's#export PATH=#&\/application/maven/bin:#' /etc/profile
source /etc/profile
检查 Maven
mvn -v
结果
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: /application/maven
Java version: 1.7.0_80, vendor: Oracle Corporation
Java home: /application/jdk1.7.0_80/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "2.6.32-573.12.1.el6.x86_64", arch: "amd64", family: "unix"

Jenkins

下载 Jenkins
cd /data/working
wget -c http://mirrors.jenkins-ci.org/war-stable/latest/jenkins.war
cd /application
mkdir jenkins-1.642.1
ln -s jenkins-1.642.1 jenkins
cp /data/working/jenkins.war jenkins/
cd ~
创建 Jenkins 运行账户,设置对应权限
useradd -u 600 jenkins -d /application/jenkins
chown -R jenkins:jenkins /application/jenkins*
创建 Jenkins 需要的目录,设置对应权限
mkdir -p /data/pid /data/jenkins/{logs,war,data}
chown -R jenkins:jenkins /data/jenkins
chmod 777 /data/pid
创建配置文件
## Path:        Development/Jenkins
## Description: Jenkins Continuous Integration Server
## Type:        string
## Default:     "/var/lib/jenkins"
## ServiceRestart: jenkins
#
# Directory where Jenkins store its configuration and working
# files (checkouts, build reports, artifacts, ...).
# 数据存储目录,包括之后的项目源码,项目编译结果
JENKINS_HOME="/data/jenkins/data"

## Type:        string
## Default:     "jenkins"
## ServiceRestart: jenkins
#
# Unix user account that runs the Jenkins daemon
# Be careful when you change this, as you need to update
# permissions of $JENKINS_HOME and /var/log/jenkins.
# 运行Jenkins的账户,需要自己建立
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(0:65535)
## Default:     8009
## ServiceRestart: jenkins
#
# Ajp13 Port Jenkins is listening on.
# Set to -1 to disable
#
JENKINS_AJP_PORT="8009"

## Type:        string
## Default:     ""
## ServiceRestart: jenkins
#
# IP address Jenkins listens on for Ajp13 requests.
# Default is all interfaces (0.0.0.0).
#
JENKINS_AJP_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=""
创建 Jenkins 启动脚本
#!/bin/bash
# File: /etc/init.d/jenkins
# Usage: /etc/init.d/jenkins
# chkconfig: 2345 57 46
# Description: Jenkins Continuous Integration Server.
# Version: 0.1
# Create Date: 2016-02-23 15:31
# Modified: 2016-02-23 15:31
# Author: Anton Chen
# Author EMail: contact@antonchen.com
. /etc/init.d/functions
# Read config
serviceConf="/application/jenkins/jenkins.conf"
if [ -f $serviceConf ];then
    . $serviceConf
else
   JENKINS_USER="jenkins"
fi

serviceName="Jenkins"
serviceUser="$JENKINS_USER"
javaExec="/application/jdk/bin/java"
serviceExec="/application/jenkins/jenkins.war"
servicePid="/data/pid/jenkins.pid"
serviceLogs="/data/jenkins/logs/jenkins.log"
accessLogs="/data/jenkins/logs/access.log"
jenkinsWebRoot="/data/jenkins/war"

# Check All
[ -f $javaExec ] || { echo "Java Exec not existing."; exit 1; }
[ -f $serviceExec ] || { echo "$serviceName Exec not existing."; exit 1; }
[ -d $jenkinsWebRoot ] || { echo "Jenkins Web directory not existing."; exit 1; }

# Set runCmd
runCmd="$javaExec $JENKINS_JAVA_OPTIONS -DJENKINS_HOME=$JENKINS_HOME -Dhudson.DNSMultiCast.disabled=true -jar $serviceExec"

# Load Jenkins config
runOpt="--logfile=$serviceLogs --webroot=$jenkinsWebRoot --daemon"
[ -n "$JENKINS_PORT" ] && runOpt="$runOpt --httpPort=$JENKINS_PORT"
[ -n "$JENKINS_LISTEN_ADDRESS" ] && runOpt="$runOpt --httpListenAddress=$JENKINS_LISTEN_ADDRESS"
[ -n "$JENKINS_HTTPS_PORT" ] && runOpt="$runOpt --httpsPort=$JENKINS_HTTPS_PORT"
[ -n "$JENKINS_HTTPS_KEYSTORE" ] && runOpt="$runOpt --httpsKeyStore=$JENKINS_HTTPS_KEYSTORE"
[ -n "$JENKINS_HTTPS_KEYSTORE_PASSWORD" ] && runOpt="$runOpt --httpsKeyStorePassword='$JENKINS_HTTPS_KEYSTORE_PASSWORD'"
[ -n "$JENKINS_HTTPS_LISTEN_ADDRESS" ] && runOpt="$runOpt --httpsListenAddress=$JENKINS_HTTPS_LISTEN_ADDRESS"
[ -n "$JENKINS_AJP_PORT" ] && runOpt="$runOpt --ajp13Port=$JENKINS_AJP_PORT"
[ -n "$JENKINS_AJP_LISTEN_ADDRESS" ] && runOpt="$runOpt --ajp13ListenAddress=$JENKINS_AJP_LISTEN_ADDRESS"
[ -n "$JENKINS_DEBUG_LEVEL" ] && runOpt="$runOpt --debug=$JENKINS_DEBUG_LEVEL"
[ -n "$JENKINS_HANDLER_STARTUP" ] && runOpt="$runOpt --handlerCountStartup=$JENKINS_HANDLER_STARTUP"
[ -n "$JENKINS_HANDLER_MAX" ] && runOpt="$runOpt --handlerCountMax=$JENKINS_HANDLER_MAX"
[ -n "$JENKINS_HANDLER_IDLE" ] && runOpt="$runOpt --handlerCountMaxIdle=$JENKINS_HANDLER_IDLE"
[ -n "$JENKINS_ARGS" ] && runOpt="$runOpt $JENKINS_ARGS"

if [ "$JENKINS_ENABLE_ACCESS_LOG" = "yes" ]; then
    runOpt="$runOpt --accessLoggerClassName=winstone.accesslog.SimpleAccessLogger --simpleAccessLogger.format=combined --simpleAccessLogger.file=$accessLogs"
fi

loading ()
{
    i=0
    while (( $i<$1 ));do
        printf "."
        sleep 1
        i=$(($i+1))
    done
}

checkPid ()
{
    pidFile="$servicePid"
    if [ -f "$pidFile" ]; then
        kill -0 `cat $pidFile` >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            return 0
        else
            return 1
        fi
    else
        return 1
    fi
}

start ()
{
    checkPid
    if [ $? -eq 0 ]; then
        echo "$serviceName is runing."
    else
        printf "Start $serviceName"
        daemon --user "$serviceUser" $runCmd $runOpt > /dev/null
        RETVAL=$?
        loading 2
        if [ $RETVAL -eq 0 ]; then
            # !!!!! Check grep !!!!!
            ps hww -u "$serviceUser" -o ppid,pid,cmd|grep -i "$serviceName"|\
            while read ppid pid cmd; do
                [ "$ppid" = 1 ] || continue
                [ "z$pid" == "z" ] && continue
                echo $pid >$servicePid
            done
            checkPid
            RETVAL=$?
            if [ $RETVAL -eq 0 ]; then
                success
            else
                failure
            fi
        else
            failure
        fi
        echo
    fi
}

stop ()
{
    checkPid
    if [ $? -eq 0 ]; then
        printf "Shutting down $serviceName"
        kill -15 `cat $servicePid`
        loading 2
        checkPid
        if [ $? -eq 0 ]; then
            failure
        else
            success
            rm -f $servicePid
        fi
        echo
    else
     echo "$serviceName is not runing."
    fi
}

status ()
{
    checkPid
    if [ $? -eq 0 ]; then
        echo "$serviceName is runing,PID = `cat $servicePid`."
    else
        echo "$serviceName is not runing."
    fi
}


case $1 in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart}"
        exit 1
        ;;
esac
添加 Jenkins 到开机启动
chkconfig --add jenkins
启动 Jenkins
/etc/init.d/jenkins start
访问 http://IP:8080 检查
安装到此结束
修订记录
  • 2016年2月25日
  • Anton Chen
    • 修正启动脚本配置文件不存在,无用户变量问题
    • 修正 JENKINS_HOME 设置
    • 添加配置文件注释
    • 添加系列 URL

Jenkins+Maven+Git 自动部署Java应用(配置)

接上文
安装好 Jenkins 后我们需要做些基础设置,安装一些插件才能进行编译。

配置

在上文中我给出了一个配置文件,这个配置文件只是一些可以在启动时指定的东西,并不是使用 Jenkins 用的配置。使用 Jenkins 的配置在网页上配置更方便。

JDK 设置

打开 http://IP:8080/ 选择 系统管理 - 系统设置
系统设置
找到 JDK 项,点击 新增 JDK,取消自动安装
JDK 设置
填写别名与 JDK路径,只要把光标从编辑框移出,Jenkins 就会对 JDK 路径进行检查,填写正确则没有任何提示
JDK 设置

Maven 设置

同样在系统设置中找到 Maven 项,点击 新增 Maven,取消自动安装
Maven 设置
填写别名与 Maven 路径,同样只要把光标从编辑框移出,Jenkins 就会检查 Maven 路径
Maven 设置
在页面底部保存,设置完这两项之后使用基本没有什么问题了。

插件

打开 http://IP:8080/ 选择 系统管理 - 管理插件
管理插件
选择 可选插件 在过滤中填写 Git plugin 勾选 Git plugin 点击直接安装。 如果列表为空,点击立即获取来获取列表内容
安装Git插件
跳转到安装页面,如图圆点代表状态
  • 灰色未执行
  • 蓝色成功
  • 黄色警告
  • 红色失败
勾选 安装完成后重启 Jenkins,安装完成后 Jenkins 会自动重启,你只需要等待即可
安装Git插件
因为国情所以会提示 Failed to connect to http://www.google.com/. Perhaps you need to configure HTTP proxy? 不必理会。
因插件服务器在国外,如果因为超时问题安装失败,多尝试几次即可。
配置到此结束。

Jenkins+Maven+Git 自动部署 Java 应用(编译)

接上文
设置好 Jenkins 后我们来尝试编译一个应用。

新建项目

打开 http://IP:8080 选择 新建 创建一个项目。这里因为我厂使用 Maven 管理项目,所以选择构建一个 Maven 项目。如果你们在使用其它的方式管理项目请自行研究。
当然 Jenkins 可以把多个版本库配置在一个项目中,主要是依赖项目这样做。但依赖项目也可以设置上下游关系来做依赖管理,个人感觉一个应用创建一个项目比较好。
新建任务
创建后自动跳转到配置页,找到 源码管理 项选择 Git,填写地址,同样这里 Jenkins 会检测 Git 地址是否正确,因为我没有配置账户所以报错了,点击 Add 按钮添加账户。
设置版本库
在 Git 上最方便的莫过于使用 SSH 密钥,这里我以密钥为例讲解。
设置 Git 密钥
添加完密钥之后就可以选择认证方式了,这里选择刚刚配置好的密钥,设置分支。
设置 Git
版本库到这里就设置好了,接下来设置 Maven
Maven 设置很简单,设置 pom.xml 在源码中的位置,以及编译参数就可以了,最后保存所有设置。
设置 Maven
到这里所有的项目设置就完成了,接下来尝试编译下。不过在编译之前我严重建议大家把 Maven 的源地址改为国内的,这里就不在赘述了。

编译

进入要编译的项目选择 立即构建,然后再下面 构建历史 就会出现如图一样的编号,点击编号查看详情。
构建
打开详情后可以查看控制台输出,第一次编译 Maven 项目 Maven 会自动下载编译依赖。
构建详情
回到项目页,可以打开 工作空间 查看或下载编译后的文件,当然可以连同依耐一起下载,同样在 构建历史 中可以通过圆点颜色查看编译状态。
构建结果

自动部署?

在项目中可以配置编译后的执行脚本,可以通过代码更新到 Git 触发编译。如图所示这是设置编译后可以操作的东西。
第一行解释:
- Run only if build succeeds = 编译成功
- Run only if build succeeds or is unstable = 编译成功但有问题
- Run regardless of build result = 编译失败执行
编译后执行

结论

研究 Jenkins 一段时间后发觉用它直接做自动部署(升级)有些不太可靠,主要是因为人的原因。Jenkins 支持定时、钩子等触发编译方式,如果做自动部署(升级)那么开发的一个失误就能导致线上业务的不稳定,甚至是全线崩溃。
综合考虑的结果是 Jenkins 在生产环境中编译应用解决依耐关系,通过 Jenkins 的 API 用 Python 做手动触发部署(升级)。
以上说法是针对生产环境
本系列到此完结,后续更新 Jenkins 估计是利用 Jenkins API 写自动化部署平台。
如有错误欢迎指正。