簡體   English   中英

CI/CD 管道 Azure devops 部署發布后自動合並

[英]CI/CD pipelines Azure devops automatic merge after deploy release

我有一個經典的環境。 設置如下:

我有 2 個分支: DevelopMaster

Azure DevOps 中是否有任何方法可以設置以下規則:

  1. 在開發環境中部署成功時(在 azure devops 的發布管道中定義) ------>自動創建pull request以將開發合並到Master中。

  2. 或另一個:如果開發分支Build成功------->自動創建pull request以將開發合並到 Master

任何幫助將不勝感激。

編輯:

我剛剛上傳了執行此操作的擴展程序: https : //marketplace.visualstudio.com/items?itemName=ShaykiAbramczyk.CreatePullRequest


您可以使用Azure DevOps Rest API創建請求請求,因此在Build / Release的最后添加執行此任務的PowerShell任務,例如:

$body =  @{
             sourceRefName= "$(Build.SourceBranch)"
             targetRefName = "refs/heads/master"
             title = "PR from Pipeline"
     }

$head = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"  }
$json = ConvertTo-Json $body
$url = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/git/repositories/$(Build.Repository.Name)/pullrequests?api-version=5.0"
Invoke-RestMethod -Uri $url -Method Post -Headers $head -Body $json -ContentType application/json

照片3

您需要允許腳本訪問OAuth令牌(選中“代理作業”選項中的復選框):

照片1

結果:

在此處輸入圖片說明

我將基本參數放在正文中(從分支到分支,標題),但是您可以添加更多參數,例如評論者,請在此處檢查文檔。

  1. 沒有內置任務,但是您可以使用oauth令牌或使用自己的auth對api發出請求,自己編寫腳本。
  2. 幾乎可以在此處使用相同的方法,也可以使用分支策略在合並請求到主服務器之前強制對拉取請求進行驗證(在我看來,這是更好的方法,因為在每次提交時從開發合並到主服務器都是毫無意義的)。

使用 python 和devops rest api人們已經提到你可以做這樣的事情。

# Tested in python 3.10
# pip install requests
import base64
import requests

# Fill the following variables with real values
personal_access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'  # https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&viewFallbackFrom=vsts&tabs=Windows
organization = "myorganization"
project_id = "00000000-0000-0000-0000-000000000000"
repository_id = "00000000-0000-0000-0000-000000000000"

authorization = str(base64.b64encode(bytes(f":{personal_access_token}", "ascii")), "ascii")
headers = {"Content-type": "application/json", "Authorization": f"Basic {authorization}"}

prs_url = f"https://dev.azure.com/{organization}/{project_id}/_apis/git/repositories/{repository_id}/pullrequests?api-version=5.1"

# create PR
response = requests.post(
    f"{prs_url}",
    headers=headers,
    data=json.dumps({
        "sourceRefName": "refs/heads/release",
        "targetRefName": "refs/heads/master",
        "title": "release to master",
    }),
)

暫無
暫無

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

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