Skip to content

Commit 6fe2a8e

Browse files
nicklpetersongreens
authored andcommitted
Refactor Jenkins Pipeline (#2393)
1 parent b6a52bc commit 6fe2a8e

File tree

2 files changed

+34
-37
lines changed

2 files changed

+34
-37
lines changed

Jenkinsfile

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ pipeline {
2828
agent {
2929
docker {
3030
image 'python:3.10'
31-
label 'production'
31+
label "${params.NODE_LABEL ?: 'production'}"
3232
args '--entrypoint= -u 0:0'
3333
}
3434
}
3535
environment {
3636
BRANCH = getEnvName()
3737
CHANGED_DRIVERS = getChangedDrivers()
38+
ENVIRONMENT = "${env.NODE_LABEL.toUpperCase()}"
3839
}
3940
stages {
4041
stage('requirements') {
@@ -51,22 +52,13 @@ pipeline {
5152
}
5253
}
5354
stage('update') {
54-
matrix {
55-
axes {
56-
axis {
57-
name 'ENVIRONMENT'
58-
values 'DEV', 'STAGING', 'ACCEPTANCE', 'PRODUCTION'
59-
}
60-
}
61-
stages {
62-
stage('environment_update') {
63-
steps {
64-
sh 'python3 tools/deploy.py'
65-
}
55+
stages {
56+
stage('environment_update') {
57+
steps {
58+
sh 'python3 tools/deploy.py'
6659
}
6760
}
6861
}
6962
}
7063
}
7164
}
72-

tools/deploy.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55
CHANGED_DRIVERS = os.environ.get('CHANGED_DRIVERS')
66
# configurable from Jenkins to override and manually set the drivers to be uploaded
77
DRIVERS_OVERRIDE = os.environ.get('DRIVERS_OVERRIDE') or "[]"
8+
DRY_RUN = os.environ.get("DRY_RUN") != None
89
print(BRANCH)
910
print(ENVIRONMENT)
1011
print(CHANGED_DRIVERS)
11-
branch_environment = "{}_{}_".format(BRANCH, ENVIRONMENT)
12-
ENVIRONMENT_URL = os.environ.get(ENVIRONMENT+'_ENVIRONMENT_URL')
12+
ENVIRONMENT_URL = os.environ.get('ENVIRONMENT_URL')
1313
if not ENVIRONMENT_URL:
1414
print("No environment url specified, aborting.")
1515
exit(0)
1616

1717
UPLOAD_URL = ENVIRONMENT_URL+"/drivers/package"
18-
CHANNEL_ID = os.environ.get(branch_environment+'CHANNEL_ID')
18+
CHANNEL_ID = os.environ.get(BRANCH+'_CHANNEL_ID')
1919
if not CHANNEL_ID:
20-
print("No channel id specified, aborting.")
20+
print("No channel id specified for "+BRANCH+", aborting.")
2121
exit(0)
2222

2323
UPDATE_URL = ENVIRONMENT_URL+"/channels/"+CHANNEL_ID+"/drivers/bulk"
24-
TOKEN = os.environ.get(ENVIRONMENT+'_TOKEN')
24+
TOKEN = os.environ.get('TOKEN')
2525
DRIVERID = "driverId"
2626
VERSION = "version"
2727
PACKAGEKEY = "packageKey"
@@ -91,7 +91,8 @@
9191
headers = {
9292
"Accept": "application/vnd.smartthings+json;v=20200810",
9393
"Authorization": "Bearer "+TOKEN,
94-
"X-ST-LOG-LEVEL": "DEBUG"
94+
"X-ST-LOG-LEVEL": "DEBUG",
95+
"X-ST-CORRELATION": "driver-deployment-"+BRANCH+"-"+ENVIRONMENT+"-"+str(time.time())
9596
},
9697
json = {
9798
DRIVERID: driver[DRIVERID],
@@ -147,7 +148,7 @@
147148
print(error.stderr)
148149
retries += 1
149150
if retries >= 5:
150-
print("5 zip failires, skipping "+package_key+" and continuing.")
151+
print("5 zip failures, skipping "+package_key+" and continuing.")
151152
continue
152153
with open(driver+".zip", 'rb') as driver_package:
153154
data = driver_package.read()
@@ -187,23 +188,27 @@
187188
print("Uploading package: {} driver id: {} version: {}".format(package_key, driver_info[DRIVERID], driver_info[VERSION]))
188189
driver_updates.append({DRIVERID: driver_info[DRIVERID], VERSION: driver_info[VERSION]})
189190

190-
response = requests.put(
191-
UPDATE_URL,
192-
headers={
193-
"Accept": "application/vnd.smartthings+json;v=20200810",
194-
"Authorization": "Bearer "+TOKEN,
195-
"Content-Type": "application/json",
196-
"X-ST-LOG-LEVEL": "DEBUG"
197-
},
198-
data=json.dumps(driver_updates)
199-
)
200-
if response.status_code != 204:
201-
print("Failed to bulk update drivers")
202-
print("Error code: "+str(response.status_code))
203-
print("Error response: "+response.text)
204-
exit(1)
191+
if DRY_RUN:
192+
print("Dry Run, skipping bulk upload to " + UPDATE_URL)
193+
else:
194+
response = requests.put(
195+
UPDATE_URL,
196+
headers={
197+
"Accept": "application/vnd.smartthings+json;v=20200810",
198+
"Authorization": "Bearer "+TOKEN,
199+
"Content-Type": "application/json",
200+
"X-ST-LOG-LEVEL": "DEBUG",
201+
"X-ST-CORRELATION": "driver-deployment-"+BRANCH+"-"+ENVIRONMENT+"-"+str(time.time())
202+
},
203+
data=json.dumps(driver_updates)
204+
)
205+
if response.status_code != 204:
206+
print("Failed to bulk update drivers")
207+
print("Error code: "+str(response.status_code))
208+
print("Error response: "+response.text)
209+
exit(1)
205210

206211
print("Update drivers: ")
207212
print(drivers_updated)
208-
print("\nDrivers currently deplpyed: ")
213+
print("\nDrivers currently deployed: ")
209214
print(uploaded_drivers.keys())

0 commit comments

Comments
 (0)