-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
141 lines (137 loc) · 4.59 KB
/
Jenkinsfile
File metadata and controls
141 lines (137 loc) · 4.59 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
Globals = [:]
pipeline {
agent {
label any
}
options{
timeout(time:2,unit:'HOURS')
disableConcurrentBuilds()
buildDiscarder(logRotator(numToKeepStr: '5'))
timestamps()
}
environment{
AWS_DEFAULT_REGION = 'eu-west-1'
AWS_DEFAULT_OUTPUT = 'json'
ORGANISATION='XYZ'
ENVIRONMENT='ABC'
BUCKETNAME='s3-bucket-name-to-store-state'
MONITORINGFREQUENCY='rate(5 minutes)' // Allowed values are described in https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html
LOGLEVEL='INFO' // Allowed values are INFO and DEBUG
CUSTOMMETRICFILTERS='disable' // Allowed values are enable and disable
NOTIFICATIONS='slack'
}
stages {
stage('CreateStateStore') {
steps {
createStateStore()
}
}
stage("ChooseStrategy"){
agent none
steps{
script {
env.DEPLOY_TYPE = input message: 'Selection of deployment strategy is required',
parameters: [
choice(name: "choose the deployment strategy?", choices: 'config-only\nconfig-and-app',
description: 'Choose "config-only" if you want to deploy only config files')
]
}
}
}
stage('ConfigUpdate') {
steps {
configUpload()
}
}
stage('CodeBuild') {
when {
environment name:'DEPLOY_TYPE', value:'config-and-app'
}
steps {
script{
Globals.versiondetails = loadPropFile('version.prop')
env.VERSION = "${Globals.versiondetails.majorVersion}.${Globals.versiondetails.minorVersion}.${Globals.versiondetails.patchVersion}"
echo "Version is ${VERSION}"
codeBuild(VERSION)
}
}
}
stage('CodeDeploy'){
when {
environment name:'DEPLOY_TYPE', value:'config-and-app'
}
steps {
echo 'Applying CloudFormation Infra Updates...'
sh '''
fullPath=`pwd`
set +e
aws cloudformation deploy \
--template-file $fullPath/cfn/pingerCF.yaml \
--stack-name $BUCKETNAME-$ENVIRONMENT-stack \
--parameter-overrides Environment="$ENVIRONMENT" \
Organisation="$ORGANISATION" \
BucketName="$BUCKETNAME" \
ArtifactName="pinger_$VERSION.zip" \
MonitoringFrequency="$MONITORINGFREQUENCY" \
CustomMetricFilters="$CUSTOMMETRICFILTERS" \
--capabilities CAPABILITY_IAM \
2>fileout
RESULT=$?
set -e
# Tolerate when no changes are required for stack updates
if [ $RESULT != 0 ]; then
if ! grep -q \"No changes to deploy\" fileout; then
echo 'Unacceptable return code encountered:' $RESULT
cat fileout
exit $RESULT
fi
fi
'''
script {
currentBuild.description = "Current Version is ${VERSION}"
}
}
}
}
}
def loadPropFile(FILE){
def content = readFile "${FILE}"
Properties propsfile = new Properties()
InputStream is = new ByteArrayInputStream(content.getBytes());
propsfile.load(is)
return propsfile
}
def createStateStore() {
echo 'Creating S3 bucket to store the state'
sh '''
fullPath=`pwd`
if ! aws s3api head-bucket --bucket $BUCKETNAME 2>/dev/null; then
aws s3api create-bucket --bucket $BUCKETNAME --region $AWS_DEFAULT_REGION --create-bucket-configuration LocationConstraint=$AWS_DEFAULT_REGION
fi
'''
}
def configUpload() {
echo 'Uploading configuration'
sh '''
fullPath=`pwd`
aws s3 cp $fullPath/config.yaml s3://$BUCKETNAME/
'''
}
def codeBuild(version){
echo 'Building code artifact'
sh '''
fullPath=`pwd`
virtualenv -p /usr/bin/python3 $fullPath/pvenv
ls -lrt $fullPath/pvenv/bin
cd $fullPath/pvenv/bin/ && . ./activate && cd $fullPath
pip install -t . -r $fullPath/requirements.txt
sed -e "s~specify-region~$AWS_DEFAULT_REGION~g" \
-e "s~s3-bucket-name~$BUCKETNAME~g" \
-e "s~none~$NOTIFICATIONS~g" \
-e "s~log-level~$LOGLEVEL~g" $fullPath/pinger_template.py > $fullPath/pinger.py
zip -r pinger_$VERSION.zip . -x "cfn/*" -x "pinger_template.py" -x ".git/*" -x "pvenv/*" -x "Images/*"
aws s3 cp pinger_$VERSION.zip s3://$BUCKETNAME/
echo '{}' > state_machine
aws s3 cp $fullPath/state_machine s3://$BUCKETNAME/
'''
}