This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
start-monofor-jenkins.sh
77 lines (66 loc) · 2.99 KB
/
start-monofor-jenkins.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
imageName="monofor/jenkins"
latestImageVersion=""
latestJenkinsVersion=""
containerNamePrefix="monofor-jenkins-"
envItem="JENKINS_VERSION="
# get latest image and find image version
# ------------------------------------------------------------------------------------
docker pull "$imageName":latest
latestImageVersion=$(docker image ls -q --no-trunc "$imageName:latest")
echo "The latest image version is: $latestImageVersion"
# ------------------------------------------------------------------------------------
# find the image version of running container
# ------------------------------------------------------------------------------------
latestJenkinsVersion=$(docker image inspect -f '{{range $index, $value := .Config.Env}}{{println $value}}{{end}}' "$imageName" | grep $envItem | sed "s/$envItem/v/" | sed "s/\./\-/")
oldContainerName=$(docker ps --format '{{.Names}}' | grep $containerNamePrefix)
newContainerName="$containerNamePrefix$latestJenkinsVersion"
if [ "$oldContainerName" = "" ]; then
echo "Running Container not found, starting..."
docker run -d \
--ulimit nofile=8192:8192 \
-p 60220:8080 -p 60221:50000 \
-v monofor-jenkins:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
--name "$newContainerName" \
--restart=always \
--ipc=host \
"$imageName":latest
else
runningImageVersion=$(docker container inspect -f '{{.Image}}' "$oldContainerName")
if [ "$latestImageVersion" = "" ]; then
echo "Latest image version could not found!"
elif [ "$latestImageVersion" = "$runningImageVersion" ]; then
echo "The running Version is up to date :) (Jenkins: $latestJenkinsVersion, Image: $runningImageVersion)"
else
echo "Stopping container"
docker stop "$oldContainerName"
docker rename "$oldContainerName" "tmp-$oldContainerName"
echo "Running new Version: $newContainerName"
docker run -d \
--ulimit nofile=8192:8192 \
-p 60220:8080 -p 60221:50000 \
-v monofor-jenkins:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
--name "$newContainerName" \
--restart=always \
--ipc=host \
"$imageName":latest
# wait a little to container run completely
sleep 30
# check if the new container is running
isRunning=$(docker container inspect -f '{{.State.Running}}' "$newContainerName")
if [ "$isRunning" = "true" ]; then
echo "Deleting old container"
docker rm "tmp-$oldContainerName"
else
sleep 60
isRunning=$(docker container inspect -f '{{.State.Running}}' "$newContainerName")
if [ "$isRunning" = "false" ]; then
echo "Something went wrong, old container restarting..."
docker rename "tmp-$oldContainerName" "$oldContainerName"
docker start "$oldContainerName"
fi
fi
fi
fi