From 000078899bb13d3a02bddfc9e21019319fe547e5 Mon Sep 17 00:00:00 2001 From: nicklpeterson Date: Thu, 11 Sep 2025 14:41:34 -0500 Subject: [PATCH] parameterize the docker environment in Jenkins pipeline --- Jenkinsfile | 20 ++++++------------- tools/deploy.py | 51 +++++++++++++++++++++++++++---------------------- 2 files changed, 34 insertions(+), 37 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 92d915bac5..62bfe12c65 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -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') { @@ -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' } } } } } } - diff --git a/tools/deploy.py b/tools/deploy.py index c9d92448f5..451229f9a4 100644 --- a/tools/deploy.py +++ b/tools/deploy.py @@ -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" @@ -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], @@ -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() @@ -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())