-
Notifications
You must be signed in to change notification settings - Fork 100
/
Jenkinsfile
195 lines (167 loc) · 8.79 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
pipeline {
agent {
/*
* Nodes that are configured for Digital Ocean are tagged as "do". We install the doctl utility as part
* of this pipeline, which does require that the Jenkins Agent has the snap store installed. 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 'do'
}
}
/*
* The Digital Ocean token is passed into the process via a credential in Jenkins. If this is not found the
* process will fail out.
*
* 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 {
DIGITALOCEAN_TOKEN = credentials('DIGITALOCEAN_TOKEN')
PULUMI_ACCESS_TOKEN = credentials('PULUMI_ACCESS_TOKEN')
MARA_PASSWORD = credentials('MARA_PASSWORD')
NO_COLOR = "TRUE"
DEBIAN_FRONTEND = "noninteractive"
}
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') {
steps {
/*
* 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.
*/
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
# Create the directory for the kubeconfig
mkdir -p $HOME/.kube || true
chmod 777 $HOME/.kube || true
# Make sure doctl is installed
snap install doctl
# Auth to Digital Ocean
doctl auth init -t $DIGITALOCEAN_TOKEN
# Fix perms for the snap....
snap connect doctl:kube-config
'''
}
}
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('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=marajenkdo${BUILD_NUMBER}" > $WORKSPACE/config/pulumi/environment
echo "DO_TOKEN=${DO_TOKEN}" >> $WORKSPACE/config/pulumi/environment
$WORKSPACE/pulumi/python/venv/bin/pulumi stack select --create marajenkdo${BUILD_NUMBER} -C pulumi/python/config
$WORKSPACE/pulumi/python/venv/bin/pulumi stack select --create marajenkdo${BUILD_NUMBER} -C pulumi/python/kubernetes/secrets
$WORKSPACE/pulumi/python/venv/bin/pulumi config set certmgr:helm_timeout "600" -C pulumi/python/config -s marajenkdo${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set kic-helm:helm_timeout "600" -C pulumi/python/config -s marajenkdo${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set kubernetes:infra_type "DO" -C pulumi/python/config -s marajenkdo${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set kubernetes:kubeconfig "$HOME/.kube/config" -C pulumi/python/config -s marajenkdo${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set logagent:helm_timeout "600" -C pulumi/python/config -s marajenkdo${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set logstore:helm_timeout "600" -C pulumi/python/config -s marajenkdo${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set prometheus:helm_timeout "600" -C pulumi/python/config -s marajenkdo${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set docean:instance_size "s-4vcpu-8gb" -C pulumi/python/config -s marajenkdo${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set docean:k8s_version "latest" -C pulumi/python/config -s marajenkdo${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set docean:node_count "3" -C pulumi/python/config -s marajenkdo${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set docean:region "sfo3" -C pulumi/python/config -s marajenkdo${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set kic-helm:fqdn "mara${BUILD_NUMBER}.docean.mantawang.com" -C pulumi/python/config -s marajenkdo${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set digitalocean:token "${DO_TOKEN}" --plaintext -C pulumi/python/config -s marajenkdo${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set prometheus:adminpass "${MARA_PASSWORD}" --secret -C pulumi/python/kubernetes/secrets -s marajenkdo${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set sirius:accounts_pwd "${MARA_PASSWORD}" --secret -C pulumi/python/kubernetes/secrets -s marajenkdo${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set sirius:demo_login_pwd "password" --secret -C pulumi/python/kubernetes/secrets -s marajenkdo${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set sirius:demo_login_user "testuser" --secret -C pulumi/python/kubernetes/secrets -s marajenkdo${BUILD_NUMBER}
$WORKSPACE/pulumi/python/venv/bin/pulumi config set sirius:ledger_pwd "${MARA_PASSWORD}" --secret -C pulumi/python/kubernetes/secrets -s marajenkdo${BUILD_NUMBER}
'''
}
}
stage('Deploying Pulumi') {
steps {
sh '''
$WORKSPACE/pulumi/python/runner -p do -s marajenkdo${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 Digital Ocean.
*
* 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 do -s marajenkdo${BUILD_NUMBER} destroy
find . -mindepth 2 -maxdepth 6 -type f -name Pulumi.yaml -execdir pulumi stack rm marajenkdo${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 do -s marajenkdo${BUILD_NUMBER} destroy || true
find $WORKSPACE -mindepth 2 -maxdepth 7 -type f -name Pulumi.yaml -execdir $WORKSPACE/pulumi/python/venv/bin/pulumi stack rm marajenk${BUILD_NUMBER} --force --yes \\;
'''
}
}
}