在 Azure Pipeline .NET Core CLI 任务中从 csproj 检索项目版本
Retrieving project version from csproj in Azure Pipeline .NET Core CLI task
我在 Azure DevOps 的 yaml 文件中有以下任务
它发布我的控制台应用程序并将其压缩到一个文件中
- task: DotNetCoreCLI@2
inputs:
command: 'publish'
publishWebProjects: false
projects: '**/MyApp.csproj'
arguments: '-r win-x64 -p:PublishSingleFile=True --self-contained true -o $(Build.ArtifactStagingDirectory)'
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
publishLocation: 'Container'
我是直接在Visual Studio中的项目文件上设置项目版本的,我做任何修改的时候。
现在,当我在 AzureDevOps 中发布项目并自定义文件名时,我正在尝试获取该版本,假设对于项目版本 1.2.1,在 azure 中发布的存档文件输出应该是 MyApp-v1.2。1.zip
目前它只输出文件 MyApp.zip
不确定我的版本控制方法是否正确,非常感谢您的意见!
更新:
不幸的是,提供的答案对我不起作用,我可以获得版本但无法重命名文件,即使复制下面的确切脚本也会生成相同的 zip 文件,而不是重命名!该文件夹将被重命名,但 zip 文件保持为 MyApp.zip。
我现在有以下脚本,最后尝试使用 PowerShell 重命名,但成功完成执行并再次在放置文件夹中产生完全相同的 MyApp.zip 文件
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$xml = [Xml] (Get-Content .\MyApp\MyApp.csproj)
$version = $xml.Project.PropertyGroup.Version
echo $version
echo "##vso[task.setvariable variable=version]$version"
- task: DotNetCoreCLI@2
inputs:
command: 'publish'
publishWebProjects: false
projects: '**/MyApp.csproj'
arguments: '-r win-x64 -p:PublishSingleFile=True --self-contained true -o $(Build.ArtifactStagingDirectory)'
- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact'
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
condition: succeededOrFailed()
- task: PowerShell@2
inputs:
targetType: 'inline'
script: 'Rename-Item -Path "$(Build.ArtifactStagingDirectory)\MyApp.zip" -NewName "$(Build.ArtifactStagingDirectory)\MyApp-Win-x64-v$(version).zip"'
但是上面的脚本也不起作用,好像我们没有权限更改那个 zip 文件,无论如何我都不能重命名它。
已修复 - 解决方案
我必须将重命名 power shell 脚本移动到发布 Artifact 之前
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$xml = [Xml] (Get-Content .\MyApp\MyApp.csproj)
$version = $xml.Project.PropertyGroup.Version
echo $version
echo "##vso[task.setvariable variable=version]$version"
- task: DotNetCoreCLI@2
inputs:
command: 'publish'
publishWebProjects: false
projects: '**/MyApp.csproj'
arguments: '-r win-x64 -p:PublishSingleFile=True --self-contained true -o $(Build.ArtifactStagingDirectory)'
- task: PowerShell@2
inputs:
targetType: 'inline'
script: 'Rename-Item -Path "$(Build.ArtifactStagingDirectory)\MyApp.zip" -NewName "$(Build.ArtifactStagingDirectory)\MyApp-Win-x64-v$(version).zip"'
- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact'
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
condition: succeededOrFailed()
要设置输出文件(zip 文件)的名称,您需要在 dotnet build 参数中设置相应的格式。
--configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory)/MyApp-$(version)
为了获取csproj文件中的Version值,可以在Dotnet Publish任务之前添加一个powershell任务。然后你可以从 csproj
检索项目版本
这是我的例子:
- powershell: |
$xml = [Xml] (Get-Content .\MyApp.csproj)
$version = $xml.Project.PropertyGroup.Version
echo $version
echo "##vso[task.setvariable variable=version]$version"
displayName: 'PowerShell Script'
- task: DotNetCoreCLI@2
displayName: Publish
inputs:
command: publish
publishWebProjects: false
projects: '**/*.csproj'
arguments: '--configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory)/MyApp-$(version)'
- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact'
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
condition: succeededOrFailed()
结果:
您也可以尝试将名称设置为$(build.artifactstagingdirectory)/MyApp-v$(version)
输出名称将是 MyApp-v1.0.0.zip
更新:
我已经在 Yaml Pipeline 中测试了你的样本,我可以重现这个问题。
要解决此问题,您可以在 Dotnet Publish 任务中添加参数:modifyOutputPath: false
。
这是 yaml 示例:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$xml = [Xml] (Get-Content .\MyApp\MyApp.csproj)
$version = $xml.Project.PropertyGroup.Version
echo $version
echo "##vso[task.setvariable variable=version]$version"
- task: DotNetCoreCLI@2
displayName: Publish
inputs:
command: publish
publishWebProjects: false
projects: '**/*.csproj'
arguments: '--configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory)/MyApp-v$(version)'
modifyOutputPath: false
- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact'
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
condition: succeededOrFailed()
我在 Azure DevOps 的 yaml 文件中有以下任务 它发布我的控制台应用程序并将其压缩到一个文件中
- task: DotNetCoreCLI@2
inputs:
command: 'publish'
publishWebProjects: false
projects: '**/MyApp.csproj'
arguments: '-r win-x64 -p:PublishSingleFile=True --self-contained true -o $(Build.ArtifactStagingDirectory)'
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
publishLocation: 'Container'
我是直接在Visual Studio中的项目文件上设置项目版本的,我做任何修改的时候。 现在,当我在 AzureDevOps 中发布项目并自定义文件名时,我正在尝试获取该版本,假设对于项目版本 1.2.1,在 azure 中发布的存档文件输出应该是 MyApp-v1.2。1.zip 目前它只输出文件 MyApp.zip
不确定我的版本控制方法是否正确,非常感谢您的意见!
更新:
不幸的是,提供的答案对我不起作用,我可以获得版本但无法重命名文件,即使复制下面的确切脚本也会生成相同的 zip 文件,而不是重命名!该文件夹将被重命名,但 zip 文件保持为 MyApp.zip。
我现在有以下脚本,最后尝试使用 PowerShell 重命名,但成功完成执行并再次在放置文件夹中产生完全相同的 MyApp.zip 文件
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$xml = [Xml] (Get-Content .\MyApp\MyApp.csproj)
$version = $xml.Project.PropertyGroup.Version
echo $version
echo "##vso[task.setvariable variable=version]$version"
- task: DotNetCoreCLI@2
inputs:
command: 'publish'
publishWebProjects: false
projects: '**/MyApp.csproj'
arguments: '-r win-x64 -p:PublishSingleFile=True --self-contained true -o $(Build.ArtifactStagingDirectory)'
- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact'
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
condition: succeededOrFailed()
- task: PowerShell@2
inputs:
targetType: 'inline'
script: 'Rename-Item -Path "$(Build.ArtifactStagingDirectory)\MyApp.zip" -NewName "$(Build.ArtifactStagingDirectory)\MyApp-Win-x64-v$(version).zip"'
但是上面的脚本也不起作用,好像我们没有权限更改那个 zip 文件,无论如何我都不能重命名它。
已修复 - 解决方案
我必须将重命名 power shell 脚本移动到发布 Artifact 之前
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$xml = [Xml] (Get-Content .\MyApp\MyApp.csproj)
$version = $xml.Project.PropertyGroup.Version
echo $version
echo "##vso[task.setvariable variable=version]$version"
- task: DotNetCoreCLI@2
inputs:
command: 'publish'
publishWebProjects: false
projects: '**/MyApp.csproj'
arguments: '-r win-x64 -p:PublishSingleFile=True --self-contained true -o $(Build.ArtifactStagingDirectory)'
- task: PowerShell@2
inputs:
targetType: 'inline'
script: 'Rename-Item -Path "$(Build.ArtifactStagingDirectory)\MyApp.zip" -NewName "$(Build.ArtifactStagingDirectory)\MyApp-Win-x64-v$(version).zip"'
- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact'
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
condition: succeededOrFailed()
要设置输出文件(zip 文件)的名称,您需要在 dotnet build 参数中设置相应的格式。
--configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory)/MyApp-$(version)
为了获取csproj文件中的Version值,可以在Dotnet Publish任务之前添加一个powershell任务。然后你可以从 csproj
检索项目版本这是我的例子:
- powershell: |
$xml = [Xml] (Get-Content .\MyApp.csproj)
$version = $xml.Project.PropertyGroup.Version
echo $version
echo "##vso[task.setvariable variable=version]$version"
displayName: 'PowerShell Script'
- task: DotNetCoreCLI@2
displayName: Publish
inputs:
command: publish
publishWebProjects: false
projects: '**/*.csproj'
arguments: '--configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory)/MyApp-$(version)'
- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact'
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
condition: succeededOrFailed()
结果:
您也可以尝试将名称设置为$(build.artifactstagingdirectory)/MyApp-v$(version)
输出名称将是 MyApp-v1.0.0.zip
更新:
我已经在 Yaml Pipeline 中测试了你的样本,我可以重现这个问题。
要解决此问题,您可以在 Dotnet Publish 任务中添加参数:modifyOutputPath: false
。
这是 yaml 示例:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$xml = [Xml] (Get-Content .\MyApp\MyApp.csproj)
$version = $xml.Project.PropertyGroup.Version
echo $version
echo "##vso[task.setvariable variable=version]$version"
- task: DotNetCoreCLI@2
displayName: Publish
inputs:
command: publish
publishWebProjects: false
projects: '**/*.csproj'
arguments: '--configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory)/MyApp-v$(version)'
modifyOutputPath: false
- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact'
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
condition: succeededOrFailed()