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

deploy images to server


Install ‘Publish Over SSH’ plugin

Copy ssh key to server 192.168.1.4

sh-copy-id 192.168.1.4

Configure publish Over SSH

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’

check docker image on production server

build task

check deployment status

Leave a Reply

Your email address will not be published. Required fields are marked *