Új Azure DevOps Build létrehozása és változóinak frissítése a REST API segítségével.
Az Azure DevOps egy átfogó fejlesztői eszközök és szolgáltatások, amelyek célja, hogy segítse a csapatokat gyorsabban és hatékonyabban szoftvereket készíteni és szállítani.. Az Azure DevOps egyik kulcsfontosságú funkciója az összeállítási rendszere, amely lehetővé teszi a csapatok számára a felépítés automatizálását, teszt, és a szoftverük telepítési folyamata.
Ebben a cikkben, Megvizsgáljuk, hogyan hozhatunk létre új Azure DevOps buildet és frissíthetjük a változóit a REST API használatával. Egy minta kódrészletet fogunk használni, amely bemutatja, hogyan hozhat létre új buildet a cURL használatával, de bármilyen programozási nyelvet használhat, amely támogatja a REST API-hívásokat.
Az új Azure DevOps build létrehozásának első lépése a tulajdonságainak meghatározása, mint például az összeállítás definíciója és az összeállítási paraméterek. A build definíciója határozza meg az összeépítési lépéseket, amelyek tartalmazhatnak olyan feladatokat, mint a kódfordítás, tesztek futtatása, and creating artifacts. The build parameters are variables that are used by the build steps to customize the build process.
To create a new build using REST API, you need to send a POST request to the Azure DevOps API endpoint that creates builds. The following code snippet demonstrates how to create a new build using cURL:
curl -X POST "https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=7.0" \
-H "Content-Type: application/json" \
-d '{
"definition": {
"id": {definition_id}
},
"parameters": "{\"{parameter_name}\":\"{parameter_value}\"}"
}'
Working example with PAT:
curl -X POST "https://{username}:{PAT}@dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=7.0" \
-H "Content-Type: application/json" \
-d '{
"definition": {
"id": {definition_id}
},
"parameters": "{\"{parameter_name}\":\"{parameter_value}\"}"
}'
Let’s break down this code snippet:
- A
-X POST
flag specifies that we want to send a POST request. - The API endpoint for creating builds is https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=7.0, ahol {organization} is the name of your Azure DevOps organization and {project} is the name of your project. A
api-version
parameter specifies the version of the API we want to use. - A
-H "Content-Type: application/json"
flag specifies that we are sending JSON data in the request body. - The request body is a JSON object that contains the following properties:
definition
: an object that specifies the build definition. Aid
property specifies the ID of the build definition.parameters
: a JSON-formatted string that specifies the build parameters. A{parameter_name}
és{parameter_value}
placeholders should be replaced with the actual parameter name and value, illetőleg.
In the example code snippet, we are creating a new build using the build definition with ID 15 and updating its NUMBER_OF_WORKERS
parameter with the value of a variable 2_NUMBER_OF_WORKERS
, which can be replaced with an actual value at runtime. We are also setting the NUMBER_OF_WORKERS2
parameter to a static value of 5.
To update a build variable using REST API, you need to send a PATCH request to the API endpoint that updates build variables. The following code snippet demonstrates how to update a build variable using cURL:
curl -X PATCH "https://dev.azure.com/{organization}/{project}/_apis/build/builds/{build_id}/variables?api-version=7.0" \
-H "Content-Type: application/json" \
-d '{
"variables": {
"{variable_name}": {
"value": "{new_value}"
}
}
}'
Let’s break down this code snippet:
- A
-X PATCH
flag specifies that we want to send a PATCH request. - The API endpoint for updating build variables is `https://dev.azure.com/{organization}/{project}/_apis/build/builds/{build_id}/variables?api-version=7.0