-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
44 lines (41 loc) · 1.9 KB
/
Jenkinsfile
File metadata and controls
44 lines (41 loc) · 1.9 KB
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
pipeline {
agent any
stages {
stage("Build") {
steps {
withCredentials([file(credentialsId: 'sitesadmin-env', variable: 'ENV_PATH')]) {
sh '''
cp $ENV_PATH .env.production
docker compose -f docker-compose.yml -f docker-compose.production.yml --env-file .env.production build
# Get the list of image names and their corresponding build numbers
images=$(docker images --format "{{.Repository}}:{{.Tag}}" | grep 'sitesadmin-' | sort -t'-' -k2 -n)
# Get the last two image names
last_two_images=$(echo "$images" | tail -n 2)
# Iterate over the images and remove the old ones
echo "$images" | while read -r image_name; do
if ! echo "$last_two_images" | grep -q "$image_name"; then
echo "Removing image: $image_name"
docker rmi -f "$image_name"
else
echo "Keeping image: $image_name"
fi
done
'''
}
}
}
stage("Deploy") {
steps {
withCredentials([file(credentialsId: 'sitesadmin-env', variable: 'ENV_PATH')]) {
sh '''
if [ $(docker ps -q -f name=sitesadmin) ]; then
docker compose -f docker-compose.yml -f docker-compose.production.yml --env-file .env.production down
fi
cp $ENV_PATH .env.production
docker compose -f docker-compose.yml -f docker-compose.production.yml --env-file .env.production up -d
'''
}
}
}
}
}