Jenkins自动构建发布镜像脚本 原

2018-08-17 11:21:04 浏览数 (1)

大体流程如下:

    1、提交代码到SVN

    2、拉取代码到本地

    3、执行maven package 打包

    4、生成Dockerfile

    5、构建、标签、推送镜像

    6、发送邮件通知

主要分两个脚本,一个是jenkins配置的构建脚本,一个是构建推送以及邮件脚本,首先上Jenkins脚本

代码语言:javascript复制
###### ===* ****************************************************  *== ######
############ 开发人员在此位置一下添加程序编译过程 #######

#EVN_HOME jenkins全局变量控制,如有必要可以进行重定义
#EVN_HOME="/where/is"

#可选择的JAVA编译环境
#JDK
#export JAVA_HOME="${EVN_HOME}/jdk1.6.0_14"
#export JAVA_HOME="${EVN_HOME}/jdk1.6.0_25"
#export JAVA_HOME="${EVN_HOME}/jdk1.7.0_51"
export JAVA_HOME="${EVN_HOME}/jdk1.8.0_25"


#ANT
#export ANT_HOME="${EVN_HOME}/apache-ant-1.8.4"
export ANT_HOME="${EVN_HOME}/apache-ant-1.9.4"


#MVN
#export MAVEN_HOME="${EVN_HOME}/apache-maven-3.0.4"
#export MAVEN_HOME="${EVN_HOME}/apache-maven-3.1.1"
export MAVEN_HOME="${EVN_HOME}/apache-maven-3.2.3"
#export MAVEN_HOME="/opt/apache-maven-3.2.3-bak"


#gradle
export GRADLE_HOME="${EVN_HOME}/gradle-2.0/"


#添加PATH
export PATH="${MAVEN_HOME}/bin:${ANT_HOME}/bin:${JAVA_HOME}/bin:${GRADLE_HOME}/bin:${PATH}"

echo ${PATH}




###############编译过程################################

echo "=========== start compile ============="
Archive=${JOB_NAME}_${version}_${BUILD_TIMESTAMP}
Archive_abs="${WORKSPACE}/Arch_files"

Archive_zip="${JOB_NAME}_${version}_${BUILD_TIMESTAMP}.zip"

rm -fr ${Archive_abs} 
mkdir  -p  ${Archive_abs}

#添加版本信息
echo "${JOB_NAME} ${version} ${BUILD_TIMESTAMP}"  >${Archive_abs}/version.txt

cd  data_sync_parent

