How to modify the service endpoint of an Azure Pipeline via API? - azure-devops

I want to modify the service endpoint of a pipeline in Azure DevOps via API.
Example:
Pipeline "build-a-release" uses my personal service endpoint "hello1". But I want it to use existing service endpoint "my-companys-global-service-endpoint" instead.
The documentation https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/pipelines?view=azure-devops-rest-6.0 does not show any "update" function, but I can update the service endpoint of a pipeline in the Azure DevOps GUI.
How can I automate this process via a script and API?

To update a service endpoint, you should use Endpoints - Update Service Endpoint API:
PUT https://dev.azure.com/{organization}/_apis/serviceendpoint/endpoints/{endpointId}?api-version=6.0-preview.4
To update a pipeline defintion, you could use Definitions - Update api to update it. When you capture the network log, you could see there is connectedServiceId in properties of repository parameter. You could update the connectedServiceId to change the service connection.
From my test, something you need to notice:
Make sure the request headers include the following items:
Make sure you have the correct "revision" value (the latest revision) in the body.

Related

Querying the agent a build step was run on from Azure DevOps via REST API

I need to extract information from a finished pipeline build in Azure DevOps. I can get the tree of steps in the executed plan for that build and the attached logs. The final piece of information that I'm looking for is the name of the build agent that was used to execute a certain step. How can I query the agent? The web frontend clearly shows the info, but I don't see it exposed via the documented API.
Querying the agent a build step was run on from Azure DevOps via REST API
First, there are Use predefined variables, which provide a list of predefined variables. You could use the Agent.Name to get the name of the build agent.
If you want to use REST API to get the name of it, you could use the REST API Builds - Get Build Log:
GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/logs/{logId}?api-version=6.0
Then we need to parse the log to get the name of the agent:

Call an API residing in Kubernetes from ADF

I have deployed an API with certain business logic in AKS. The load balancer type is internal. I am able to access this within AKS cluster. on the below address
http:servicename/myapi/
But I want to call this API from an ADF pipeline. How can I do that? What are the configurations I should do to be able to call this API from ADF?
You have to use Web Activity to make Rest API call from Azure DataFactory Pipeline.
An Azure Data Factory may be used to call a custom REST endpoint
through Web Activity. You can send datasets and connected services to
the activity to be consumed and accessed.
Note : By utilizing self-hosted integration runtime, Web Activity may also invoke URLs that are hosted on a private virtual network. The URL endpoint should be visible to the integration runtime.
Please check below documentation to know more about web activity : Web Activity in Azure Datafactory.

How to trigger my "Incoming Webhook" Service Connection in Azure DevOps (OnPremise)? The Endpoint URL is unknown

