def get_headers(request): # CSRF Protection response = request.get('crumbIssuer/api/json') if response.isSuccessful(): crumb = JsonPathResult(response.response, 'crumb').get() return {'Jenkins-Crumb': crumb} else: return None
def isJobParameterized(request, jobContext, headers): jobInfo = request.get(jobContext + 'api/json', contentType='application/json', headers=headers) jobProperties = JsonPathResult(jobInfo.response, 'property').get() if jobProperties is not None: for prop in jobProperties: if (prop is not None and 'parameterDefinitions' in prop): return True jobActions = JsonPathResult(jobInfo.response, 'actions').get() if jobActions is not None: for action in jobActions: if (action is not None and 'parameterDefinitions' in action): return True return False
def isJobParameterized(request, jobContext): jobInfo = request.get(jobContext + 'api/json', contentType = 'application/json') jobActions = JsonPathResult(jobInfo.response, 'actions').get() if jobActions is not None: for action in jobActions: if (action is not None and 'parameterDefinitions' in action): return True return False
task.setStatusLine("Running build #%s" % jobBuildNumber) task.schedule("jenkins/multibranch/Build.wait-for-build.py") jenkinsURL = jenkinsServer['url'] jobContext = jobUrl.replace(jenkinsURL, '') request = HttpRequest(jenkinsServer, username, password) if location: # check the response to make sure we have an item print "location is {0}".format(jobContext) response = request.get(location + 'api/json', contentType='application/json') if response.isSuccessful(): # if we have been given a build number this item is no longer in the queue but is being built buildNumber = JsonPathResult(response.response, 'executable.number').get() if buildNumber: notifyBuildStarted(jenkinsURL, jobContext, jobName, buildNumber) else: task.schedule("jenkins/multibranch/Build.wait-for-queue.py") else: print "Could not determine build number for queued build at %s." % ( jenkinsURL + location + 'api/json') sys.exit(1) else: print "jobContext is {0}".format(jobContext) # fallback to the unreliable check because old jenkins(<1.561) does not populate the Location header response = request.get(jobContext + 'api/json',
if buildStatus != 'SUCCESS': task.schedule("jenkins/multibranch/Build.fail.py") jenkinsURL = jenkinsServer['url'] request = HttpRequest(jenkinsServer, username, password) jobContext = jobUrl.replace(jenkinsURL,'') response = None buildStatus = None try: fullJobContext = jobContext + str(buildNumber) + '/api/json' print("fullJobContext {0}".format(fullJobContext)) response = request.get(fullJobContext, contentType='application/json') if response.isSuccessful(): buildStatus = JsonPathResult(response.response, 'result').get() duration = JsonPathResult(response.response, 'duration').get() if buildStatus and duration != 0: try: addBuildRecord(response.response) except RuntimeException as e: print "\nCould not add 'Build' attribute: %s\n" % str(e) except Exception as e: print "\nCould not add 'Build' attribute: %s\n" % str(e) finishPolling(buildStatus) else: task.schedule("jenkins/multibranch/Build.wait-for-build.py") else: print "\nFailed to check the job status. Received an error from the Jenkins server: `%s`" % response.response task.schedule("jenkins/multibranch/Build.wait-for-build.py")
return None if jenkinsServer is None: print "No server provided." sys.exit(1) jenkinsURL = jenkinsServer['url'] jobContext = '/job/' + urllib.quote(jobName) + '/' request = HttpRequest(jenkinsServer, username, password) headers = get_headers(request) response = request.get(jobContext + 'api/json', contentType='application/json') if response.isSuccessful(): jobClass = JsonPathResult(response.response, '_class').get() if not "WorkflowMultiBranchProject" in jobClass: print "Job %s is not a multibranch project. Please use the other Jenkins task to manage it" % ( jenkinsURL + jobContext) sys.exit(1) else: # WorkflowMultiBranchProject : true # print response.response # print jobClass jobs = JsonPathResult(response.response, 'jobs').get() for job in jobs: if job['name'] == branch: jobUrl = job['url'] print "job url is %s" % (jobUrl)
sys.exit(1) request = HttpRequest(jenkinsServer, username, password) jobContext = '/job/' + urllib.quote(jobName) + '/' response = None try: response = request.get(jobContext + str(buildNumber) + '/api/json', contentType='application/json') except IOException as error: print "\nFailed to check the job status due to connection problems. Will retry in the next polling run. Error details: `%s`" % error task.schedule("jenkins/Build.wait-for-build.py") buildStatus = None if response and response.isSuccessful(): buildStatus = JsonPathResult(response.response, 'result').get() duration = JsonPathResult(response.response, 'duration').get() if buildStatus and duration != 0: # Customization: get additional variables from the jenkins job if jobEnvVarName: envResponse = request.get(jobContext + '/' + str(buildNumber) + '/injectedEnvVars/api/json', contentType = 'application/json') jobEnvVarValue = JsonPathResult(envResponse.response, "envMap.%s" % jobEnvVarName).get() # end custom logic finishPolling(buildStatus) else: task.schedule("jenkins/Build.wait-for-build.py") else: print "\nFailed to check the job status. Received an error from the Jenkins server: `%s`" % response.response
buildContext = jobContext + 'buildWithParameters' + buildQueryString(jobParameters) else: buildContext = jobContext + 'build' buildUrl = jobUrl + buildContext buildResponse = request.post(buildContext, '', contentType = 'application/json') if buildResponse.isSuccessful(): # polls until the job has been actually triggered (it could have been queued) while True: time.sleep(poll_interval) response = request.get(jobContext + 'api/json', contentType = 'application/json') # response.inQueue is a boolean set to True if a job has been queued inQueue = JsonPathResult(response.response, 'inQueue').get() if not inQueue: buildNumber = JsonPathResult(response.response, 'lastBuild.number').get() break # polls until the job completes while True: time.sleep(poll_interval) response = request.get(jobContext + str(buildNumber) + '/api/json', contentType = 'application/json') buildStatus = JsonPathResult(response.response, 'result').get() duration = JsonPathResult(response.response, 'duration').get() if buildStatus and duration != 0: break print "Job '%s' #%s" % (jobName, buildNumber)