-
Notifications
You must be signed in to change notification settings - Fork 18
/
Jenkinsfile
122 lines (92 loc) · 3.62 KB
/
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env groovy
def getUptime() {
def fileContent = readFile('result.txt')
def integerValue = fileContent.find(/\d+/)?.toInteger()
return integerValue
}
pipeline {
environment {
username = "admin"
# AWS Secrets Manager Secret Name -- Subject to Change
mysecret = "rds!db-350ee583-4aff-4b83-b828-55601f8a50f3"
# AWS Region Code -- Subject to Change
aws_region = 'ap-southeast-1'
fpath="${WORKSPACE}"
dsn = "asterisk-ipcc-db"
}
agent any
stages {
stage ('Clean Up Workspace') {
steps {
cleanWs()
}
}
stage('SCM Checkout') {
steps {
git branch: 'main', url: 'https://github.com/sarbajitD-24/ipcc-odbc-configuration.git'
}
}
stage ('Retrieve secret') {
steps {
script {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'aws-sm-getsecretvalue',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
def rds_password = sh(script: """aws secretsmanager get-secret-value --region ${aws_region} \
--secret-id ${mysecret} | jq -r .SecretString | jq -r .password""", returnStdout: true).trim()
env.password=rds_password
echo "RDS DB Password: ${password}"
}
}
}
}
stage('Run Ansible Playbook') {
steps {
withEnv(["fpath=${env.fpath}","password=${env.password}"]) {
sh 'ansible-playbook -i hosts updatedb.yaml'
}
}
}
stage('Reboot Server') {
steps {
script {
withEnv(["fpath=${env.fpath}"]) {
sh 'ansible-playbook -i hosts check_uptime.yaml'
}
def prev_uptime=getUptime()
sh 'ansible-playbook -i hosts reboot_server.yaml'
withEnv(["fpath=${env.fpath}"]) {
sh 'ansible-playbook -i hosts check_uptime.yaml'
}
def curr_uptime=getUptime()
def autoCancelled = false
try {
if (curr_uptime < prev_uptime) {
echo "Asterisk Server Got Rebooted"
}else {
autoCancelled = true
error('Aborting the build as Asterisk Server didn\'t get rebooted')
}
}
catch (e) {
if (autoCancelled) {
currentBuild.result = 'ABORTED'
//return here instead of throwing error to keep the build "green"
//return
}
throw e
}
}
}
}
stage('Check DB Connectivity') {
steps {
withEnv(["dsn=${env.dsn}","username=${env.username}","password=${env.password}"]) {
sh 'ansible-playbook -i hosts db_connectivity.yaml'
}
}
}
}
}