Overview: I try to trigger an Azure DevOps 2020 Pipeline (YAML) in the event that a WorkItem of a specific type is created. So my idea I had was to create a connection between "WorkItem" -> Service Hook of Type "Web Hook" -> Service Connection of Type "Incoming Webhook" -> resource:webhook triggers the Pipeline in YAML.
My problem now is, that I could create the serviceendpoint by GUI or by API, but the documentation and the created endpoint does not make sense for me. I am running my server on-premise and the endpoint I got looks like this:
{"count":1,"value":[{"data":{},"id":"1babbef7-1edb-4b01-bf18-b6e3c309caae","name":"TestIncomingWebhook","type":"incomingwebhook","url":"https://dev.azure.com", ...
The url does not make sense to me.
I can alter the url if I use the REST API to create the endpoint by using this format
POST https://{instance}/{collection}/{project}/_apis/serviceendpoint/endpoints
as described in Microsoft Azure API Documentation.
Questions:
Why is the url that the service connection gets pointed to https://dev.azure.com while I am running on-premise from a server on a totally different url? Would that be the url I have to POST to trigger the ServiceConnection?
What must be the correct URL-Format for my local Server? Where should I point that URL to?
How can I trigger the Service Connection from curl? Do I have to use GET with a lot of parameters or POST with a JSON payload?
I would appreciate if someone could help me out on this. Perhaps someone even got a full solution on how to trigger pipelines on workitem events. I searched a lot but could not find a solution, yet.
Thanks, Peter.
I created the WebHook and ServiceConnection from the GUI like this:
WebHook Configuration Screenshot
ServiceHook Configuration Screenshot
My real problem is, that I got no feedback what the correct ServiceHook URL is. I have no idea where to point my WebHook.
Addition:
The correct URL for accessing the Incoming Webhook Service Endpoint in Azure DevOps is https://{instance}/{Collection name}/_apis/public/distributedtask/webhooks/{Service Connection name}?api-version=6.0-preview
This should be related to your machine settings, you could share the detail steps here
Since you are using Azure DevOps Server 2020, the url format for your local Server should be https://{instance}. You could open Azure DevOps Server Administration Console to check it.
It will trigger the webhook via the event you selected.
You could refer to this ticket for more details.
In addition, you could try this Logic Apps then create flow to trigger build when specific type is created
Update1
Create Service connection Incoming WebHook, eg. Web Hooks name is HookTests and Service connection name is TestHook
Create Web Hooks and enter the URL https://{instance}/{Collection name}/_apis/public/distributedtask/webhooks/{Web Hook name eg. HookTests}?api-version=6.0-preview
Create YAML build and add service connection resources in the yaml pipeline see below:
resources:
webhooks:
- webhook: MyWebhookTrigger
connection: {Service connection name, eg.TestHook}
Above steps actually do below things:
New workitem is created-->automatically Trigger Azure pipeline.
In addition, we could also add task power shell and call REST API to trigger another pipeline.

In Azure Devops I want my yaml pipeline only to execute after the previous execution of the same pipeline has completed

I tried batch: true setting described here, but it seems to:
ignore commits that are pushed when a pipeline is running. I want the last commit to trigger a pipeline after the current run of that pipeline has finished
be ignored when you publish directly from CI by pressing build
Has someone found a way to configure a pipeline to run as I have described.
You can try adding a Invoke REST API check on the agent pool. The rest api will get the previous build and evaluate its status. So when new build is queued targeting the agent pool. The Invoke REST API will be invoked, this new build will only start when the response of the rest api is evaluated to true.
Please check below steps:
1, create a service connection to your azure devops organization.
Go Project Setting--> Service connections under Pipelines-->Click new service connection--> select Generic to create a generic service connection.
Then Edit the service connection type the information shown in below screenshot. Check here to get a Personal access token.
2, add Invoke REST API check on the agent pool**.
Go Project Setting--> Agent Pools under Pipelines-->Select the agent pool--> Click the 3dots -->Click Approvals and checks.
.
3, Click the "+"--> Choose Invoke REST API
4, Edit the Invokde Rest API
Select the generic service connection to your azure devops created in the first step.
Set the fields as below:
URL suffix and parameters: _apis/build/builds?definitions=the DefinitionId of your pipeline&$top=2&queryOrder=queueTimeDescending&api-version=5.1
Success criteria: eq(root['value'][1]['status'], 'completed')
please check here for more information about build rest api.
Note: Since the Invoke Rest api check is set on agent pool scope. It may have effects on other pipelines that target this agent pool. For example, if the desired yaml pipeline is waiting its previous run to complete. And now another pipeline targeting this same agent pool is triggered, it will have to wait for the previous run of desired yaml pipeline to complete too.

Create service connection and use the same in next stage of azure devops multi stage yaml pipeline

I have two yaml templates defined one for creating a docker registry service connection and second for deploying some stuff via container job. The second template uses the docker registry connection which is being deployed in first template. When I am running both the templates separately then both the stages are successful but when I run them in one azure-pipelines.yaml, it fails :
There was a resource authorization issue: "The pipeline is not valid. A service connection with name shared-stratus-acr-endpoint could not be found. The service connection does not exist or has not been authorized for use. For authorization details, refer to https://aka.ms/yamlauthz."
Is there any way like dependsOn or condition that we can provide in this situation?
It's likely that you only authorized the service connection for the individual template\pipelines when you created them. The workflow is not super friendly.
What if you try and authorize the pipeline that is failing for that service connection explicitly. See docs here
You could also just authorize the service connection for all pipelines depending on your security needs.