在 Azure DevOps Pipelines 中部署构建时,$(Build.ArtifactStagingDirectory) 变量的值发生变化

The $(Build.ArtifactStagingDirectory) variable's value changes when deploying a build in Azure DevOps Pipelines

我有一个失败的 DACPAC 部署任务,因为由于某种原因 $(Build.ArtifactStagingDirectory) 管道变量的值在构建管道和发布管道之间发生变化。在构建管道中,变量设置为 C:\agent\_work\a,但在发布管道中它是 C:\agent\_work\r2\a。这导致发布管道在尝试部署 DACPAC 工件时失败,因为它正在查找的文件夹是空的;工件实际所在的文件夹将被忽略。如何使这些变量在构建和发布管道之间保持一致,以便从生成它的同一文件夹中检索工件?这些变量似乎是内置的,所以我看不出有什么方法可以改变它们。我总是可以对路径进行硬编码,但这似乎有点笨拙...

releases 你有 System.ArtifactsDirectory

The directory to which artifacts are downloaded during deployment of a release. The directory is cleared before every deployment if it requires artifacts to be downloaded to the agent. Same as Agent.ReleaseDirectory and System.DefaultWorkingDirectory.

Example: C:\agent_work\r1\a

并在 pipelines/builds Build.ArtifactStagingDirectory

The local path on the agent where any artifacts are copied to before being pushed to their destination. For example: c:\agent_work\a

A typical way to use this folder is to publish your build artifacts with the Copy files and Publish build artifacts tasks.

Note: Build.ArtifactStagingDirectory and Build.StagingDirectory are interchangeable. This directory is purged before each new build, so you don't have to clean it up yourself.

See Artifacts in Azure Pipelines.

This variable is agent-scoped, and can be used as an environment variable in a script and as a parameter in a build task, but not as part of the build number or as a version control tag.

这符合你的经验。你不能改变它,因为它们是预定义的。但是你能解释一下为什么这对你来说是个问题吗?

在release pipeline中,你不能直接访问build pipeline中的文件,不仅因为工作目录不同,而且他们使用的不是同一个agent。您需要先下载工件,然后在发布管道中使用它们。

您可以使用以下方式下载神器:

  1. 使用Download Build Artifacts任务。

  2. 进入edit release pipeline页面-> Select Add artifact -> Select Build -> 填写build pipeline相关信息(注意值源别名)-> 添加。您会发现您的工件已下载到 $(System.ArtifactsDirectory)/${Source alias}

有关在发布管道中使用工件的更多信息,您可以单击 this document

哦,我想我明白了。每个发布管道阶段都有一个名为“工件下载”的选项,它允许您指定该阶段实际使用管道中所有链接的工件中的哪些工件。我需要选中相应的复选框才能在阶段的任务中使用工件。