持续集成平台Jenkins的搭建和使用
2014年12月10日


Jenkins配置


丢弃旧的构建 -> 保持构建的最大个数 :10


MAVEN;

Root POM :pom.xml

Goals and options:clean install sonar:sonar -U -DskipTests=true



GIT:

参数化构建过程:

方法1:

Dynamic Choice Parameter:

name: release_branch

Choices Script: def ver_keys = [ 'bash', '-c', 'cd ~/.jenkins/workspace/my_project; git pull>/dev/null; git fetch -p; git branch -a|grep -v "*" | grep -v ">"|cut -d "/" -f3|sort -r |head -10 ' ] ver_keys.execute().text.tokenize('\n')


方法2:

Git Parameter

Name:developer_branch

Parameter Type:Branch


Repository URL: git@xxx:projgroup/proj.git

Credentials: 

Branches to build, Branch Specifier (blank for 'any'): $release_branch


SVN:



SSH:

Send build artifacts over SSH

Transfers:

Source files :target/*.war 

Remove prefix :target/ 

Remote directory: /my_project8080/war/ 

Exec command:/home/tomcat/scripts/deploy.sh my_project 8080

deploy.sh脚本内容如下:

#!/bin/bash
parasnum=2
# function
help_msg()
{
cat << help
+----------------------------------------------------+
+ Error Cause:
+ you enter $# parameters
+ the total paramenter number must be $parasnum
+ 1st :PROJECT_NAME
+ 2nd :SOURCE_PORT
+----------------------------------------------------+
help
}

# ----------------------------------------------------
# Check parameter number
# ----------------------------------------------------
if [ $# -ne ${parasnum} ]
then
        help_msg 
        exit
fi

# ----------------------------------------------------
# Initialize the parameter.
# ----------------------------------------------------
PROJECT_NAME=$1
SPORT=$2

BASE_HOME="/home/tomcat/${PROJECT_NAME}${SPORT}"
TOMCAT_HOME="${BASE_HOME}/apache-tomcat-7.0.54"  
TOMCAT_PORT=${SPORT}
TOMCAT_PROJECT=${PROJECT_NAME}
WAR_DIRECTORY="${BASE_HOME}/war"

export JAVA_HOME=/home/tomcat/jdk1.7.0_60
export JRE_HOME=${JAVA_HOME}/jre
export PATH=$PATH:${JAVA_HOME}/bin  
export CLASSPATH=./:${JAVA_HOME}/lib:${JAVA_HOME}/jre/lib

echo "----Stop tomcat"
${TOMCAT_HOME}/bin/shutdown.sh

TOMCAT_PID=`/usr/bin/lsof -n -P -t -i :${TOMCAT_PORT}`
if [ -n "${TOMCAT_PID}" ]; then
  for i in {1 .. 3}
    do
    echo "current tomcat pid: " ${TOMCAT_PID}
    sleep 3
    TOMCAT_PID=`/usr/bin/lsof -n -P -t -i :${TOMCAT_PORT}`

  done
  if [ -n "${TOMCAT_PID}" ]; then
        echo "----Tomcat thread still exists, needs to kill: "${TOMCAT_PID}
        kill -9 ${TOMCAT_PID}
        sleep 1
  fi
fi

echo "----Delete old files"
rm -rf ${TOMCAT_HOME}/work/Catalina/localhost/*
rm -rf ${TOMCAT_HOME}/webapps/${TOMCAT_PROJECT}.war
rm -rf ${TOMCAT_HOME}/webapps/${TOMCAT_PROJECT}

echo "----Move new war file to target Directory"
mv ${WAR_DIRECTORY}/*.war ${TOMCAT_HOME}/webapps/${TOMCAT_PROJECT}.war

echo "----Startup tomcat"
${TOMCAT_HOME}/bin/startup.sh