.net - 如何在 Azure Pipeline 期间创建包含 build.* 变量的文件

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

我需要创建以下 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.SourcesDirectory),这是下载源代码文件的位置。请改用 $(Build.StagingDirectory),这是任何工件在推送到目的地之前都会复制到的位置。

引用:Build variables (DevOps Services)

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

关于.net - 如何在 Azure Pipeline 期间创建包含 build.* 变量的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66130994/

相关文章:

python - Azure Python SDK : Get Role Assignment Type (eligible vs active)

excel - PowerShell 重命名 Excel 工作表

string - 在大文件中高效地搜索字符串

windows - 如何从批处理文件运行 PowerShell 脚本

.net - 未处理的异常:System.Configuration.ConfigurationErrorsException:无法识别的元素“服务”。嵌套ConfigurationElementCollection

.net - 在 .NET 的方法中拥有但不抛出异常的成本

c# - 使用 SSL 的网络请求

c# - .NET 4.5 命名空间 'Standard'

azure - 重新部署 Azure WebJob

azure - 如何创建命令式 Azure 队列输入函数