-
Notifications
You must be signed in to change notification settings - Fork 100
/
Jenkinsfile
219 lines (181 loc) · 9.73 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
pipeline {
agent {
/*
* Nodes that are configured for Amazon Web Services Ocean are tagged as "aws". We install the aws utility as part
* of the venv creation step.
*
* This has been tested on Ubuntu 20.04. Be sure to check that your Agent has the necessary components installed
* if you are using a different OS.
*/
node {
label 'aws'
}
}
/*
* The AWS security credentials are passed in via environment variables that are then made available to the pipeline.
* If these are not defined, and if the credential store on the build agent do not exist the process will fail
* at this point.
*
* The JWT for using NGINX Plus is passed in via a variable; if the JWT is not found the process will deploy the
* open source IC.
*/
environment {
AWS_DEFAULT_REGION = credentials('AWS_DEFAULT_REGION')
AWS_PROFILE = credentials('AWS_PROFILE')
AWS_ACCESS_KEY_ID = credentials('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = credentials('AWS_SECRET_ACCESS_KEY')
AWS_SESSION_TOKEN = credentials('AWS_SESSION_TOKEN')
NO_COLOR = "TRUE"
PULUMI_ACCESS_TOKEN = credentials('PULUMI_ACCESS_TOKEN')
MARA_PASSWORD = credentials('MARA_PASSWORD')
}
stages {
/*
* This logic allows any branch to be checked out and built, and is designed to be triggered by a github
* webhook. If desired, you can change the branch specification to force the process to only build from a specific
* branch.
*
* Note that we also init the submodule(s).
*/
stage('Checkout Scm') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [[$class: 'CheckoutOption'],
[$class: 'CloneOption', noTags: false, reference: '', shallow: false],
[$class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: false,
recursiveSubmodules: true, reference: '', trackingSubmodules: false]],
userRemoteConfigs: [[url: 'https://github.com/nginxinc/kic-reference-architectures']]])
}
}
stage('Prepping OS') {
/*
* This step handles ensuring the OS has the necessary tooling to build the project. This process has been
* built for Ubuntu 20.04 and assumes you have the snap store installed. It also assumes that you are running
* with an account that has access to passwordless sudo.
*
* We also make a few opinionated decisions, such as making sure we are always running on the latest set of
* packages for our Jenkins Agent.
*
* This should work on other Ubuntu related distributions, but most certainly will not work on RHEL or friends.
*/
steps {
sh '''
# Update catalogs
apt update
# Upgrade everything; we always run with latest patches.
DEBIAN_FRONTEND=noninteractive apt -y upgrade
# Make sure our deps are installed
DEBIAN_FRONTEND=noninteractive apt -y install figlet openjdk-11-jdk make docker.io
'''
}
}
stage('Cleaning Up') {
steps {
/*
* Other cleanup related functions can be placed here as well.
*/
sh '''
true
'''
}
}
stage('Create VENV') {
steps {
/*
* Create our virtual environment.
*/
sh '''
$WORKSPACE/bin/setup_venv.sh
'''
}
}
stage('Copy AWS Credentials') {
steps {
/*
* Copy credentials over
*/
sh '''
$WORKSPACE/bin/aws_write_creds.sh
'''
}
}
stage('Configure Pulumi') {
steps {
/*
* This logic sets the necessary variables in the configuration files; this differs from the manual procedure
* where we prompt the user for a number of these required variables. This same approach can be used as part
* of the manual deployment if required.
*
* This will likely evolve further as the project does, and we may reach a point where these defaults are assumed
* for a given development type.
*/
sh '''
echo "PULUMI_STACK=marajenkaws${BUILD_NUMBER}" > $WORKSPACE/config/pulumi/environment
echo "AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION}" >> $WORKSPACE/config/pulumi/environment
echo "AWS_PROFILE=${AWS_PROFILE}" >> $WORKSPACE/config/pulumi/environment
$WORKSPACE/pulumi/python/venv/bin/pulumi stack select --create marajenkaws${BUILD_NUMBER} -C pulumi/python/config
$WORKSPACE/pulumi/python/venv/bin/pulumi stack select --create marajenkaws${BUILD_NUMBER} -C pulumi/python/kubernetes/applications/sirius
$WORKSPACE/pulumi/python/venv/bin/pulumi stack select --create marajenkaws${BUILD_NUMBER} -C pulumi/python/kubernetes/secrets
$WORKSPACE/pulumi/python/venv/bin/pulumi config set certmgr:helm_timeout "600" -C pulumi/python/config -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set kic-helm:fqdn "marajenks${BUILD_NUMBER}.zathras.io" -C pulumi/python/config -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set kic-helm:helm_timeout "600" -C pulumi/python/config -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set kubernetes:kubeconfig "$HOME/.kube/config" -C pulumi/python/config -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set kubernetes:infra_type "aws" -C pulumi/python/config -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set kubernetes:cluster_name "marajenkaws${BUILD_NUMBER}" -C pulumi/python/config -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set logagent:helm_timeout "600" -C pulumi/python/config -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set logstore:helm_timeout "600" -C pulumi/python/config -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set prometheus:helm_timeout "600" -C pulumi/python/config -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set prometheus:adminpass "${MARA_PASSWORD}" --secret -C pulumi/python/kubernetes/secrets -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set sirius:accounts_pwd "${MARA_PASSWORD}" --secret -C pulumi/python/kubernetes/secrets -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set sirius:demo_login_pwd "password" --secret -C pulumi/python/kubernetes/secrets -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set sirius:demo_login_user "testuser" --secret -C pulumi/python/kubernetes/secrets -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set sirius:ledger_pwd "${MARA_PASSWORD}" --secret -C pulumi/python/kubernetes/secrets -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set aws:profile "${AWS_PROFILE}" -C pulumi/python/config -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set aws:region "${AWS_DEFAULT_REGION}" -C pulumi/python/config -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set eks:k8s_version "1.22" -C pulumi/python/config -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set eks:instance_type "t2.large" -C pulumi/python/config -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set eks:min_size "3" -C pulumi/python/config -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set eks:max_size "12" -C pulumi/python/config -s marajenkaws${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set eks:desired_capacity "3" -C pulumi/python/config -s marajenkaws${BUILD_NUMBER}
'''
}
}
stage('Deploying Pulumi') {
steps {
/*
*
*/
sh '''
$WORKSPACE/pulumi/python/runner -p aws -s marajenkaws${BUILD_NUMBER} up
'''
}
}
stage('Resetting Environment') {
steps {
/*
* Clean up the environment; this includes running the destroy script to remove our pulumi resources and
* destroy the deployed infrastructure in AWS
*
* After that completes, we remove the pulumi stack from the project with the find command; this is because
* we need to delete the stack in each project it's been instantiated in.
*/
sh '''
$WORKSPACE/pulumi/python/runner -p aws -s marajenkaws${BUILD_NUMBER} destroy
find . -mindepth 2 -maxdepth 6 -type f -name Pulumi.yaml -execdir $WORKSPACE/pulumi/python/venv/bin/pulumi stack rm marajenkaws${BUILD_NUMBER} --force --yes \\;
'''
}
}
}
post {
failure {
/*
* On failure we still need to remove the partial build; however, we want to make sure we exit with a zero
* status so we can move on to the next step. Hence the "or true" logic below.W
*/
sh '''
# Destroy our partial build...
$WORKSPACE/pulumi/python/runner -p aws -s marajenkaws${BUILD_NUMBER} destroy || true
find $WORKSPACE -mindepth 2 -maxdepth 7 -type f -name Pulumi.yaml -execdir $WORKSPACE/pulumi/python/venv/bin/pulumi stack rm marajenkaws${BUILD_NUMBER} --force --yes \\;
'''
}
}
}