Hello @Dhanavath Vishnu
Please refer to the below YML script!
Frist Time RUN: the variable is updated and the boolean flag output is 'True'
Second time run: Few stages are skipped due to variable read as 'False'
Please update the values of Group name, PAT token and Org URL.
trigger:
- master
pool:
vmImage: ubuntu-latest
variables:
- group: AzureIoTEdge-Dev # Link the variable group
stages:
- stage: Stage1
jobs:
- job: Stage1_Job1
steps:
- task: PowerShell@2
displayName: 'Stage1-Job1-Task1'
name: 'Stage1_Job1_Task1'
inputs:
targetType: 'inline'
script: |
$path="module.json"
$content = Get-Content $path
$version = ($content | ConvertFrom-Json).image.tag.version
Write-Host "##vso[task.setvariable variable=Module1_IMAGE_VERSION;isOutput=true]$version"
Write-Host "Stage1_Job1_Task1: Current Version read from the Repo file is : $version"
Write-Host "##vso[task.setvariable variable=currentVersion;isOutput=true;]$version"
- job: Stage1_Job2
dependsOn: Stage1_Job1
variables:
NewVersion: $[dependencies.Stage1_Job1.outputs['Stage1_Job1_Task1.currentVersion']]
steps:
- task: PowerShell@2
displayName: 'Stage1-Job2-Task1'
inputs:
targetType: 'inline'
script: |
Write-Host "Stage1_Job2: The value of currentVersion read from Stage1_Job1_Task1 is $(NewVersion)"
Write-Host "Stage1_Job2: The value of Predefined variable PreviousVersion read from Pipeline Variables is: $(PreviousVersion)"
- task: PowerShell@2
displayName: 'Stage1-Job2-Task2'
name: 'Stage1_Job2_Task2'
inputs:
targetType: 'inline'
script: |
Write-Host "Stage1_Job2_Task2: Checking if image version has changed..."
if ("$(NewVersion)" -ne "$(PreviousVersion)") {
$connectionToken="qh6q"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$URL = "https://dev.azure.com/{}/AzureIoTEdge/_apis/distributedtask/variablegroups/{}?api-version=6.0-preview.1"
$body = '{"id":2,"type":"Vsts","name":"AzureIoTEdge-Dev","variables":{"PreviousVersion":{"isSecret":false,"value":"$(NewVersion)"}}}'
$Result = Invoke-RestMethod -Uri $URL -Headers @{authorization = "Basic $base64AuthInfo"} -Method Put -Body $body -ContentType "application/json"
$Variable = $Result.value.variables | ConvertTo-Json -Depth 100
Write-Output $Variable
Write-Host "##vso[task.setvariable variable=MODULE1_IMAGE_CHANGE;isOutput=true]true"
Write-Host "Stage1_Job2_Task2: Checking if image version has changed...$MODULE1_IMAGE_CHANGE "
} else {
Write-Host "##vso[task.setvariable variable=MODULE1_IMAGE_CHANGE;isOutput=true]false"
}
- job: Stage1_Job3
dependsOn: Stage1_Job2
variables:
Module1ImageChange: $[dependencies.Stage1_Job2.outputs['Stage1_Job2_Task2.MODULE1_IMAGE_CHANGE']]
steps:
- task: PowerShell@2
displayName: 'Stage1-Job3-Task1'
condition: eq(variables['Module1ImageChange'], 'true')
inputs:
targetType: 'inline'
script: |
Write-Host "Stage1_Job3_Task1: The value of Module1ImageChange is true, running new tasks..."
- task: PowerShell@2
displayName: 'Stage1-Job3-Task2'
condition: ne(variables['Module1ImageChange'], 'true')
inputs:
targetType: 'inline'
script: |
Write-Host "Stage1_Job3_Task2: The value of Module1ImageChange is false, skipping new tasks..."
- job: Stage1_Job4
dependsOn: Stage1_Job2
variables:
Module1ImageChange: $[dependencies.Stage1_Job2.outputs['Stage1_Job2_Task2.MODULE1_IMAGE_CHANGE']]
steps:
- task: PowerShell@2
displayName: 'Stage1-Job4-Task1'
condition: eq(variables['Module1ImageChange'], 'true')
inputs:
targetType: 'inline'
script: |
Write-Host "Stage1_Job4_Task1: The value of Module1ImageChange is true, running new tasks..."
- task: PowerShell@2
displayName: 'Stage1-Job4-Task2'
condition: ne(variables['Module1ImageChange'], 'true')
inputs:
targetType: 'inline'
script: |
Write-Host "Stage1_Job4_Task2: The value of Module1ImageChange is false, skipping new tasks..."
If the response is helpful, please click "Accept Answer" and upvote it. So that we can close this thread.