簡體   English   中英

使用Bit Bucket中的webhook觸發VSTS / TFS構建

[英]Trigger VSTS/TFS build with webhook from Bit Bucket

更新:這是一個老問題。 TFS現在完全支持與Bitbucket的集成!

是否真的不可能通過http從外部觸發TFS / VSTS 2015中的構建?

我在BitBucket上有存儲庫,我想在提交時觸發構建。 我在網上搜索過,一無所獲。

是的,除非您使用像Zapier這樣的第三方服務。

已經為此提交了用戶語音。 查看此鏈接了解詳情: https//visualstudio.uservoice.com/forums/330519-team-services/suggestions/10674648-enable-ci-build-support-for-bitbucket-git-reposito

更新:此功能現在可在VSTS中使用。 您可以選擇“BitBucket”作為“Srouce”並在“Triggers”面板下啟用觸發器。

VSTS構建定義中的觸發器設置無法與外部存儲庫一起正常工作。 基本上用戶需要登錄VSTS帳戶,然后觸發器檢查存儲庫。

您可以創建Azure功能並將其用作BitBucket Webhook。 然后在Azure Function中使用VSTS REST API來觸發構建。

以下是您可以在Azure功能中設置並在設置后粘貼的代碼

  • 實例名稱
  • 項目名
  • 定義ID(s)
  • 分支名稱
  • 個人訪問令牌

它將以您想要的方式工作 - >在您將提交推送到存儲庫之后,它將為指定的定義排隊構建。

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);
}

我已經寫了一篇關於它的詳細博客文章 ,以防您需要更多信息。


更新 - TFS現在提供bitbucket集成


這是相當古老的線程,仍然 - tfs文檔有以下鏈接: https//www.visualstudio.com/docs/build/define/repository#external-git

看到這個快照: 在此輸入圖像描述

在更新某個分支時觸發構建:

鏈接到存儲庫后,返回到構建定義,然后選擇“ 觸發器”選項卡。

從這里,您可以在更新存儲庫分支時觸發TFS構建定義。

  1. 選中持續集成(CI)復選框
  2. 單擊+ Add new Filter鏈接以添加新觸發器並將分支名稱插入文本框。
  3. 在您的其他分支上添加新過濾器以觸發(包括)或從CI過程中排除分支(選擇在列表框中排除) 在此輸入圖像描述

我最近遇到過這個問題,我推送到外部Bitbucket的任何東西都沒有觸發我的VSTS版本。

我發現我本地回購中的git屬性user.emailuser.name中的值與外部Bitbucket不同。

要解決這個問題,請訪問Bitbucket的Repo。

  1. 打開回購設置
  2. 常規下選擇用戶名別名
  3. 輸入Bitbucket的名稱和來自本地回購的user.email git屬性的電子郵件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM