Azure 构建管道 - 获取源 - 干净 - `Sources` 和 `Sources directory` 有什么区别?

Azure build pipeline - Get sources - clean - what is the difference between `Sources` and `Sources directory`?

在 azure build pipeline 中,在 Get Sources 下假设 clean 设置为 true,那么您需要选择以下之一:

Sources
Sources and output directory
Sources directory
All build directories

以下是SourcesSources directory的区别。你能用实际例子给我解释一下吗?

Sources:构建管道执行撤消 $(Build.SourcesDirectory) 中的任何更改。更具体地说,在获取源之前执行以下 Git 命令。

git clean -ffdx
git reset --hard HEAD

Sources 目录:删除并重新创建 $(Build.SourcesDirectory)。这会导致为每个构建初始化一个新的本地 Git 存储库。

Azure build pipeline - Get sources - clean - what is the difference between Sources and Sources directory?

对于 Sources,正如 ducoment 所说:

The build pipeline performs an undo of any changes in $(Build.SourcesDirectory)

重要:此处提到的an undo of any changes指的是受源代码控制的更改。

我们可以从 git 命令得到这个 git clean -ffdx:

git-clean:

OPTIONS

-d

Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use -f option twice if you really want to remove such a directory.

因此,它将清除源代码管理中未跟踪的任何更改

例如,我直接在我的构建服务器机器中对 $(Build.SourcesDirectory) 进行了一些更改(不是回购)。 clean:source 选项将恢复此更改。

构建管道后,我修改了源目录中的一个测试 txt 文件,如:c:\agent_work\s:

我直接把目录里的1122改成了112233。接下来,我设置 clean:source,当我们执行管道时,azure devops 将删除上面的修改,它将恢复为 1122.

对于Sources 目录,就像文档解释的那样,azure devops 将删除并重新创建$(Build.SourcesDirectory)。它将删除文件夹 c:\agent_work\s,然后创建一个新文件夹,并在新创建的文件夹中检出源代码。


更新了更详细的示例:

说到清理源,我们需要知道这个选项是用于自托管代理而不是微软托管代理,清理自己的源目录$(Build.SourcesDirectory) -托管代理。那是因为每次使用 Microsoft 托管代理时都会获得一个新代理。查看官方文档中的note

现在,让我们比较一下SourcesSources directory的区别。

当我们使用自托管代理创建新管道时,Azure devops 将从 repo 检出源到自托管代理:

因此,来自 repo 的构建源将保存到我们自托管代理中的文件夹 $(Build.SourcesDirectory)

然后,我们打开那个文件夹,在这个文件夹中手动添加一个新的测试文件(不是从repo添加),比如Test.txt

如果我们 select clean:source,azure devops 将 删除我们手动添加的文件 Test.txt 但保留其他文件 :

接下来,我们测试clean:Sources directory选项。我们还在文件夹 $(Build.SourcesDirectory) 中添加相同的测试文件,然后 select 选项 Sources Directory:

所以,你应该清楚SourcesSources directory的区别,一个只清除我们添加的多余文件,一个是清除所有文件。当我们的repo很大时,我们不想花时间重新检查所有的repo,我们可以选择源。

这是最简单最直接的例子。当然,这个选项不仅对文件有用,对任何文件修改也有用。