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

大体流程如下:

    1、提交代码到SVN

    2、拉取代码到本地

    3、执行maven package 打包

    4、生成Dockerfile

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

    6、发送邮件通知

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

###### ===* ****************************************************  *== ######
############ 开发人员在此位置一下添加程序编译过程 #######

#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

#!/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=\"http://imgstbox.voole.com/img/new_img/2018/03/20/2018032020594549GU9.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 条评论
登录 后参与评论

相关文章

来自专栏乐沙弥的世界

再说 Oracle RAC services

        应用程序工作负载在Oracle 10g中可以被定为services,也称之为服务,能够在单实例中使用,也能够在RAC中单独使用和管理。因此整个数...

1194
来自专栏大魏分享(微信公众号:david-share)

“车”跑在Openshift上?--基于Openshift 3.11的持续部署工具链

Eclipse Che是一个现代的、开放源代码的软件开发环境。它是一个通过提供结构化的工作区、项目输入、模块化扩展插件来支持Codenvy的引擎。Che可以用作...

2163
来自专栏iOSDevLog

Docker Cheat Sheet

“使用Docker,开发人员可以使用任何工具链以任何语言构建任何应用程序。”Dockerized“应用程序完全可移植,可以在任何地方运行 - 同事的OS X和W...

1352
来自专栏Java技术分享

如何解决ajax跨域问题

由 于此前很少写前端的代码(哈哈,不合格的程序员啊),最近项目中用到json作为系统间交互的手段,自然就伴随着众多ajax请求,随之而来的就是要解决 ajax的...

2238
来自专栏ml

关于docker 意外停止,重新快速启动措施

2027
来自专栏糊一笑

小白学Docker之Swarm

承接上篇文章:小白学Docker之基础篇,自学网站来源于https://docs.docker.com/get-started 系列文章: 小白学Docker之...

42811
来自专栏前端侠2.0

部署webapi,singalR的坑

我的系统是win2008,iis7.部署webapi后,应用池只能用经典模式。且要添加一个映射:

1933
来自专栏bboysoul

中间人攻击工具(Xerosploit)

Xerosploit是一个渗透测试工具包,它的目的是实现中间人攻击。它附带着各种有效的攻击模块,并且还允许执行拒绝服务攻击和端口扫描

1013
来自专栏技术翻译

Kubernetes揭秘:解决服务依赖问题

本系列文章探讨了企业客户在使用Kubernetes时遇到的一些常见问题。Container Service客户经常提出的一个问题是,“我如何处理服务之间的依赖关...

3262
来自专栏王亚昌的专栏

如何在Go中使用Protobuf

Protobuf对于Golang通过插件进行支持,因些需要安装protoc的执行环境,下面我们来一步步看下,如何搭建一个编译环境。

1331

扫码关注云+社区