azure-pipelines

Update Pipeline Run Retention


I'm looking to update a lease in a multi-stage pipeline, and my tasks are configured as per the docs

My task fails with the below error

##[error]Invoke-RestMethod : 
{"count":1,"value":{"Message":"The requested resource does not support http method 'PATCH'."}}

The lease is created fine in the Build stage - I just can't update it in a later stage

I'm using the latest API version, where there's clearly a PATCH update

What am I doing wrong here, and has anyone managed to get this to work?

I've seen this issue, but the code is the same as the docs - it still errors out on me

Ran the same tasks as the docs, but get an error as described above


Solution

  • The sample in the doc: Example: Updating the retention window for a multi-stage pipeline based on stage success is using cross-stage variable to pass the newLeaseId to the next stage.

    When the variables of the previous stage are not passed to the next stage, the error you encountered will occur.

    I can reproduce the same issue.

    enter image description here

    To solve the issue, you need to check the cross stage variable settings.

    You need to set dependsOn in the next stage and use the following format to reference the output variables of the previous stage:

    $[ stageDependencies.stagename.jobname.outputs['taskname.MyVar'] ]

    stages:
    - stage: One
      jobs:
      - job: A
        steps:
        - task: MyTask@1  # this step generates the output variable
          name: ProduceVar  # because we're going to depend on it, we need to name the step
    
    - stage: Two
      dependsOn:
      - One
      jobs:
      - job: B
        variables:
          # map the output variable from A into this job
          varFromA: $[ stageDependencies.One.A.outputs['ProduceVar.MyVar'] ]
    

    For example:

    stages:
    - stage: previousstagename
      jobs:
         - job: previousjobname
           steps:
             - task: PowerShell@2
               name: RetainOnSuccess
               displayName: Retain on Success
               inputs:
                failOnStderr: true
                targetType: 'inline'
                script: |
                   $contentType = "application/json";
                   $headers = @{ Authorization = 'Bearer $(System.AccessToken)' };
                   $rawRequest = @{ daysValid = 365; definitionId = $(System.DefinitionId); ownerId = 'User:$(Build.RequestedForId)'; protectPipeline = $false; runId = $(Build.BuildId) };
                   $request = ConvertTo-Json @($rawRequest);
                   $uri = "$(System.CollectionUri)$(System.TeamProject)/_apis/build/retention/leases?api-version=6.0-preview.1";
                   $newLease = Invoke-RestMethod -uri $uri -method POST -Headers $headers -ContentType $contentType -Body $request;
                   $newLeaseId = $newLease.Value[0].LeaseId
                   echo "##vso[task.setvariable variable=newLeaseId;isOutput=true]$newLeaseId";
       
    - stage: Release
      dependsOn: previousstagename
      jobs:
      - job: default
        variables:
        - name: NewLeaseId
          value: $[ stageDependencies.previousstagename.previousjobname.outputs['RetainOnSuccess.newLeaseId']]
        steps:
        - task: PowerShell@2
          condition: and(succeeded(), not(canceled()))
          name: RetainOnSuccess
          displayName: Retain on Success
          inputs:
            failOnStderr: true
            targetType: 'inline'
            script: |
              $contentType = "application/json";
              $headers = @{ Authorization = 'Bearer $(System.AccessToken)' };
              $rawRequest = @{ daysValid = 365 };
              $request = ConvertTo-Json $rawRequest;
              $uri = "$(System.CollectionUri)$(System.TeamProject)/_apis/build/retention/leases/$(newLeaseId)?api-version=7.1-preview.2";
              echo $uri;
              echo $(NewLeaseId);
              Invoke-RestMethod -uri $uri -method PATCH -Headers $headers -ContentType $contentType -Body $request;
    

    Result:

    enter image description here