EN VI

Azure-devops - Azure Pipelines - Deployment Environment - Querying (+ deleting) Resources?

2024-03-11 19:00:06
How to Azure-devops - Azure Pipelines - Deployment Environment - Querying (+ deleting) Resources

Within the "Classic" deployment pipelines in Azure Devops we would utilise Azure Deployment Agents Groups to spin up a docker instance and then do things inside the container.

We are moving to YAML pipelines and are struggling to execute the same workflow.

It seems that there isn't feature parity so we need to utilise Environments within DevOps. I have a docker container that can register itself as a resource within a Environment with no issue. I can also use the REST API to query and create these on the fly.

My issue is that it doesn't seem to be possible to delete a resource without nuking the entire environment? We can query then via /_apis/distributedtask/environments/62?expands=resourceReferences

We can of course unregister the resource by running the remove command on the container, but then doesn't cater for the scenario of "what if the docker container dies/goes offline". We would be left with dangling resources which would block the creation of new ones

I originally got around this by creating a new dynamically named environment per container, but it seems that due to YAML evaluating the deployment environments ahead of running, this would not be viable (as the env wouldn't exist!)

So back to the actual question: Is it possible to delete/unregister a resource within an environment?

Solution:

Is it possible to delete/unregister a resource within an environment?

Yes. We can use the following two methods to delete the VM resource in the Pipeline Environment.

Method1: We can manually delete the VM resource via Delete Option in Environment.

Check the screenshot sample

enter image description here

Method2: We can use the API to achieve it. We currently do not have an official Rest API to support this operation, but we can use the API captured by the backend(Browser F12 Option and check Network tab) to delete the Resource in the Environment.

Here is the API Example:

API url:

POST https://dev.azure.com/{OrganizationName}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1

Request Body:

{
    "contributionIds":["ms.vss-environments-web.environment-delete-resource-data-provider"],
    "dataProviderContext":
      {
          "properties":
          {
              "resourceId":"{VMresourceID}",
              "resourceType":"2",
              "sourcePage":
                 {
                     "url":"https://dev.azure.com/{orgname}/{projectname}/_environments/{environmentID}?view=resources",
                     "routeId":"ms.vss-environments-web.environments-route-with-id",
                     "routeValues":{
                          "project":"{Projectname}",
                          "environmentId":"{environmentID}",
                          "viewname":"environment",
                          "controller":"ContributedPage",
                          "action":"Execute"
                      }
                }
        }
    }
}

We can get the related parameters with the Rest API:

GET https://dev.azure.com/{organization}/{project}/_apis/distributedtask/environments/{environmentId}?expands=resourceReferences&api-version=7.1-preview.1
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login