Cómo usar la API de Azure DevOps para obtener el tiempo de construcción total (in Minutes) Para una piscina de agentes de construcción. Si necesita retroceder del agente comprado a la versión gratuita, Esta guía te ayudará.
Azure DevOps es una plataforma popular para administrar procesos de desarrollo de software. With its REST API, Los desarrolladores pueden interactuar con la plataforma programáticamente y automatizar tareas como la gestión de compilación y la versión. En este tutorial, we will demonstrate how to use the Azure DevOps API to get the total build time (in minutes) Para un grupo de agentes de compilación dentro de un rango de fecha específico. We will provide step-by-step instructions and use Python to make API requests.
Pasos:
- Obtener un Azure DevOps Token de acceso personal (PALMADITA) Before we start, Necesitamos obtener un Azure DevOps Pat, which we will use to authenticate our API requests. Para crear una palmadita, Vaya a la configuración de su organización de Azure DevOps y haga clic en “Tokens de acceso personal”. Siga las instrucciones para crear un nuevo token con el “Construir (leer)” alcance.
- Set up a Python environment We will be using Python to make our API requests. Si no tiene Python instalado, you can download it from the official website (https://www.python.org/downloads/). También recomendamos utilizar un entorno virtual para administrar sus dependencias de Python. Para configurar un entorno virtual, run the following commands en tu terminal:
pip install virtualenv
virtualenv venv
source venv/bin/activate
- Instale la biblioteca de solicitudes que utilizaremos el
requests
library to make our API requests. Para instalarlo, run the following command:
pip install requests
- Define the API endpoint The API endpoint we will be using is:
https://{username}:{pat}@dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=7.0
Reemplazar {username}
, {pat}
,{organization}
y {project}
con su organización y nombres de proyectos reales.
- Make the API request To get the total build time for a build agent pool within a specified date range, Necesitamos usar el
minTime
ymaxTime
query parameters in our API request. También necesitamos filtrar los resultados de compilación por el grupo de agentes de compilación. Here’s an example Python script that makes the API request and calculates the total build time (in minutes):
pip install python-dateutil requests
import requests
from dateutil.parser import parse
# Set up variables for the API request
organization = "<Organization Name>"
project = "Project Name"
queue_id = "9"
min_time = "2023-04-01"
max_time = "2023-04-05"
# you can leave username as user
username = "user"
pat = "<PAT>"
# Construct the API URL
url = f"https://{username}:{pat}@dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=7.0&minTime={min_time}&maxTime={max_time}&$filter=queue.id eq {queue_id}&$orderby=startTime asc"
# Send the API request
response = requests.get(url)
# Parse the response JSON
json_response = response.json()
# Retrieve the count of build runs
count = json_response["count"]
# Calculate the total build time in seconds
total_build_time = 0
for build in json_response["value"]:
start_time = parse(build["startTime"])
finish_time = parse(build["finishTime"])
duration = finish_time - start_time
total_build_time += duration.total_seconds()
# Convert the total build time to minutes
total_build_time_minutes = total_build_time / 60
print(f"Number of build runs: {count}")
print(f"Total build time: {total_build_time_minutes:.2f} minutes")
Ejemplo de salida:
Number of build runs: 35
Total build time: 122.19 minutes