agent: |
UPnD6XgEACcQK53VUSYKGet last build status for a Jenkins job
Get last build status for a Jenkins job
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
Python script to check the status of the last triggered Jenkins job for the pipeline 'taskservice'
inputs
outputs
import requests
import time
# Get environment variables
jenkins_username = getEnvVar('JENKINS_USERNAME')
jenkins_api_token = getEnvVar('JENKINS_API_TOKEN')
jenkins_url = getEnvVar('JENKINS_URL')
# Jenkins API endpoint to get the last build status
# pipeline_name = 'taskservice'
url = f'{jenkins_url}/job/{pipeline_name}/lastBuild/api/json'
# Function to get the build status
def get_build_status(url, auth):
response = requests.get(url, auth=auth)
if response.status_code == 200:
data = response.json()
return data.get('result', 'IN_PROGRESS')
else:
return f'Failed to fetch build status: {response.status_code}'
# Wait for the build to complete
max_attempts = 10
attempt = 0
status = 'IN_PROGRESS'
while status == 'IN_PROGRESS' and attempt < max_attempts:
status = get_build_status(url, (jenkins_username, jenkins_api_token))
if status == 'IN_PROGRESS':
time.sleep(10) # Wait for 10 seconds before checking again
attempt += 1
# Print inputs and outputs
print('Pipeline Name:', pipeline_name)
print('Build Status:', status)
# Output the build status
status
copied