使用来自 Bitbucket 的 webhook 触发 VSTS/TFS 构建

Trigger VSTS/TFS build with webhook from Bit Bucket

更新:这是一个老问题。 TFS 现在完全支持与 Bitbucket 的集成!

真的无法从外部使用 http 触发 TFS/VSTS 2015 中的构建吗?

我在 BitBucket 上有存储库,我想在提交时触发构建。 我在网上搜索了一下,什么也没找到。

是的,除非您使用像 Zapier 这样的第三方服务。

已经有用户为此提交了意见。查看此 link 了解详细信息:https://visualstudio.uservoice.com/forums/330519-team-services/suggestions/10674648-enable-ci-build-support-for-bitbucket-git-reposito

更新: 此功能现在在 VSTS 中可用。您可以选择 "BitBucket" 作为 "Srouce" 并在 "Triggers" 面板下启用触发器。


更新 - TFS 现在提供 bitbucket 集成


这是一个很老的线程,仍然 - tfs 文档有这个 link 遵循:https://www.visualstudio.com/docs/build/define/repository#external-git

看这张快照:

每当某个分支更新时触发构建:

在 link 访问您的存储库后,返回您的构建定义和 select Triggers 选项卡。

从这里您可以在存储库分支更新时触发您的 TFS 构建定义。

  1. 选中持续集成 (CI) 复选框
  2. 单击 + Add new Filter link 添加新触发器并将分支名称插入文本框。
  3. 在您要触发的其他分支上添加新过滤器(包括)或从 CI 进程中排除分支(select 在列表框中排除)

VSTS 构建定义中的触发器设置无法与外部存储库正常工作。基本上用户需要登录到 VSTS 帐户,然后触发器检查存储库。

您可以创建一个 Azure 函数并将其用作您的 BitBucket Webhook。然后在 Azure 函数中使用 VSTS REST API 触发构建。

下面是您可以粘贴到您的 Azure 函数中并设置

后的代码
  • 实例名称
  • 项目名称
  • 定义 ID
  • 分行名称
  • 个人访问令牌

它会按照您想要的方式工作 -> 在您将提交推送到存储库后,它会立即为指定的定义排队构建。

using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
  var definitionId = -1;

  dynamic data = await req.Content.ReadAsAsync<object>();
  var branch = data?.push?.changes[0]?.@new?.name;

  if(branch == "master") definitionId = 6; // TODO update the branch name and definition Id to match your settings
  else if(branch == "ci/website-staging") definitionId = 7; // TODO update the branch name and definition Id to match your settings

  if (definitionId >= 0) // Known branch
  {
      string accessToken = GetEnvironmentVariable("PersonalAccessToken"); // TODO add your personal token to your app settings or paste it here
      const string instance = "instance_name"; // TODO put the instance name
      const string project = "project_name"; // TODO put the project name
      const string version = "api-version=2.0";

      var url = $"https://{instance}.visualstudio.com/DefaultCollection/{project}/_apis/build/builds?{version}";
      var authorizationToken = Convert.ToBase64String(Encoding.ASCII.GetBytes($":{accessToken}"));
      var body = "{\"definition\" : {\"id\" : " + definitionId + "}}";

      return await PostAsync(url, body, authorizationToken);
  }

  return req.CreateResponse(HttpStatusCode.OK);
}

private static async Task<HttpResponseMessage> PostAsync(string url, string jsonBody, string authorizationToken = null)
{
  using (var client = new HttpClient())
  {
      if (!string.IsNullOrEmpty(authorizationToken))
          client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authorizationToken);
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
      client.BaseAddress = new Uri(url);
      var content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
      return await client.PostAsync("", content);
  }
}

private static string GetEnvironmentVariable(string name)
{
  return Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
}

我已经写了一篇详细的blog post,以备您需要更多信息时使用。

我最近遇到了这个问题,我推送到外部 Bitbucket 的任何内容都没有触发我的 VSTS 构建。

我在 git 属性 user.email & user.name 中找到了值本地 Repo 与外部 Bitbucket 不同。

要解决此问题,请转到您在 Bitbucket 中的 Repo。

  1. 打开 Repo 的设置
  2. 一般下select用户别名
  3. 为 Bitbucket 输入您的 name 和来自您本地的 user.email git 属性的电子邮件回购