Skip to content

CI/CD with Jenkins – Part 8: deploy docker images

deploy images to server


Install ‘Publish Over SSH’ plugin

image 20200423004532272

Copy ssh key to server 192.168.1.4

sh-copy-id 192.168.1.4

Configure publish Over SSH

image 20200423010448812

Create jenkins_folder and upload shell script on production server

mkdir /opt/jenkins_shell
chmod +x /opt/jenkins_shell/deploy.sh

deploy.sh shell script

#! /bin/sh
harbor_url=$1
harbor_project_name=$2
project_name=$3
tag=$4
port=$5

imageName=$harbor_url/$harbor_project_name/$project_name:$tag

echo "$imageName"

#check docker container, if existing, delete it
containerId=`docker ps -a | grep -w ${project_name}:${tag}  | awk '{print $1}'`
if [ "$containerId" !=  "" ] ; then
    #stop container
    docker stop $containerId

    #delete container
    docker rm $containerId
	
	echo "delete container successfully"
fi

#check image, if existing, delete it
imageId=`docker images | grep -w $project_name  | awk '{print $3}'`

if [ "$imageId" !=  "" ] ; then
      
    #delete image
    docker rmi -f $imageId
	
	echo "delete image successfully"
fi

# login Harbor
docker login -u eric -p Eric123456 $harbor_url

# download image
docker pull $imageName

# start container
docker run -di -p $port:$port $imageName

echo "start container successfully"

Add string patameter ‘port environment’

image 20200423012535642

check docker image on production server

image 20200423012012402

build task

image 20200423013928783

check deployment status

image 20200423013952837
image 20200423015439653

Leave a Reply