forked from nubenetes/awesome-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaily_restart.Jenkinsfile
83 lines (73 loc) · 2.56 KB
/
daily_restart.Jenkinsfile
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
#!/usr/bin/env groovy
// Description: Daily restart of jenkins with declarative pipeline
// A Declarative Pipeline is defined within a 'pipeline' block.
pipeline {
agent { label 'master' }
triggers {
// “At 05:00 on every day-of-week from Sunday through Friday.”
// Not triggered on Saturday as Jenkinsfile is launched instead (cleanup pipeline script of the same repo that finishes with a restart)
cron('0 3 * * 0-5')
}
stages {
stage('Start') {
steps {
sh 'ls'
sh 'date'
sh 'pgrep -d, java | xargs ps -eo pcpu,pmem,pid,lstart,cmd | sort -r | grep -v while | head -2'
sh 'uptime'
}
}
stage('Clone Repository') {
/* Let's make sure we have the repository cloned to our workspace */
steps {
checkout scm
dir('cleanup-script') {
git credentialsId: 'Bitbucket_Access', url: "ssh://git@mybitbucketserver.com:999/cicd/cleanup-script.git"
}
}
}
stage('Restart Jenkins') {
steps {
sh '''
#!/bin/bash
echo "===================================="
echo "Automated daily restart of Jenkins: "
echo;
JENKINS_CLI=$JENKINS_HOME/war/WEB-INF/jenkins-cli.jar
if [ -e $JENKINS_HOME/war/WEB-INF/jenkins-cli.jar ]; then
JENKINS_CLI=$JENKINS_HOME/war/WEB-INF/jenkins-cli.jar
elif [ -e /usr/share/jenkins/war/WEB-INF/jenkins-cli.jar ]; then
JENKINS_CLI=/usr/share/jenkins/war/WEB-INF/jenkins-cli.jar
fi
#PREFIX is unique to each jenkins master server. It is obtained from the running process:
PREFIX=\$(ps -efj | egrep java | awk -F"--prefix=" '{ print $2 }' | cut -d" " -f1)
RESTART_TYPE=safe-restart
AUTH="-auth @$JENKINS_HOME/.jenkins-cli"
#AUTH="-i $JENKINS_HOME/.ssh/id_rsa"
if [ $PREFIX == "/jenkins" ]; then
RESTART_TYPE=restart
else
RESTART_TYPE=safe-restart
fi
date;
if [ -e $JENKINS_CLI -a -e $JENKINS_HOME/.jenkins-cli ]; then
echo "Restarting jenkins with jenkins-cli $RESTART_TYPE";
java -jar $JENKINS_CLI -s "http://localhost:8080$PREFIX/" $AUTH $RESTART_TYPE ;
else
ls -l $JENKINS_CLI;
ls -l $JENKINS_HOME/.jenkins-cli;
echo "jenkins-cli.jar or .jenkins-cli not found";
fi
date;
'''
}
}
stage('End') {
steps {
sh 'date'
sh 'pgrep -d, java | xargs ps -eo pcpu,pmem,pid,lstart,cmd | sort -r | grep -v while | head -2'
sh 'uptime'
}
}
}
}