From 09e9c9e0261a4d142909d237ed4d47323d008640 Mon Sep 17 00:00:00 2001 From: victormylle <32961910+victormylle@users.noreply.github.com> Date: Sat, 14 Nov 2020 12:25:17 +0100 Subject: [PATCH] Update bot with downtime zero --- Jenkinsfile | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 0ba9102..9ed87d5 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -10,9 +10,31 @@ pipeline { stage('Publish') { steps { - sh '"docker stack rm worldcraftdiscordbot"' - sh '"docker stack deploy worldcraftdiscordbot"' - sh '"docker service update worldcraftdiscordbot --image worldcraftdiscordbot:v${env.BUILD_ID}"' + sh '''if [ ! "$(docker ps -q -f name=worldcraftdiscordbot_1)" ]; then +    if [ "$(docker ps -aq -f status=exited -f name=worldcraftdiscordbot_1)" ]; then +        # cleanup +        docker rm worldcraftdiscordbot_1 +    fi +    # run your container +    NEW=worldcraftdiscordbot_1 +    OLD=worldcraftdiscordbot_2 +    docker run -d --name worldcraftdiscordbot_1 --restart unless-stopped worldcraftdiscordbot:v${env.BUILD_ID} +else [ ! "$(docker ps -q -f name=worldcraftdiscordbot_2)" ]; then +    if [ "$(docker ps -aq -f status=exited -f name=worldcraftdiscordbot_2)" ]; then +        # cleanup +        docker rm worldcraftdiscordbot_2 +    fi +    NEW=worldcraftdiscordbot_2 +    OLD=worldcraftdiscordbot_1 +    # run your container +    docker run -d --name $NEW --restart unless-stopped worldcraftdiscordbot:v${env.BUILD_ID} +fi + +# remove old container after 3 seconds +if [ "$(docker ps -a -f name=$OLD)" ]; then +    sleep 3s +    docker stop $OLD +fi''' } }