After the project has been pushed to tag according to the previous configuration, Jenkins Host will automatically build the Docker image and push the image to Ali Cloud Registry. Then Jenkins Host will Publish over SSH plug-in, Push a script that generates an automatic deployment to the specified host, then automatically stop the previous image and pull the current image and run it

legacy

  1. The version rollback function is not implemented

    The version rollback function is required to avoid emergency problems

  2. Rollback version records

    The constructed image is created with the name of Gitlab tag, so the last image must be known before the rollback. Therefore, the image of each automatic deployment needs to be recorded to facilitate the automatic deployment rollback

Record automatic deployment history

Modify the SSH construction script to add the image of the last time to the local history file during automatic deployment to generate the automatic deployment script for version rollback

CONTAINER_NAME="citest"
GIT_TAG=`git describe --always --tag`
CONTAINER_FULL_NAME=${CONTAINER_NAME}-${GIT_TAG}
REPOSITORY=registry.cn-shanghai.aliyuncs.com/xxx/${CONTAINER_NAME}:${GIT_TAG}

#Build the Docker image
docker build -t $REPOSITORY -f Dockerfile .

#Push a Docker image
docker login --username=xxx --password=password registry.cn-shanghai.aliyuncs.com
docker push $REPOSITORY

#Delete the generated image
docker images | grep citest | awk '{print $1":"$2}' | xargs docker rmi || true

#Delete the mirror whose name or label is Nonedocker rmi -f `docker images | grep '<none>' | awk '{print $3}'` || true mkdir -p ./release && rm -f ./release/repull &&  echo \ "docker ps | grep citest | awk '{print \$2}' >> /data/jenkins/mi_test_history\n"\ "echo /data/jenkins/mi_test_history\n"\ "docker ps | grep citest | awk '{print \$1}' | xargs docker kill || true\n"\ "docker images | grep citest | awk '{print \$1\":\"\$2}' | xargs docker rmi -f || true\n"\ "docker login --username=xxx --password=password registry.cn-shanghai.aliyuncs.com\n"\ "docker pull $REPOSITORY\n"\ "docker run -d $REPOSITORY" >> ./release/repullCopy the code

After each build, an automatic deployment script repull is generated and sent to another specified host for automatic deployment. The modified repull script will record the last image in the /data/ Jenkins /mi_test_history file before each automatic deployment. Each line of this file represents a version update, and each update will add the record to the last line of the file

Create a parameterized build task

The original job is triggered by listening for gitLab’s tag push event. In order to support rollback, a parameterized build task is added. This task supports manual build and manual rollback

The git configuration of the newly created task is the same as that of the previous task, and then check the parameterized build process and configure the parameters. Now add a bool parameter, true indicates manual build, false indicates version rollback

Create shell scripts for build execution and support parsing of parameters for build configurations to perform different actions

if [ "$CTRL_BUILD" = "true" ]
then
	echo "to build image"
	CONTAINER_NAME="citest"
	GIT_TAG=`git describe --always --tag`"_manual_$BUILD_NUMBER"
    CONTAINER_FULL_NAME=${CONTAINER_NAME}-${GIT_TAG}
	REPOSITORY=registry.cn-shanghai.aliyuncs.com/xxx/${CONTAINER_NAME}:${GIT_TAG}

	#Build the Docker image
	docker build -t $REPOSITORY -f Dockerfile .

	#Push a Docker image
	docker login --username=xxx --password=password registry.cn-shanghai.aliyuncs.com
	docker push $REPOSITORY

	#Delete the generated image
	docker images | grep citest | awk '{print $1":"$2}' | xargs docker rmi || true

	#Delete the mirror whose name or label is Nonedocker rmi -f `docker images | grep '<none>' | awk '{print $3}'` || true rm -f ./auto.sh && echo \ "docker ps | grep citest | awk '{print \$2}' >> /data/jenkins/mi_test_history\n"\ "echo /data/jenkins/mi_test_history\n"\ "docker ps | grep citest | awk '{print \$1}' | xargs docker kill || true\n"\ "docker images | grep citest | awk '{print \$1\":\"\$2}'  | xargs docker rmi -f || true\n"\ "docker login --username=xxx --password=password registry.cn-shanghai.aliyuncs.com\n"\ "docker pull $REPOSITORY\n"\ "docker run -d $REPOSITORY" >> ./auto.sh else echo "to rollback image" rm -f ./auto.sh && echo \ "popline(){ LC_CTYPE=C l=\`tail -\"\${2:-1}\" \"\$1\"; echo t\`; l=\${l%t}; truncate -s \"-\${#l}\" \"\$1\"; printf %s \"\$l\"; }\n"\ "last=\`popline /data/jenkins/mi_test_history || \"\"\`\n"\ "if [ -n \"\$last\" ]; then\n"\ "\tdocker ps | grep citest | awk '{print \$1}' | xargs docker kill || true\n"\ "\tdocker images | grep citest |  awk '{print \$1\":\"\$2}' | xargs docker rmi -f || true\n"\ "\tdocker login --username=xxx --password=password registry.cn-shanghai.aliyuncs.com\n"\ "\tdocker pull \$last\n"\ "\tdocker run -d \$last\n"\ "\techo \"rollback to \$last  success\"\n"\ "else\n"\ "\techo \"nothing to rollback\"\n"\ "fi\n" >> ./auto.sh fiCopy the code

The manual build is basically the same as the automatic build, except that the image name will add the manual build id and build number, and the rollback will generate a rollback script, which will pop the last line from the /data/ Jenkins /mi_test_history file, Then stop the current task and delete the image, and pull the old version of the image and run it

The configuration of Publish over SSH is the same as before, with the auto-. sh script produced being transferred to the slave machine and executed

A functional test

Manually click task Build and check custom Build Parameters

A new image is generated to override the previous image and run, and the history record is also generated

Manually click Task Build again, this time uncheck custom build parameters to roll back the version

The version has been rolled back to the previous version and version records have been cleared