Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ pipeline {
agent {
docker {
image 'python:3.10'
label 'production'
label "${params.NODE_LABEL ?: 'production'}"
args '--entrypoint= -u 0:0'
}
}
environment {
BRANCH = getEnvName()
CHANGED_DRIVERS = getChangedDrivers()
ENVIRONMENT = "${env.NODE_LABEL.toUpperCase()}"
}
stages {
stage('requirements') {
Expand All @@ -51,22 +52,13 @@ pipeline {
}
}
stage('update') {
matrix {
axes {
axis {
name 'ENVIRONMENT'
values 'DEV', 'STAGING', 'ACCEPTANCE', 'PRODUCTION'
}
}
stages {
stage('environment_update') {
steps {
sh 'python3 tools/deploy.py'
}
stages {
stage('environment_update') {
steps {
sh 'python3 tools/deploy.py'
}
}
}
}
}
}

51 changes: 28 additions & 23 deletions tools/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
CHANGED_DRIVERS = os.environ.get('CHANGED_DRIVERS')
# configurable from Jenkins to override and manually set the drivers to be uploaded
DRIVERS_OVERRIDE = os.environ.get('DRIVERS_OVERRIDE') or "[]"
DRY_RUN = os.environ.get("DRY_RUN") != None
print(BRANCH)
print(ENVIRONMENT)
print(CHANGED_DRIVERS)
branch_environment = "{}_{}_".format(BRANCH, ENVIRONMENT)
ENVIRONMENT_URL = os.environ.get(ENVIRONMENT+'_ENVIRONMENT_URL')
ENVIRONMENT_URL = os.environ.get('ENVIRONMENT_URL')
if not ENVIRONMENT_URL:
print("No environment url specified, aborting.")
exit(0)

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

UPDATE_URL = ENVIRONMENT_URL+"/channels/"+CHANNEL_ID+"/drivers/bulk"
TOKEN = os.environ.get(ENVIRONMENT+'_TOKEN')
TOKEN = os.environ.get('TOKEN')
DRIVERID = "driverId"
VERSION = "version"
PACKAGEKEY = "packageKey"
Expand Down Expand Up @@ -91,7 +91,8 @@
headers = {
"Accept": "application/vnd.smartthings+json;v=20200810",
"Authorization": "Bearer "+TOKEN,
"X-ST-LOG-LEVEL": "DEBUG"
"X-ST-LOG-LEVEL": "DEBUG",
"X-ST-CORRELATION": "driver-deployment-"+BRANCH+"-"+ENVIRONMENT+"-"+str(time.time())
},
json = {
DRIVERID: driver[DRIVERID],
Expand Down Expand Up @@ -147,7 +148,7 @@
print(error.stderr)
retries += 1
if retries >= 5:
print("5 zip failires, skipping "+package_key+" and continuing.")
print("5 zip failures, skipping "+package_key+" and continuing.")
continue
with open(driver+".zip", 'rb') as driver_package:
data = driver_package.read()
Expand Down Expand Up @@ -187,23 +188,27 @@
print("Uploading package: {} driver id: {} version: {}".format(package_key, driver_info[DRIVERID], driver_info[VERSION]))
driver_updates.append({DRIVERID: driver_info[DRIVERID], VERSION: driver_info[VERSION]})

response = requests.put(
UPDATE_URL,
headers={
"Accept": "application/vnd.smartthings+json;v=20200810",
"Authorization": "Bearer "+TOKEN,
"Content-Type": "application/json",
"X-ST-LOG-LEVEL": "DEBUG"
},
data=json.dumps(driver_updates)
)
if response.status_code != 204:
print("Failed to bulk update drivers")
print("Error code: "+str(response.status_code))
print("Error response: "+response.text)
exit(1)
if DRY_RUN:
print("Dry Run, skipping bulk upload to " + UPDATE_URL)
else:
response = requests.put(
UPDATE_URL,
headers={
"Accept": "application/vnd.smartthings+json;v=20200810",
"Authorization": "Bearer "+TOKEN,
"Content-Type": "application/json",
"X-ST-LOG-LEVEL": "DEBUG",
"X-ST-CORRELATION": "driver-deployment-"+BRANCH+"-"+ENVIRONMENT+"-"+str(time.time())
},
data=json.dumps(driver_updates)
)
if response.status_code != 204:
print("Failed to bulk update drivers")
print("Error code: "+str(response.status_code))
print("Error response: "+response.text)
exit(1)

print("Update drivers: ")
print(drivers_updated)
print("\nDrivers currently deplpyed: ")
print("\nDrivers currently deployed: ")
print(uploaded_drivers.keys())