This question is similar to the following question: How to do a Git pull request on remote branches via the command line
I have a pipeline that generates a branch with a new document that needs to be integrated into the remote repository to the master branch. This needs to be done through a pull request, I have tried with the git command git request-pull
to no avail as I don't see the pr in the could. This is what I currently have (Which directly merges the new branch to master without the pr).
- script: |
cd documentation
git config --global user.email "myemail@someOrganization.com" && git config --global user.name "John Doe"
git checkout -b release-notes
dir
mv $(System.DefaultWorkingDirectory)\\..\\tempdocs.md $(System.DefaultWorkingDirectory)\\..\\"$(SourceBranch)".md
mv $(System.DefaultWorkingDirectory)\\..\\"$(SourceBranch)".md docs\\applications\\app\\versions\\"$(SourceBranch)".md
git add .
git commit -m "add release notes for $(SourceBranch)"
git checkout feature/automatic-docs && git merge release-notes && git push origin
displayName: 'Publish Documentation'
My colleague Sander has created an extension which does all the hard work of creating the PR .
If you want to see how it's done, check out the source to the extension .
Another option s to use the az repos pr create
command in the Azure CLI with the DevOps extension . You'll need to enable script access to the pipeline's OAuth token to authenticate I presume.
You can use Azure devops services REST API to create a pull request .
POST https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests?api-version=5.1
So you just need to run your git commands in the powershell task and add a few more codes to call above api. Please check below example:
- powershell: |
git config --global user.email "@email.com"
git config --global user.name "name"
git checkout -b new_branch -q
git ....
$PAT= "Personal access token"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
$url = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/git/repositories/$(Build.Repository.ID)/pullrequests?api-version=5.1"
$body = '{
"sourceRefName": "refs/heads/sourcebranch",
"targetRefName": "refs/heads/targetbranch",
"title": "A new feature",
"description": "Adding a new feature",
}'
Invoke-RestMethod -Uri $url -Method post -Header @{Authorization = "Basic $base64AuthInfo"} -ContentType "application/json" -Body $body
In the end of above powershell scripts, pull request api is called to create PR in the cloud. To get a Personal access token, check this document .
If you like simplicity and are a fan of command line, you can use Azure CLI. @jessehouwing mentioned this in his answer but I will add some more details.
Pre-requisite: The build service user must have 'PullRequestContribute' access to the repository. Then you can add a powershell task like below.
- task: PowerShell@2
inputs:
targetType: inline
Script: 'az repos pr create -r <YOUR_REPO> -s <SOURCE_BRANCH> -t <TARGET_BRANCH> --title "Auto PR: XXX" --delete-source-branch true -d "Created by $(System.CollectionUri)$(System.TeamProject)/_build/results?buildId=$(Build.BuildID)" --merge-commit-message "Integration [skip ci]" --org $(System.CollectionUri) -p $(System.TeamProject)'
displayName: Create PR
name: CreatePR
env:
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
Since AZ CLI runs in any OS, you do not need to restrict yourself to powershell.
Authentication - https://docs.microsoft.com/en-us/azure/devops/cli/log-in-via-pat?view=azure-devops&tabs=windows
Azure Devops CLI - https://docs.microsoft.com/en-us/cli/azure/repos/pr?view=azure-cli-latest#az_repos_pr_create
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.