azure-devopsazure-pipelinesazure-pipelines-build-taskazure-pipelines-yamlazure-pipelines-release-task

Is there a way to have a variable group defined at stage level? If so how to access it at Job Level?


I am trying to find a way to define a variable group at stage level and then access it in below jobs through a template? How would I go about doing this?

# Template file getchangedfilesandvariables.yaml
parameters:
  - name: "previouscommitid"
    type: string

    
steps:
  - task: PowerShell@2
    displayName: 'Get the changed files'
    name: CommitIds
    inputs:
      targetType: 'filePath'
      filePath: '$(Build.SourcesDirectory)\AzureDevOpsPipelines\Get-COChangedfiles.ps1'
      arguments: >
        -old_commit_id ${{ previouscommitid }}

  - task: PowerShell@2
    name: PassOutput
    displayName: 'Getting Variables for Packaging'
    inputs:
        targetType: 'filepath'
        filepath: '$(System.DefaultWorkingDirectory)\AzureDevOpsPipelines\Get-COADOvariables.ps1'

And below is my yaml file.

trigger: none
name: $(BuildID)

variables:
  
  system.debug: true
  CodeSigningCertThumbprint: "somethumbprint"
  # Triggering builds on a branch itself.
  ${{ if startsWith(variables['Build.SourceBranch'], 'refs/heads/') }}:
    branchName: $[ replace(variables['Build.SourceBranch'], 'refs/heads/', '') ]
  # Triggering builds from a Pull Request.
  ${{ if startsWith(variables['Build.SourceBranch'], 'refs/pull/') }}:
    branchName: $[ replace(variables['System.PullRequest.SourceBranch'], 'refs/heads/', '') ]

## it will create pipeline package and it will push it private or public feed artifacts
stages:
  - stage: Stage1
    variables:
    - group: Cloudops
    - name: oldcommitid
      value: $[variables.lastcommitid]

    jobs:
    - job: IdentifyChangedFilesAndGetADOVariables
      

      pool:
        name: OnPrem

      workspace:
        clean: all # Ensure the agent's directories are wiped clean before building.

      steps:
      - powershell: |
          [System.Version]$PlatformVersion = ((Get-Content "$(Build.SourcesDirectory)\AzureDevOpsPipelines\PlatformVersion.json") | ConvertFrom-Json).PlatformVersion
          Write-Output "The repository's PlatformVersion is: $($PlatformVersion.ToString())"
          $NewPackageVersion = New-Object -TypeName "System.Version" -ArgumentList @($PlatformVersion.Major, $PlatformVersion.Minor, $(Build.BuildId))

          Write-Output "This run's package version is $($NewPackageVersion.ToString())"
          echo "##vso[task.setvariable variable=NewPackageVersion]$($NewPackageVersion.ToString())"
          echo "##vso[task.setvariable variable=commitidold;isOutput=true]$(oldcommitid)" 
        displayName: 'Define package version.'
        name: commitidorpackageversion
        errorActionPreference: stop

      - template: getchangedfilesandvariables.yaml
        parameters: 
          previouscommitid:
            - $(commitidorpackageversion.commitidold)
           # - $(oldcommitid)

I get the error at the second last line of the code that

/AzureDevOpsPipelines/azure-pipelines.yml (Line: 49, Col: 13): The 'previouscommitid' parameter is not a valid String.

I tried different combinations but I am still getting the errors.

Any ideas?


Solution

  • Thanks for your response. I already had the variable group setup in my library. I was just not able to use it.

    The way I was able to achieve this I created another template file and supplied it to variables section under my stage. After doing this I was able to actually able to use the variables from my variable group in my successive jobs.

    For more information you can review this doc : https://learn.microsoft.com/en-us/azure/devops/pipelines/library/variable-groups?view=azure-devops&tabs=yaml

    stagevariables.yaml 
    
    variables:
    - group: Cloudops
    
    azure-pipelines.yml
        stages:
          - stage: Stage1
            variables:
              - template: stagevariables.yaml
            jobs:
            - job: CheckwhichfeedsAreAvailable