如何在TFS 18构建任务中按预定义变量选择并检出Git自定义分支
回答:TFS 2018构建任务中自定义分支检出与动态分支选择
Hey there! I’ve worked with TFS 2018 quite a bit, so let’s walk through your two questions clearly—both are straightforward once you know where to look. Here’s how to tackle each one:
1. 在TFS 2018构建任务中检出自定义源代码分支
This is the basic setup for targeting a specific branch, and it’s done right in the core "Get Sources" step:
- Step 1: Open your existing build definition, or create a new one. Navigate to the Get Sources tab (this is the first tab by default, where TFS configures how it pulls code).
- Step 2: Under the Repository section, confirm your Git repo is selected. Look for the Branch dropdown—by default, it might be set to
mainormaster. Click the dropdown and either:- Select your custom branch directly from the list (if it shows up), or
- Manually type the full branch name (e.g.,
feature/user-authentication) if it’s not listed.
- Step 3: Save your build definition. Next time you run it, TFS will pull code from your chosen custom branch.
对应截图说明

2. 依据CI流水线启动前的变量值动态选择Git分支
To make the branch selection dynamic based on a variable you set right before starting the pipeline, follow these steps:
Step 1: Create a custom pipeline variable
- Go to the Variables tab in your build definition.
- Click the Add button to create a new variable. Name it something clear like
TargetBranch, set a default value (e.g.,mainfor fallback), and check the box that says "Allow users to override this value when running a pipeline" (this lets you change the branch every time you start the build). - Save the variable.
Step 2: Link the variable to the Get Sources branch
- Go back to the Get Sources tab.
- Replace the static branch name in the Branch field with the variable reference:
$(TargetBranch). TFS will automatically resolve this variable to its value when the pipeline runs.
Step 3: Run the pipeline with a custom branch value
- When you click Queue build to start the pipeline, you’ll see a parameters page. Look for your
TargetBranchvariable, type in the branch name you want to use (e.g.,bugfix/login-issue), and click Queue. - The pipeline will now pull code from the branch you specified in the variable.
对应截图说明
- )
- 
小提示
- Make sure the branch name you enter matches exactly what’s in your Git repo—typos will cause the build to fail at the Get Sources step.
- Ensure the TFS build service account has read access to the branch you’re targeting.
- If you’re using branch naming conventions (e.g.,
feature/*), you can even use wildcards in the variable, but keep it simple unless you need that flexibility.
内容的提问来源于stack exchange,提问作者Yash Ghia
相关产品推荐
相关产品推荐

