首页 > 解决方案 > Azure YAML 管道 - 作业/任务包问题

问题描述

因此,我一直在对 Azure DevOps Yaml Pipelines 进行一些学习,但遇到了一个我似乎无法弄清楚原因的问题。

我正在为一个小型类库解决方案构建我的第一个管道,其想法是在将更改提交到 master 时恢复、构建、测试、打包和发布它。

我将部署的不同部分拆分为阶段/作业(这可能不是使用这些的正确方法),但是当我这样做时,“Nuget pack”步骤永远找不到任何构建的文件。

此 YAML 不起作用,并且在“NuGet(Pack)”步骤中出错,因为它找不到“projects.assets.json”文件,我已确认构建步骤确实产生了该文件。

trigger:
- master

pool:
  vmImage: 'windows-latest'

name: 'Set dynamically'

variables:
  buildConfiguration: 'Release'
  version.Major: 1
  version.Minor: $[counter(variables['version.Major'], 0)]
  version.Patch: 0
  version.Revision: $[counter(variables['version.Minor'], 0)]
  version.Number: '$(version.Major).$(version.Minor).$(version.Patch).$(version.Revision)'

stages:
- stage: Prepare
  jobs:
    - job: Prepare_Sources
      steps:
      - checkout: self
        clean: true

    - job: Prepare_BuildAndVersionNumbers
      steps:
      - task: PowerShell@2
        displayName: Set the name of the build
        inputs:
          targetType: 'inline'
          script: |
            [string] $dateTime = (Get-Date -Format 'yyyyMMdd')
            [string] $buildName = "$(Build.DefinitionName)_$(Build.SourceBranchName)_$($dateTime)_$(version.Number)"
            Write-Host "Setting the name of the build to '$buildName'."
            Write-Host "##vso[build.updatebuildnumber]$buildName"

- stage: Build
  jobs:
    - job: BuildRestore
      steps:
      - task: NuGetCommand@2
        displayName: 'Restore (NuGet)'
        inputs:
          command: restore
          restoreSolution: '**\*.sln'fs
          feedsToUse: select
          includeNuGetOrg: true
          vstsFeed: 'internalfeed1'
          arguments: '--configuration $(buildConfiguration) /p:Version=$(version.Number)'

      - task: DotNetCoreCLI@2
        displayName: 'Restore (.NET Core)'
        inputs:
          command: restore
          includeNuGetOrg: true
          nobuild: true
          vstsFeed: 'internalfeed1'
          nuGetFeedType: internal
          projects: '**/*.csproj'
          arguments: '--configuration $(buildConfiguration) /p:Version=$(version.Number)'

      - task: DotNetCoreCLI@2
        displayName: 'Build all projects in solution'
        inputs:
          command: build
          projects: '**/*.csproj'
          arguments: '--configuration $(buildConfiguration) /p:Version=$(version.Number)'

- stage: Test
  jobs:
    - job: Test_UnitTests
      steps:
      - task: DotNetCoreCLI@2
        displayName: 'Run & Analyse UnitTests'
        inputs:
          command: test
          projects: '**/*Tests/*UnitTests.csproj'
          arguments: '--configuration $(buildConfiguration) --collect "Code coverage"'

- stage: Package
  jobs:
    - job: Package_Nuget
      steps:
      - task: NuGetAuthenticate@0
        displayName: "Nuget (Authenticate)"
      
      - task: DotNetCoreCLI@2
        displayName: 'NuGet (Package)'
        inputs:
          nobuild: true
          command: pack
          packagesToPack: '**/*.csproj'
          versioningScheme: byBuildNumber
          arguments: '--configuration $(buildConfiguration)'

      - task: DotNetCoreCLI@2
        displayName: 'NuGet (Publish)'
        inputs:
          command: push
          searchPatternPush: '$(Build.ArtifactStagingDirectory)/*.nupkg;'
          feedPublish: 'internalfeed1'
          

如果我将其全部简化为一项没有阶段/作业的单一作业,则部署一切正常(如下所示)

trigger:
- master

pool:
  vmImage: 'windows-latest'

name: 'Set dynamically'

variables:
  buildConfiguration: 'Release'
  version.Major: 1
  version.Minor: $[counter(variables['version.Major'], 0)]
  version.Patch: 0
  version.Revision: $[counter(variables['version.Minor'], 0)]
  version.Number: '$(version.Major).$(version.Minor).$(version.Patch).$(version.Revision)'

steps:
      - checkout: self
        clean: true

    - task: PowerShell@2
        displayName: Set the name of the build
        inputs:
          targetType: 'inline'
          script: |
            [string] $dateTime = (Get-Date -Format 'yyyyMMdd')
            [string] $buildName = "$(Build.DefinitionName)_$(Build.SourceBranchName)_$($dateTime)_$(version.Number)"
            Write-Host "Setting the name of the build to '$buildName'."
            Write-Host "##vso[build.updatebuildnumber]$buildName"

    - task: NuGetCommand@2
        displayName: 'Restore (NuGet)'
        inputs:
          command: restore
          restoreSolution: '**\*.sln'fs
          feedsToUse: select
          includeNuGetOrg: true
          vstsFeed: 'internalfeed1'
          arguments: '--configuration $(buildConfiguration) /p:Version=$(version.Number)'

    - task: DotNetCoreCLI@2
        displayName: 'Restore (.NET Core)'
        inputs:
          command: restore
          includeNuGetOrg: true
          nobuild: true
          vstsFeed: 'internalfeed1'
          nuGetFeedType: internal
          projects: '**/*.csproj'
          arguments: '--configuration $(buildConfiguration) /p:Version=$(version.Number)'

    - task: DotNetCoreCLI@2
        displayName: 'Build all projects in solution'
        inputs:
          command: build
          projects: '**/*.csproj'
          arguments: '--configuration $(buildConfiguration) /p:Version=$(version.Number)'

      - task: DotNetCoreCLI@2
        displayName: 'Run & Analyse UnitTests'
        inputs:
          command: test
          projects: '**/*Tests/*UnitTests.csproj'
          arguments: '--configuration $(buildConfiguration) --collect "Code coverage"'

      - task: DotNetCoreCLI@2
        displayName: 'NuGet (Package)'
        inputs:
          nobuild: true
          command: pack
          packagesToPack: '**/*.csproj'
          versioningScheme: byBuildNumber
          arguments: '--configuration $(buildConfiguration)'

      - task: DotNetCoreCLI@2
        displayName: 'NuGet (Publish)'
        inputs:
          command: push
          searchPatternPush: '$(Build.ArtifactStagingDirectory)/*.nupkg;'
          feedPublish: 'internalfeed1'
          

在这些难题的文档上找不到答案,可以解释为什么在分成阶段/工作时它不起作用,有人知道原因是什么吗?阶段/作业不应该以这种方式相互交互吗?

谢谢

标签: azure-devopsyamlazure-pipelines

解决方案


这是因为每个作业都在不同的代理上运行

一个阶段包含一个或多个作业。每个作业都在一个代理上运行。作业代表一组步骤的执行边界。所有步骤在同一个代理上一起运行。例如,您可以构建两种配置 - x86 和 x64。在这种情况下,您有一个构建阶段和两个作业。

这是因为 jon 是一组步骤的边界,源代码在它们之间不共享。因此,如果您需要将其保留为单独的作业和阶段,您应该在每个作业checkout步骤中重复

      - checkout: self
        clean: true

请阅读有关管道基础知识,它们将为您提供有关其工作原理的高级图片。

如果你想在工作之间分享一些神器,请看这里

如果您需要在阶段之间共享一些变量,我写了一篇关于的文章。


推荐阅读