我有一个共享模板,我在我的azure管道中引用它来构建,测试,打包和发布c# nuget包。
DotNet-BuildTestPackPublish.yml
parameters:
- name: project
type: string
default: ''
- name: dotnetVersion
type: string
default: '5.0.x'
- name: isNugetPackage
type: boolean
default: false
- name: isFirstProjectInRepo
type: boolean
default: true
steps:
- task: UseDotNet@2
displayName: 'Add .NET ${{parameters.dotnetVersion}}'
condition: and(succeeded(), eq(${{parameters.isFirstProjectInRepo}}, true))
inputs:
packageType: 'sdk'
version: ${{parameters.dotnetVersion}}
- task: NuGetAuthenticate@0
displayName: 'NuGet Auth'
condition: and(succeeded(), eq(${{parameters.isFirstProjectInRepo}}, true))
- checkout: self
persistCredentials: true
displayName: "Git Checkout"
condition: and(succeeded(), eq(${{parameters.isFirstProjectInRepo}}, true))
- task: DotNetCoreCLI@2
displayName: "Build"
inputs:
command: 'build'
projects: '**/*.sln'
- task: DotNetCoreCLI@2
displayName: Unit Test
inputs:
projects: '**/*.sln'
command: 'test'
- task: PowerShell@2
displayName: "Extract Version"
condition: and(succeeded(), eq(${{parameters.isNugetPackage}}, true), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
inputs:
targetType: 'inline'
script: |
Write-Host "Locating Project Path..."
$input_path = (Get-ChildItem -Include ${{parameters.project}}.csproj -File -Recurse)
Write-Host "Project Path = $input_path"
Write-Host "Getting version from csproj..."
$regex = '(?<=<Version>).*?(?=</Version>)'
$version = Select-String -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value }
Write-Host "Version = $version"
if([string]::IsNullOrWhiteSpace($version))
{
Write-Host "Version not in csproj. Defaulting to 1.0.0"
$version = "1.0.0"
}
Write-Host "Getting Build Number from Environment"
$buildNumber = '$(Build.BuildId)'
Write-Host "Build Number = $buildNumber"
$versionAndBuildNumber = "$version+$buildNumber"
Write-Host "Package Version = $versionAndBuildNumber"
$tagText = "${{parameters.project}}+$version+$buildNumber"
Write-Host "Tag Text = $tagText"
Write-Output "##vso[task.setvariable variable=gitTag]$tagText"
Write-Output "##vso[task.setvariable variable=versionNumber]$versionAndBuildNumber"
- task: PublishSymbols@2
inputs:
searchPattern: '**/bin/**/${{parameters.project}}.pdb'
symbolServerType: "teamServices"
- task: DotNetCoreCLI@2
displayName: "Nuget Pack"
condition: and(succeeded(), eq(${{parameters.isNugetPackage}}, true), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
inputs:
command: 'pack'
packagesToPack: '**/${{parameters.project}}.csproj'
versioningScheme: byEnvVar
versionEnvVar: versionNumber
includesymbols: true
includesource: true
- task: NuGetCommand@2
displayName: "Nuget Push"
condition: and(succeeded(), eq(${{parameters.isNugetPackage}}, true), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
inputs:
command: 'push'
packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg'
nuGetFeedType: 'internal'
publishVstsFeed: 'Internal'
allowPackageConflicts: true
- script: |
echo Current Directory:
cd
dir /s
git tag $(gitTag)
git push --tags origin $(gitTag)
workingDirectory: $(Build.SourcesDirectory)
displayName: "Git Tag Repository"
condition: and(succeeded(), eq(${{parameters.isNugetPackage}}, true), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
如果我的存储库中只有一个项目:
azure-pipelines。Yml(仅一个项目)
trigger:
branches:
include:
- '*'
pool:
vmImage: 'windows-latest'
resources:
repositories:
- repository: BuildScripts
name: Feldryn/BuildScripts
type: git
ref: main
trigger:
- main
variables:
- group: 'Pipeline Shared Variables'
steps:
- template: DotNet-BuildTestPackPublish.yml@BuildScripts
parameters:
project: 'Daemon.Domain.RumbleRunners.Values'
isNugetPackage: true
# the following step is superfluous for this example, but it DOES work
- task: PowerShell@2
displayName: "Replace Project Reference with Nuget"
inputs:
targetType: 'inline'
script: |
cd Daemon.Domain.RumbleRunners
dotnet remove reference ..Daemon.Domain.RumbleRunners.ValuesDaemon.Domain.RumbleRunners.Values.csproj
dotnet add package Daemon.Domain.RumbleRunners.Values
cd..
当我试图在同一管道中为另一个项目再次调用模板时,第一个将开始失败。
这行不通:azure-pipelines。Yml(两个项目)
trigger:
branches:
include:
- '*'
pool:
vmImage: 'windows-latest'
resources:
repositories:
- repository: BuildScripts
name: Feldryn/BuildScripts
type: git
ref: main
trigger:
- main
variables:
- group: 'Pipeline Shared Variables'
steps:
- template: DotNet-BuildTestPackPublish.yml@BuildScripts
parameters:
project: 'Daemon.Domain.RumbleRunners.Values'
isNugetPackage: true
- task: PowerShell@2
displayName: "Replace Project Reference with Nuget"
inputs:
targetType: 'inline'
script: |
cd Daemon.Domain.RumbleRunners
dotnet remove reference ..Daemon.Domain.RumbleRunners.ValuesDaemon.Domain.RumbleRunners.Values.csproj
dotnet add package Daemon.Domain.RumbleRunners.Values
cd..
# the inclusion of following step is the only change
- template: DotNet-BuildTestPackPublish.yml@BuildScripts
parameters:
project: 'Daemon.Domain.RumbleRunners'
isNugetPackage: true
isFirstProjectInRepo: false
'fatal: not a git repository(或任何父目录):.git'当尝试标记第一个项目时。
我已经做了一些故障排除并确定:
- 这不是我的git标签步骤有问题。
- 当有一个项目
- 如果我注释了git标签步骤,则失败将移到以下步骤。
- 新的错误:无法找到任何项目在
D:a1sDaemon.Domain.RumbleRunners
.
- 新的错误:无法找到任何项目在
我已经输出了我的工作目录的内容,可以看到所有期望的文件都存在,包括.git文件夹 - 工作路径和非工作路径的工作目录没有区别。
我不知道如何进一步排除故障。
为什么我不能调用模板两次?这里发生了什么?
checkout
任务在有多个签出时表现不同:
- 当只有一个
checkout: self
步骤时,它将检出存储库到/home/vsts/work/1/s/
。 - 当有多个
checkout
步骤时(即使它们引用同一个存储库),它将检出到/home/vsts/work/1/s/{repository_name}
。
即使在第二次签出时有条件,该步骤仍然计数,因此是第2点。适用。
为了验证这一点,检查checkout
任务的日志。你也可以尝试最小再生产管道:
steps:
- checkout: self # <-- /home/vsts/work/1/s/
和
steps:
- checkout: self # <-- /home/vsts/work/1/s/{repo_name}/
- checkout: self # <-- /home/vsts/work/1/s/{repo_name}/
condition: false