mvn install -Dmaven.test.skip=true
cp  ./data_sync_admin/target/*.war  ${Archive_abs}


cd  data_sync_agent

mvn install -Dmaven.test.skip=true
cp  ./target/*.jar  ${Archive_abs}

cd ..
cd  data_sync_api

mvn install -Dmaven.test.skip=true
cp  ./target/*.jar  ${Archive_abs}



cd ..
cd  data_sync_base

mvn install -Dmaven.test.skip=true
cp  ./target/*.jar  ${Archive_abs}

#删除sources.jar文件
cd  ${Archive_abs}
rm -f *-sources.jar
echo "=================md5sum====================="

cd ${Archive_abs}   

md5sum * > md5sum.txt

echo "=================docker start====================="
#将文件传输到远程docker机器
scp -r  -i ~/.ssh/id_rsa ${Archive_abs} jenkins@172.16.10.99:/opt/${Archive}/
#登陆docker机器
ssh -t -t 172.16.10.99 << eeooff
#进入工作目录
cd /opt/${Archive}
#以下为循环,建立任务主要变动地方
#1、打包&推送base镜像
mkdir base && cd base && mv ../*base*.jar ./
sh /opt/docker_workspace/pushimage.sh -n ${JOB_NAME} -v ${version} -t base
cd ../
#2、打包&推送admin镜像
mkdir admin && cd admin && mv ../*admin*.war ./
sh /opt/docker_workspace/pushimage.sh -n ${JOB_NAME} -v ${version} -t admin
cd ../
#发送邮件通知
sh email.sh
#完成打包&推送任务,删除工作目录
rm -rf /opt/${Archive}
#退出
exit
eeooff
echo "ALL images build and push complete! "
echo "=================docker end====================="
echo "===================end =================="

这里注意最后的sh email.sh这句命令,email.sh是由pushimage.sh这个脚本生成,下面是pushimage.sh

代码语言:javascript复制
#!/bin/bash
echo "******  start push image to registry......"
echo "(1/5)  parse args......"
# 接收参数
while getopts :n:v:r:h:t:k:c opt
do
    case $opt in
        n)
            image_name=$(echo $OPTARG | tr A-Z a-z)
            ;;
        v)
            image_version="$OPTARG"
            ;;
        r)
            image_registry="$OPTARG"
            ;;
        t)
            Type="$OPTARG"
            ;;
        k)
            kafka_address="$OPTARG"
            ;;
        c)
            conf_server="$OPTARG"
            ;;
        h)
            echo "******  -n set image name"
            echo "******  -v set image version"
            echo "******  -r set image registry address"
            echo "******  -t set dockerfile type(base|inf|admin)"
            echo "******  -k set kafka address"
            echo "******  -c set conf server address"
            echo "******  -h for help"
            ;;
        *)
            echo "******  ERR!unsuport command -$opt"
            ;;
    esac
done
# 解析验证参数
[ -z "$image_version" ] && echo "ERR! missing args:image_version" && exit 1
echo "******  image version is $image_version"
[ -z "$Type" ] && Type=base
echo "******  Type is $Type"
[ -z "$image_name" ] && echo "ERR! missing args:image_name" && exit 1
if echo $image_version |grep -E "[0-9]{8}.[0-9]{6}">/dev/null;then
	# 测试环境
	echo "init test env args!"
	[ -z "$image_registry" ] && image_registry="xxxx.xxx.com/xxxx"
	echo "******  image registry is $image_registry"
	[ -z "$kafka_address" ] && kafka_address="172.16.40.4:9092,172.16.40.5:9092"
	echo "******  kafka_address is $kafka_address"
	[ -z "$conf_server" ] && conf_server="172.16.40.4:80"
	echo "******  conf_server is $conf_server"
	Env="test"
	echo "******  Env is $Env"
	case $Type in
    base)
		image_name="test_"$image_name"_base"
		echo "******  image name is $image_name"
        jar_file=$(find -name *base*.jar|awk -F/  '{print $NF}')
        echo "******  jar_file is $jar_file"
        ;;
    inf)
		image_name=$image_name"_inf"
        image_name="test_"$image_name"_inf"
		echo "******  image name is $image_name"
        jar_file=$(find -name *inf*.jar|awk -F/  '{print $NF}')
        echo "******  jar_file is $jar_file"
        ;;
    admin)
       image_name="test_"$image_name"_admin"
		echo "******  image name is $image_name"
        jar_file=$(find -name *admin*.jar|awk -F/  '{print $NF}')
        echo "******  jar_file is $jar_file"
        ;;
	esac
	kafka_topic="log_test_${jar_file%-*}"	
	echo "******  kafka_topic is $kafka_topic"
	recivers="xxxx@xxx.com"
	echo "******  email recivers is $recivers"
	env_name="测试"
else
	# 线上环境
	echo "init online env args!"
	[ -z "$image_registry" ] && image_registry="xxx.xxxx.com/xxx"
	echo "******  image registry is $image_registry"
	[ -z "$kafka_address" ] && kafka_address="172.16.40.4:9092,172.16.40.5:9092"
	echo "******  kafka_address is $kafka_address"
	[ -z "$conf_server" ] && conf_server="conf.xxxx.com:80"
	echo "******  conf_server is $conf_server"
	Env="online"
	echo "******  Env is $Env"
	case $Type in
    base)
		image_name="online_"$image_name"_base"
		echo "******  image name is $image_name"
        jar_file=$(find -name *base*.jar|awk -F/  '{print $NF}')
        echo "******  jar_file is $jar_file"
        ;;
    inf)
        image_name="online_"$image_name"_inf"
		echo "******  image name is $image_name"
        jar_file=$(find -name *inf*.jar|awk -F/  '{print $NF}')
        echo "******  jar_file is $jar_file"
        ;;
    admin)
        image_name="online_"$image_name"_admin"
		echo "******  image name is $image_name"
        jar_file=$(find -name *admin*.war|awk -F/  '{print $NF}')
        echo "******  jar_file is $jar_file"
        ;;
	esac
	kafka_topic="log_online_${jar_file%-*}"	
	echo "******  kafka_topic is $kafka_topic"
	recivers="xxxx@xxx.com"
	echo "******  email recivers is $recivers"
	env_name="线上"
fi
# 生成对应类型Dockerfile文件
file_name=Dockerfile
base(){
	echo "
		# base on java8
		FROM xxx.xxx.com/xxx/xxx/java8:1.0
		# run with base path : /opt
		WORKDIR /opt
		#create webapps
		RUN mkdir webapps
		# add jar to container
		ADD $jar_file webapps
		# run with base path : /opt
		WORKDIR /opt/webapps
		# run when container started
		CMD java -Dredis.properties.filename=/opt/webapps/redis.properties -Dlog.kafka.address=$kafka_address -Dlog.kafka.topic=$kafka_topic -Ddisconf.conf_server_host=$conf_server -Ddisconf.env=$Env  -jar /opt/webapps/$jar_file
	">$file_name
}
inf(){
	echo "
		# base on java8
		FROM xxx.xxx.com/xx/xxx/java8:1.0
		# run with base path : /opt
		WORKDIR /opt
		#create webapps
		RUN mkdir webapps
		# add jar to container
		ADD $jar_file webapps
		# run with base path : /opt
		WORKDIR /opt/webapps
		# run when container started
		CMD java -Dlog.kafka.address=$kafka_address -Dlog.kafka.topic=$kafka_topic -Ddisconf.conf_server_host=$conf_server -Ddisconf.env=$Env  -jar /opt/webapps/$jar_file
	">$file_name
}
admin(){
	echo "
		# base on tomcat8
		FROM xx/tomcat8:1.0
		# run with base path : /opt
		WORKDIR /opt
		# add tomcat to container
		ADD $jar_file tomcat/webapps/
		# rename war to ROOT.war
		run mv tomcat/webapps/$jar_file tomcat/webapps/ROOT.war
		#set env
		ENV JAVA_OPTS -Ddisconf.conf_server_host=$conf_server -Dlog.kafka.address=$kafka_address -Dlog.kafka.topic=$kafka_topic  -Ddisconf.env=$Env
		# start tomcat
		CMD tomcat/bin/catalina.sh run
	">$file_name
}
echo "(2/5)  generate Dockerfile to current file......"
case $Type in 
    base)
        base
        ;;
    inf)
        inf
        ;;
    admin)
        admin
        ;;
esac
# 构建镜像
echo "(3/5)  build docker image:$image_name......"
sudo docker build -t $image_name .
# 对镜像打标签
echo "(4/5)  tag image:$image_name $image_registry/$image_name:$image_version......"
sudo docker tag $image_name $image_registry/$image_name:$image_version
# 上传镜像到私服
echo "(5/5)  push $image_registry/$image_name:$image_version......"
sudo docker push $image_registry/$image_name:$image_version
echo "<li>$image_registry/$image_name:$image_version</li>">>../email-images.text
#!/bin/bash
echo "
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body style="margin: 0; padding: 0;">
	<table border="0" cellpadding="0" cellspacing="0" width="100%">	
		<tr>
			<td style="padding: 10px 0 30px 0;">
				<table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border: 1px solid #cccccc; border-collapse: collapse;">
					<tr>
						<td align="center" bgcolor="#70bbd9" style="padding: 40px 0 30px 0; color: #153643; font-size: 28px; font-weight: bold; font-family: Arial, sans-serif;">
							<img src="https://img.yuanmabao.com/zijie/pic/2018/08/17/aqizcdtczrw.gif" alt="Creating Email Magic" width="300" height="230" style="display: block;" />
						</td>
					</tr>
					<tr>
						<td bgcolor="#ffffff" style="padding: 40px 30px 40px 30px;">
							<table border="0" cellpadding="0" cellspacing="0" width="100%">
								<tr>
									<td style="color: #153643; font-family: Arial, sans-serif; font-size: 24px;">
										<b><span style="color: red;">${env_name}环境</span>镜像发布成功!</b>
									</td>
								</tr>
								<tr>
									<td style="padding: 20px 0 30px 0; color: #153643; font-family: Arial, sans-serif; font-size: 16px; line-height: 20px;">
										各位好,<span style="color: red;">${env_name}环境</span>镜像构建发布成功!镜像全称为:</br>
										<ul style="color: red;">
	">../email-header.text
echo "
										</ul>
									</td>
								</tr>
								<tr>
							</table>
						</td>
					</tr>
					<tr>
						<td bgcolor="#ee4c50" style="padding: 30px 30px 30px 30px;">
							<table border="0" cellpadding="0" cellspacing="0" width="100%">
								<tr>
									<td style="color: #ffffff; font-family: Arial, sans-serif; font-size: 14px;" width="75%">
										Send from xxxxxx@xxxx.com<br/>
										POWER BY &reg; SCC, VOOLE 2018
									</td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</body>
</html>
">../email-foot.text
echo "
	#!/bin/bash
	echo "`cat email-header.text`">>email.text
	echo "`cat email-images.text`">>email.text
	echo "`cat email-foot.text`">>email.text
	sendEmail  -f xxxx@xxx.com -t "$recivers" -s mail.voole.com -u "${env_name}环境镜像打版成功!" -o message-content-type=html -o message-charset=utf8 -xu xxxxr@xxxx.com  -xp xxxxx -m "`cat email.text` "
	">../email.sh
chmod  x ../email.sh

0 人点赞