简体   繁体   中英

CI/CD pipelines Azure devops automatic merge after deploy release

I have a classic env. setup like following:

I have 2 branches: Develop and Master .

Is there any way in Azure DevOps to setup the following rule:

  1. When a deploy is succeeded on dev environment (defined in the release pipeline of azure devops) ------> create automatically a pull request to merge develop into Master .

  2. or the other one: if a Build of develop branch is succeded -------> create automatically a pull request to merge develop into Master .

Any help will be appreciated.

Edit:

I just uploaded an extension that does it: https://marketplace.visualstudio.com/items?itemName=ShaykiAbramczyk.CreatePullRequest


You can use Azure DevOps Rest API to create a Pull Request, so in the end of the Build / Release add a PowerShell task that do it, for example:

$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

You need to Allow scripts to access the OAuth token (check the checbox in the Agent Job options):

照片1

Results:

在此处输入图片说明

I put the basic parameters in the body (from branch, to branch, title) but you can add more parameters like reviewers, check the docs here .

  1. there is no build-in tasks for that, but you can script this yourself using the oauth token, or using your own auth to issue a request against the api.
  2. pretty much the same approach can be used here, or you can use Branch policies to force pull requests to be validated before merging them to master (which in my mind is better, because merging from develop to master on every commit is pretty pointless).

Using python and the devops rest api people already mentioned you could do something like this.

# 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",
    }),
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM