2
votes

In azure build pipeline, under Get Sources suppose clean is set to true, then you need to choose one of the following:

Sources
Sources and output directory
Sources directory
All build directories

Following is the difference between Sources and Sources directory. Can you explain this to me with practical example?

Sources: The build pipeline performs an undo of any changes in $(Build.SourcesDirectory). More specifically, the following Git commands are executed prior to fetching the source.

git clean -ffdx
git reset --hard HEAD

Sources directory: Deletes and recreates $(Build.SourcesDirectory). This results in initializing a new, local Git repository for every build.

1

1 Answers

3
votes

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

For the Sources, just like the ducoment said:

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

Important: an undo of any changes mentioned here refers to changes that are NOT under source control.

We could get this from the git command 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.

So, it will clean any change untracked by source control in the source.

For example, I have make some changes in the $(Build.SourcesDirectory) in my build server machine directly (Not the repo). The clean:source option will revert this change.

After build the pipeline, I modified one test txt file in the sources directory like: c:\agent_work\1\s:

enter image description here

I change the 1122 to 112233 in the directory directly. Next, I set the clean:source, when we execute the pipeline, azure devops will remove above modification, it will be revert to 1122.

For the Sources directory, just like the document explained, azure devops will delete and recreates $(Build.SourcesDirectory). It will delete the folder c:\agent_work\1\s, then create a new one, and checkout the source in the new created folder.


Updated with more detailed example:

To talk about the clean source, we need to know this option is used for self-hosted agents not the Microsoft-hosted agent, to clean the source directory $(Build.SourcesDirectory) in the self-hosted agent. That because you'll get a new agent every time when you use Microsoft-hosted agent. Check the note in the official documents.

Now, let us compare their differences between Sources and Sources directory.

When we create a new pipeline with self-hosted agents, Azure devops will checkout the source from repo to the self-hosted agent:

enter image description here

So, the build source from repo will be save to folder $(Build.SourcesDirectory) in our self-hosted agent:

enter image description here

Then, we open that folder, and add a new test file in this folder manually (Not add it from repo), like Test.txt.

If we select the clean:source, the azure devops will remove the file Test.txt we added manually but keep other files:

enter image description here

Next, we test the clean:Sources directory option. We also add the same test file in the folder $(Build.SourcesDirectory), then select the option Sources Directory:

enter image description here

So, you should clearly know the difference between the Sources and Sources directory, one only clears the extra files we added, and the other is to clear all the files. When our repo is very large, we don't want to spend time rechecking out all the repo, we can choose source.

This is the simplest and most direct example. Of course, this option is not only useful for files, but also for any file modification.