为何VSTS私有代理执行Git Checkout?部署场景下能否取消该操作?
Great question—let’s break down what’s happening here and why that checkout step isn’t as redundant as it might seem at first glance.
What the "Get Sources" step actually does
In Azure DevOps (formerly VSTS), the built-in "Get Sources" step isn’t just a simple "pull files" operation—it’s a combination of git fetch + git checkout (plus extra cleanup for build consistency). This is intentional because builds need to run against a precise, consistent version of your code every time.
Why checkout is necessary (even for deployment-only scenarios)
You mentioned you only need to deploy files, but checkout serves two critical purposes here:
- It ensures your agent’s working directory is switched to the exact commit/branch triggered by the build. A
git fetchonly downloads the latest remote objects to the agent’s local Git repo, but doesn’t update the actual files in your working directory to match the version you want to deploy. - It cleans up leftover files from previous builds. Without checkout, your working directory might have stale files, untracked changes, or mismatched versions that could break your deployment’s consistency.
Can you modify or skip this behavior?
Unfortunately, there’s no built-in toggle in the "Get Sources" step to disable the checkout part—it’s baked into the step’s core logic. But if you really want to avoid full Git checkout for your deployment scenario, here are practical workarounds:
- Use a file download task instead: If your deployment files are stored in a blob storage or artifact repository, skip the "Get Sources" step entirely and use a task like "Download Files" to pull exactly what you need.
- Replace with a custom script: Disable the default "Get Sources" step, then add a PowerShell/Bash script that uses lighter Git commands. For example, you could use
git archiveto directly export the files from a remote branch without cloning the entire repo:git archive --remote=https://your-repo-url.git main:src/deployment-files | tar -x - Optimize Git caching on your agent: While you can’t skip checkout, you can configure your private agent to reuse the local Git repo cache (instead of cloning from scratch every time) to speed up the process significantly.
内容的提问来源于stack exchange,提问作者user1142433

