【问题标题】:How to create a file in during Azure Pipeline that includes build.* variables如何在 Azure Pipeline 期间创建包含 build.* 变量的文件
【发布时间】:2021-05-13 19:01:17
【问题描述】:

我需要创建以下 json 结构并使用 Azure Pipelines 将其包含在我的 .NET 应用程序中,但我不清楚如何执行此操作。

{
"buildNumber":"$(Build.BuildNumber)",
"buildId":"$(Build.BuildId)",
"branchName":"$(Build.SourceBranchName)",
"commitHash":"$(Build.SourceVersion)"
}

我尝试过简单地使用命令行将信息回显到文件中,但我不确定发布步骤获取该信息所需的路径。

现在我正在尝试一个 powershell 任务:

    - task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      # Write your PowerShell commands here.

      new-item -itemtype file -path $(Build.SourcesDirectory)/MyFirstExperiment -name "buildinfo.json" -force -value '{
      \"buildNumber\":\"$(Build.BuildNumber)\",
      \"buildId\":\"$(Build.BuildId)\",
      \"branchName\":\"$(Build.SourceBranchName)\",
      \"commitHash\":\"$(Build.SourceVersion)\"
      }'

我在创建文件的任务期间看到了输出,但我仍然必须遗漏一些明显的东西,因为它没有出现在管道稍后的构建/发布中。

我应该检查什么?

我强烈怀疑我遗漏了一些非常明显的东西,因为在谷歌搜索“azure pipeline create file”时没有很多结果

提前谢谢你!

【问题讨论】:

  • 要获得$(Build.SourceVersion)短git commit hash,请查看this post。它将短哈希(如提交历史中所示)保存到变量$(short_hash),您可以将其包含到您的文件中。

标签: .net azure powershell azure-devops azure-pipelines


【解决方案1】:

我看到您正在使用$(Build.SourcesDirectory),这是下载源代码文件的位置。请改用$(Build.StagingDirectory),这是任何工件在被推送到其目的地之前被复制到的位置。

参考:Build variables (DevOps Services)

如果您的组织允许,您还可以考虑使用 Visual Studio Marketplace 中的扩展,例如 File Creator。这是一项任务,可以作为一个步骤添加到您的管道中,以在构建或发布过程中创建文件。

【讨论】:

  • 我修复了这个问题(谢谢!),现在我在末尾显示了文件以及发布输出的 zip。有什么方法可以将文件放入 zip 中?我原以为如果我在构建步骤之前添加它,它会被复制,但似乎并非如此。
  • 乐于助人!实际上,您可以 copy the file 到目的地,然后将 publish 作为构建工件的一部分。
  • MSBuild 是一个复杂的野兽。如果您可以将文件作为构建过程的一部分生成,则可以将其添加到您的输出中(通过创建项目<Content Include="Path\to\file" CopyToOutputDirectory='PreserveNewest' />,甚至使用<ContentWithTargetPath Include="..." CopyToOutputDirecory="PreserveNewest" CopyToPublishDirectory="PreserveNewest" TargetPath="some\other\path\in\bin\Release" />。查看Microsoft.Common.CurrentVersion.targets。有一个很多东西要学!
猜你喜欢
  • 2019-09-10
  • 2020-10-30
  • 2021-08-28
  • 1970-01-01
  • 2021-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多