英文:
Powershell variable in Azure yml file is not passed from one stage to other stages
问题
Variable when output is not getting passed to other stages.
I am getting the major and minor version from .version file which is in same root folder as azure-pipelines.yml file. The first stage "GetVersion" works fine and prints the value of manor and minor but the value of major and minor is never passed to BuildMasterVersion" stage and "TagRelease" stage. Hence I see error below for BuildMasterVersion
The value is
Async Command Start: Update Build Number
Update build number to ..8 for build 14074
Async Command End: Update Build Number
Finishing: PowerShell
And below error for TagRelease
major : The term 'major' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\grmn\prj\mrn\vsts-agent_work_temp\97b95723-b57c-451c-9e65-17cbecac17cf.ps1:3 char:20
- if ($(git tag -l $(major).$(minor).20)) {
-
~~~~~ + CategoryInfo : ObjectNotFound: (major:String) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : CommandNotFoundException
##[error]PowerShell exited with code '1'.
Finishing: PowerShell
Below is the azure-pipelines.yml
pool:
name: Marine-Server
stages:
- stage: GetVersion
displayName: Get Major and Minor Version from .Version File
jobs:- job: Get_Major_Minor_From_Version_File
steps:- checkout: self
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$version = Get-Content -Path .version -TotalCount 1
$versionSplit = $version.Split(".")
$major = $versionSplit[0]
$minor = $versionSplit[1]
Write-Host ##vso[task.setvariable variable=major;isOutput=true]$major
Write-Host "The value of major is $($major)"
Write-Host ##vso[task.setvariable variable=minor;isOutput=true]$minor
Write-Host "The value of minor is $($minor)"
name: 'GetMajorMinorVersion'
- job: Get_Major_Minor_From_Version_File
# Versioning master branch builds
-
stage: BuildMasterVersion
displayName: Build_Master_Version_Number
#change to 'refs/head/master' or master when done testing
condition: and(eq(variables['Build.SourceBranchName'], 'convert-to-commonjs-module-with-tags-implementation'), eq(variables['Build.Reason'], 'IndividualCI'))
jobs:- job: Build_Master_Version_Number
variables:
#patch: $[counter(variables['minor'], 0)]
variables:
major: $[stageDependencies.GetVersion.Get_Major_Minor_From_Version_File.outputs['GetMajorMinorVersion.major']]
minor: $[stageDependencies.GetVersion.Get_Major_Minor_From_Version_File.outputs['GetMajorMinorVersion.minor']]
semantic: $(major).$(minor)
buildNum: $[counter(variables['semantic'], 0)]
steps:- checkout: self
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
Write-Host "The value is $(major)"
Write-Host "##vso[build.updatebuildnumber]$(major).$(minor).$(buildNum)"
name: 'SetMasterBuildName'
- job: Build_Master_Version_Number
-
stage: TagRelease
when done testing update branch to master
condition: and(eq(variables['Build.SourceBranchName'], 'convert-to-commonjs-module-with-tags-implementation'), eq(variables['Build.Reason'], 'IndividualCI'))
jobs:- job: Tag
variables:
semantic: $major.$minor
buildNum: $[counter(variables['semantic'], 0)]
workspace:
clean: allvariables:
patch: $[counter(variables['minor'], 0)]
steps:
- checkout: self
persistCredentials: true - task: PowerShell@2
inputs:
targetType: 'inline'
script: |
if ($(git tag -l $(major).$(minor).$(buildNum))) {
Write-Output "$(major).$(minor).$(buildNum) already released"
}
else {
git tag $(major).$(minor).$(buildNum)
git push origin $(major).$(minor).$(buildNum)
}
workingDirectory: $(Build.SourcesDirectory)
displayName: 'Conditionally Tag'
- checkout: self
- job: Tag
英文:
Variable when output is not getting passed to other stages.
I am getting the major and minor version from .version file which is in same root folder as azure-pipelines.yml file. The first stage "GetVersion" works fine and prints the value of manor and minor but the value of major and minor is never passed to BuildMasterVersion" stage and "TagRelease" stage. Hence I see error below for BuildMasterVersion
The value is
Async Command Start: Update Build Number
Update build number to ..8 for build 14074
Async Command End: Update Build Number
Finishing: PowerShell
And below error for TagRelease
major : The term 'major' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\grmn\prj\mrn\vsts-agent\_work\_tempb95723-b57c-451c-9e65-17cbecac17cf.ps1:3 char:20
+ if ($(git tag -l $(major).$(minor).20)) {
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (major:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : CommandNotFoundException
##[error]PowerShell exited with code '1'.
Finishing: PowerShell
Below is the azure-pipelines.yml
pool:
name: Marine-Server
stages:
- stage: GetVersion
displayName: Get Major and Minor Version from .Version File
jobs:
- job: Get_Major_Minor_From_Version_File
steps:
- checkout: self
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$version = Get-Content -Path .version -TotalCount 1
$versionSplit = $version.Split(".")
$major = $versionSplit[0]
$minor = $versionSplit[1]
Write-Host ##vso[task.setvariable variable=major;isOutput=true]$major
Write-Host "The value of major is $($major)"
Write-Host ##vso[task.setvariable variable=minor;isOutput=true]$minor
Write-Host "The value of minor is $($minor)"
name: 'GetMajorMinorVersion'
# # Versioning master branch builds
- stage: BuildMasterVersion
displayName: Build_Master_Version_Number
#change to 'refs/head/master' or master when done testing
condition: and(eq(variables['Build.SourceBranchName'], 'convert-to-commonjs-module-with-tags-implementation'), eq(variables['Build.Reason'], 'IndividualCI'))
jobs:
- job: Build_Master_Version_Number
# variables:
#patch: $[counter(variables['minor'], 0)]
variables:
major: $[stageDependencies.GetVersion.Get_Major_Minor_From_Version_File.outputs['GetMajorMinorVersion.major']]
minor: $[stageDependencies.GetVersion.Get_Major_Minor_From_Version_File.outputs['GetMajorMinorVersion.minor']]
semantic: $(major).$(minor)
buildNum: $[counter(variables['semantic'], 0)]
steps:
- checkout: self
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
Write-Host "The value is $(major)"
Write-Host "##vso[build.updatebuildnumber]$(major).$(minor).$(buildNum)"
name: 'SetMasterBuildName'
- stage: TagRelease
# when done testing update branch to master
condition: and(eq(variables['Build.SourceBranchName'], 'convert-to-commonjs-module-with-tags-implementation'), eq(variables['Build.Reason'], 'IndividualCI'))
jobs:
- job: Tag
variables:
semantic: $major.$minor
buildNum: $[counter(variables['semantic'], 0)]
workspace:
clean: all
# variables:
# patch: $[counter(variables['minor'], 0)]
steps:
- checkout: self
persistCredentials: true
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
if ($(git tag -l $(major).$(minor).$(buildNum))) {
Write-Output "$(major).$(minor).$(buildNum) already released"
}
else {
git tag $(major).$(minor).$(buildNum)
git push origin $(major).$(minor).$(buildNum)
}
workingDirectory: $(Build.SourcesDirectory)
displayName: 'Conditionally Tag'
答案1
得分: 1
在你的阶段Tagrelease中,变量不可用,因为你没有映射它们。但在之前的阶段buildmasterversion中,你已经这样做了。
所以你应该将buildmasterversion中映射的变量major和minor复制到tagrelease中:
buildmasterversion:
variables:
major: $[ stageDependencies.GetVersion.Get_Major_Minor_From_Version_File.outputs['GetMajorMinorVersion.major'] ]
minor: $[ stageDependencies.GetVersion.Get_Major_Minor_From_Version_File.outputs['GetMajorMinorVersion.minor'] ]
semantic: $(major).$(minor)
buildNum: $[counter(variables['semantic'], 0)]
Tagrelease:
variables:
major: $[ stageDependencies.GetVersion.Get_Major_Minor_From_Version_File.outputs['GetMajorMinorVersion.major'] ]
minor: $[ stageDependencies.GetVersion.Get_Major_Minor_From_Version_File.outputs['GetMajorMinorVersion.minor'] ]
semantic: $major.$minor
buildNum: $[counter(variables['semantic'], 0)]
在需要共享的变量之间的阶段之间,需要将它们映射到每个需要它们的作业,可以参考文档。
英文:
In your stage Tagrelease the variables are not available because you haven't mapped them. Which you did do in the stage before that, in buildmasterversion.
So you should copy the mapped variables major and minor from the buildmasterversion to tagrelease:
buildmasterversion:
variables:
major: $[ stageDependencies.GetVersion.Get_Major_Minor_From_Version_File.outputs['GetMajorMinorVersion.major'] ]
minor: $[ stageDependencies.GetVersion.Get_Major_Minor_From_Version_File.outputs['GetMajorMinorVersion.minor'] ]
semantic: $(major).$(minor)
buildNum: $[counter(variables['semantic'], 0)]
Tagrelease:
variables:
major: $[ stageDependencies.GetVersion.Get_Major_Minor_From_Version_File.outputs['GetMajorMinorVersion.major'] ]
minor: $[ stageDependencies.GetVersion.Get_Major_Minor_From_Version_File.outputs['GetMajorMinorVersion.minor'] ]
semantic: $major.$minor
buildNum: $[counter(variables['semantic'], 0)]
Variables that are shared between stages need to be mapped to every job that needs them, as you can see in the documentation
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论