AzureDevOps Multistage build and release pipeline variable sharing

59 views Asked by At

What am I doing wrong? I've got a multistage build and release pipeline that works in the following way

Build > release to UAT > release to UAT 2 (our prod) but I'm just playing around to get it working

trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

stages:
  - stage: Build
    displayName: 'Build'
    variables:
      solution: '**/*.sln'
      buildPlatform: 'Any CPU'
      buildConfiguration: 'Release'
    jobs:
      - job: Build                  
        steps: 
          - task: NuGetToolInstaller@0
            displayName: 'Use NuGet 5.x'
            inputs:
              versionSpec: 5.x
      
          - task: NuGetCommand@2
            displayName: 'NuGet restore'
            inputs:
              restoreSolution: '**/*.sln' 

          - task: VSBuild@1
            displayName: 'Build solution'
            inputs:
              solution: '$(solution)'
              msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.ArtifactStagingDirectory)\\"'
              platform: '$(buildPlatform)'
              configuration: '$(buildConfiguration)'

          - task: PublishSymbols@2
            displayName: 'Publish symbols path'
            inputs:
              SearchPattern: '**\bin\**\*.pdb'
              PublishSymbols: false
            continueOnError: true

          - task: PublishBuildArtifacts@1
            displayName: 'Publish Artifact'
            inputs:
              PathtoPublish: '$(build.ArtifactStagingDirectory)'
              ArtifactName: 'drop'

  - stage: UAT
    displayName: 'UAT'
    condition: and(succeeded(), eq(variables['Build.SourceBranchName'], 'master'))
    dependsOn: Build

    jobs:
      - deployment: DeployApp
        pool:
          vmImage: 'windows-latest'
        displayName: 'Release artefacts to Test-Evn (Zip)'
        environment: 'test-env'
       
        strategy:
          runOnce:
            deploy:
              steps:
              - task: DownloadPipelineArtifact@2
                displayName: 'Download Artifacts (drop)'
                inputs:
                  buildType: 'current'
                  artifactName: 'drop'
                  targetPath: '$(System.ArtifactsDirectory)'

              - 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; 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";
     
              - script: echo $(RetainOnSuccess.newLeaseId)
                name: echovar

  - stage: UAT2
    displayName: 'UAT2'
    condition: and(succeeded(), eq(variables['Build.SourceBranchName'], 'master'))
    dependsOn: UAT
     
    jobs:
      - deployment: DeployApp
        pool:
          vmImage: 'windows-latest'
        displayName: 'Release artefacts to TestEnv2 (Zip)'
        environment: 'test-env-2'
        variables:
        - name: NewLeaseId
          value: $[ dependencies.UAT.outputs['DeployApp.RetainOnSuccess.newLeaseId']]

        strategy:
          runOnce:
            deploy:
              steps:
              - task: DownloadPipelineArtifact@2
                displayName: 'Download Artifacts (drop)'
                inputs:
                  buildType: 'current'
                  artifactName: 'drop'
                  targetPath: '$(System.ArtifactsDirectory)'

              - script: echo "https://dev.azure.com/testing-ci/$(System.TeamProject)/_apis/build/retention/leases/$(NewLeaseId)?api-version=6.1-preview.2"

              - 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 * 100; leaseId = $newLeaseId; ownerId = 'User:$(Build.RequestedForId)' };
                    $request = ConvertTo-Json @($rawRequest);
                    $uri = "https://dev.azure.com/testing-ci/$(System.TeamProject)/_apis/build/retention/leases/$(NewLeaseId)?api-version=6.1-preview.2";
                    Invoke-RestMethod -uri $uri -method PATCH -Headers $headers -ContentType $contentType -Body $request;

I'm trying to update the retention lock as per documentation

If a release gets put to UAt then apply a lock for a year If a release gets to prod then update that retention indef

the error I'm getting is:

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

That's because its missing the value of the variable and therefore the url that its trying to patch to is

https://dev.azure.com/testing-ci/$(System.TeamProject)/_apis/build/retention/leases/?api-version=6.1-preview.2"

It's missing the value of $(NewLeaseId)

1

There are 1 answers

0
Xiang ZHU On

$[ dependencies.UAT.DeployApp.outputs['RetainOnSuccess.newLeaseId'] ]