You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从TeamCity直接获取分支与构建编号用于Go自动化测试?

从TeamCity动态获取构建编号与分支信息用于Go自动化测试

针对你遇到的硬编码构建编号无法关联分支的问题,这里提供两种适配Go自动化测试的解决方案,分别对应不同的运行场景:

方案一:直接读取TeamCity环境变量(推荐在Agent上运行)

当你的Go测试在TeamCity Agent环境中执行时,TeamCity会自动注入一系列环境变量,直接读取这些变量就能获取当前构建的编号和分支信息,无需额外API调用:

package your_test_package

import (
	"os"
	"testing"
)

func TestYourAutomatedLogic(t *testing.T) {
	// 从TeamCity注入的环境变量中提取关键信息
	buildNumber := os.Getenv("BUILD_NUMBER")
	branchName := os.Getenv("BUILD_BRANCH")

	// 校验信息完整性
	if buildNumber == "" || branchName == "" {
		t.Fatal("未获取到TeamCity构建信息,请确认测试在TeamCity Agent环境中运行,或手动设置BUILD_NUMBER、BUILD_BRANCH环境变量")
	}

	// 执行你的验证逻辑,使用buildNumber和branchName关联对应构建
	t.Logf("当前测试绑定构建:%s(分支:%s)", buildNumber, branchName)
	// 后续测试步骤...
}

常用的TeamCity环境变量还包括:

  • BUILD_ID:构建的唯一ID(区别于显示用的BUILD_NUMBER)
  • TEAMCITY_PROJECT_NAME:当前项目名称

方案二:通过TeamCity REST API获取(适合本地调试/跨构建查询)

如果需要在本地运行测试,或者要查询历史构建、其他分支的构建信息,可以调用TeamCity的REST API获取关联数据:

package your_test_package

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
	"os"
	"testing"
)

// 定义API返回的构建数据结构(按需裁剪字段)
type TeamCityBuild struct {
	Number     string `json:"number"`
	BranchName string `json:"branchName"`
	Status     string `json:"status"`
}

func fetchTeamCityBuild(projectID, branchName, tcURL, username, password string) (*TeamCityBuild, error) {
	// 构造查询URL:筛选指定项目、分支的最新成功构建
	escapedBranch := url.QueryEscape(branchName)
	apiEndpoint := fmt.Sprintf("%s/app/rest/builds?locator=project:%s,branch:%s,status:SUCCESS,count:1", tcURL, projectID, escapedBranch)

	req, err := http.NewRequest("GET", apiEndpoint, nil)
	if err != nil {
		return nil, err
	}

	// 设置Basic认证(如果TeamCity开启了权限控制)
	req.SetBasicAuth(username, password)
	req.Header.Set("Accept", "application/json")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("API请求失败,状态码:%d", resp.StatusCode)
	}

	// 解析API响应
	var respData struct {
		Builds []TeamCityBuild `json:"build"`
	}
	if err := json.NewDecoder(resp.Body).Decode(&respData); err != nil {
		return nil, err
	}

	if len(respData.Builds) == 0 {
		return nil, fmt.Errorf("未找到分支%s的成功构建记录", branchName)
	}

	return &respData.Builds[0], nil
}

func TestWithAPIFetch(t *testing.T) {
	// 从环境变量读取配置(避免硬编码敏感信息)
	tcURL := os.Getenv("TEAMCITY_URL")
	projectID := os.Getenv("TEAMCITY_PROJECT_ID")
	targetBranch := os.Getenv("TEST_TARGET_BRANCH")
	tcUser := os.Getenv("TEAMCITY_USER")
	tcPass := os.Getenv("TEAMCITY_PASSWORD")

	buildInfo, err := fetchTeamCityBuild(projectID, targetBranch, tcURL, tcUser, tcPass)
	if err != nil {
		t.Fatalf("获取构建信息失败:%v", err)
	}

	t.Logf("获取到目标分支的构建:%s(状态:%s)", buildInfo.Number, buildInfo.Status)
	// 执行验证逻辑...
}

最佳实践

  1. 所有敏感配置(如TeamCity地址、凭证)都通过环境变量传入,不要硬编码在代码中
  2. 在测试前加入信息校验,避免因缺失构建信息导致测试无意义地执行
  3. 若需要批量查询构建,可调整API的count参数或使用分页逻辑

内容的提问来源于stack exchange,提问作者Zubeen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 19:32:10