前言
Azure DevOps 的 Wiki 頁面通常紀錄著許多專案相關的重要資訊,這些資訊可能包含環境設定、系統操作流程、開發程序與系統版本資訊...等。在某些情境下,團隊會想要在 CI /CD 流程中,自動的建立或編輯 Wiki 資訊,以減少手動更新資料需求,如 Release Note。 在 Azure DevOps,您可以透過 Services REST API 對於專案內的 wiki 頁面進行新增、修改、刪除操作,並將撰寫好的 scripts 放入 Build/Deploy pipeline 內,輕鬆地達成這個需求。本篇文章若有錯誤或任何建議,請各位先進不吝指教。![]() |
Azure DevOps Wiki Page |
介紹
設定與取得 Personal Access Token (PAT)Step 1. 首先,我們先登入 Azure DevOps,點選右上角圖像 Security
Step 2. 點選 New Token
Step 3. 依序輸入名稱、過期時間...等資訊 選擇 Custom Defined,找到 Wiki,勾選 Read & Write → 點選 Create
Tip: 您可以點選 Show all scopes 顯示所有 Scope
Step 4. 紀錄下 Token,使用 Services REST API 時會需要它 (若離開這個頁面,您又忘記Token,就只能建立新的 Token 了)
Azure DevOps Services REST API - Wiki 說明
透過 Services REST API 建立 Wiki 頁面的 url 如下,方法為 PUT https://dev.azure.com/{organization}/{project}/_apis/wiki/wikis/{wikiIdentifier}/pages?path={path}&api-version=5.0
參數描述如下:
名稱 | 位置 | 必要 | 類型 | 描述 |
---|---|---|---|---|
organization | path | True | string | 您的組織名稱 |
project | path | True | string | Project ID 或名稱 |
wikiIdentifier | path | True | string | Wiki ID 或名稱 |
path | query | True | string | Wiki 頁面路徑 |
api-version | query | True | string | API 版本 |
您能在 wiki 頁面上找到 organization、project ID 與 wikiIdentifier。如下圖所示,organization 為 duranhsieh;project ID 為 durantest;wikiIdentifier 為 durantest.wiki
Tip: Wiki 頁面有主頁面與子頁面,若要新增子頁面,路徑 (path) 表示方式為 主頁面/子頁面 (MasterPage/SubPage)
標頭 (Header)
名稱 | 必要 | 類型 | 描述 |
---|---|---|---|
authorization | True | string | Basic your_token |
主體 (Body)
名稱 | 必要 | 類型 | 描述 |
---|---|---|---|
content | True | string | wiki 頁面內文 |
測試與應用
了解上列參數後,我們可以開始呼叫 Services REST API 來編輯 Wiki 頁面,下面提供 Postman、C#、curl 與 powershell 方式,提供參考:
Postman
C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using System; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Text; | |
namespace ConsoleApp1 | |
{ | |
class Program | |
{ | |
public static string personalAccessToken = "your_personal_token"; | |
public static string path = "/Test"; | |
public class request | |
{ | |
public string content { get; set; } | |
} | |
static void Main(string[] args) | |
{ | |
using (var client = new HttpClient()) | |
{ | |
request req = new request { content = "New content for page" }; | |
var jsonString = JsonConvert.SerializeObject(req); | |
var httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json"); | |
client.BaseAddress = new Uri("https://dev.azure.com"); | |
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", | |
Convert.ToBase64String( | |
System.Text.ASCIIEncoding.ASCII.GetBytes( | |
string.Format("{0}:{1}", "", personalAccessToken)))); | |
var response = client.PutAsync("/duranhsieh/Bupa Demo/_apis/wiki/wikis/TestDemo.wiki/pages?path=" + path + "&api-version=5.0", httpContent).Result; | |
if (response.IsSuccessStatusCode) | |
{ | |
Console.WriteLine("Success"); | |
} | |
else | |
{ | |
Console.WriteLine("Error"); | |
} | |
} | |
} | |
} | |
} |
curl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -X PUT \ | |
'https://dev.azure.coms/TestDemo.wiki/pages?path=BlogTest&api-version=5.0' \ | |
-H 'authorization: basic your_personal_token' \ | |
-H 'cache-control: no-cache' \ | |
-H 'content-type: application/json' \ | |
-d '{ | |
"content":"Test" | |
}' |
Powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" | |
$headers.Add("authorization", 'basic your_personal_access_token') | |
$headers.Add("Content-Type", 'application/json') | |
$body = "{`n `"content`":`"Test`"`n}" | |
$response = Invoke-RestMethod 'https://dev.azure.coms/TestDemo.wiki/pages?path=Test&api-version=5.0' -Method 'PUT' -Headers $headers -Body $body | |
$response | ConvertTo-Json |
執行後,您可以到 wiki page 看見執行結果:
0 留言