Jade Dungeon

使用Jenkins发布docker

Docker发布脚本

######################################################################
# `deploy-docker-tomcat.sh` for deploy new docker container
# 
# USER       YYYY-MM-dd - Action
# qwshan     2020-12-22 - Created
# 
######################################################################

MAINTAINER=auto_script
MAINTAINER_MAIL=deploy@mycrop.com

DOCKER_CMD=/usr/bin/docker
DOCKER_WORKSPACE=/data/dockerfiles

CATALINA_BASE=/usr/local/tomcat

parasnum=5
# function
help_msg()
{
cat << help
+--------------------------------------------------------------------+
+ Error Cause: need $parasnum args but you input $#
+ Sample: 
+ deploys.h <docker_name> <prj_name> <prj_ver> <src_port> <dst_port>
+--------------------------------------------------------------------+
help
}


# --------------------------------------------------------------------
# Check args num
# --------------------------------------------------------------------
if [ $# -ne ${parasnum} ]
then
	help_msg
	exit
fi

# --------------------------------------------------------------------
# Init args
# --------------------------------------------------------------------
DOCKER_NAME=$1
PRJ_NAME=$2
PRJ_VER=$3
SRC_PORT=$4
DST_PORT=$5

PRJ_VER=${PRJ_VER/"origin/"/""}
echo $PRJ_VER


DOCKER_FILE_DIR="${DOCKER_WORKSPACE}/${DOCKER_NAME}"
DOCKER_FILE="${DOCKER_FILE_DIR}/Dockerfile"
if [ ! -d ${DOCKER_FILE_DIR} ]; then
	mkdir -p ${DOCKER_FILE_DIR}
fi

# --------------------------------------------------------------------
# check docker images
# --------------------------------------------------------------------
DOCKER_IMAGE=`$DOCKER_CMD images | grep ${DOCKER_NAME} | awk -F ' '  '{print $3}'`
if [ -n "${DOCKER_NAME}" ]; then
	# check docker container
	for dc in `$DOCKER_CMD images | grep ${DOCKER_NAME} | awk -F ' '  '{print $1}'`
	do 
		echo "Stop container: ${dc}"
		$DOCKER_CMD stop ${dc}
		# delete while docker container was exists
		echo "## Delete exists container, id: ${dc}"
		$DOCKER_CMD rm ${dc}
	done

	# delete while docker images was exists
	echo "## Delete exists Image: ${DOCKER_IMAGE}"
	$DOCKER_CMD rmi ${DOCKER_IMAGE}
fi
echo ""

# --------------------------------------------------------------------
# Init dockerfile
# --------------------------------------------------------------------

echo "** Init dockerfile: ${DOCKER_FILE}"
echo "FROM tomcat"                                         >  ${DOCKER_FILE}
echo "MAINTAINER ${MAINTAINER} \"${MAINTAINER_MAIL}\""     >> ${DOCKER_FILE}
echo "ADD *.war $CATALINA_BASE/webapps/${PRJ_NAME}.war"    >> ${DOCKER_FILE}
echo "EXPOSE 8080"                                         >> ${DOCKER_FILE}
echo "CMD $CATALINA_BASE/bin/start.sh && tail -f $CATALINA_BASE/logs/catalina.out" >> ${DOCKER_FILE}
cat ${DOCKER_FILE}
echo "** init docker end."

# --------------------------------------------------------------------
# Build Dockerfile
# --------------------------------------------------------------------
cd ${DOCKER_FILE_DIR}
rm *.war -rf
mv ${DOCKER_WORKSPACE}/war/${DOCKER_NAME}/*.war ./
echo ""
echo "## Build dockerfile for ${DOCKER_NAME}"
$DOCKER_CMD build -t ${DOCKER_NAME}:${PRJ_VER} .

# --------------------------------------------------------------------
# Run docker container
# --------------------------------------------------------------------
echo ""
echo "## Running docker container: ${DOCKER_NAME}"
$DOCKER_CMD run --name ${DOCKER_NAME}_d1 -d -d ${SRC_PORT}:${DST_PORT} ${DOCKER_NAME}:${PRJ_VER}
echo ""

修改脚本权限与用户,以后会以docker用户调用:

sudo chmod -x deploy-docker-tomcat.sh
sudo chown -R docker:docker deploy-docker-tomcat.sh

Jenkins插件

  • 版本管理工具插件,如git
  • 构建工具,Maven
  • 发布时通过SSH连接,要Publish Over SSH 和 SSH plugin

Jenkins 构造war包后上传

在「构建后操作」添加「Send build artifacts over SSH」:

  • Name:dest host
  • Source Filestarget/*.war
  • Remove prefixtarget/
  • Remote directory/war/$project_name
  • Exec command/data/dockerfiles/scripts/deploy.sh \(project_name hello_docker \)release_branch 11000 8080