Reputation: 4074
Say we have following branches:
Stories can be merged to sprint (for sprint items) or Master (for hotfix) and we want is this:
We wants to protect master or sprint branch, so they can only be changed via PR request.
Other than QA manually create a PR and then merge to master branch, I would like to do it in a build task. So I tried to use Azure CLI task to run a batch:
az repos pr create --auto-complete true --bypass-policy true --bypass-policy-reason "CI build" --repository JerryTestCI --source-branch R_Current_Sprint --target-branch master
This gives me an error: Before you can run Azure DevOps commands, you need to run the login command(az login if using AAD/MSA identity else az devops login if using PAT token) to setup credentials. Please see https://aka.ms/azure-devops-cli-auth for more information.
However, as my script runs in a build task, how can I login? I tried this, but my build will just hang on this command.
az devops login --organization https://XXX.visualstudio.com/
So is my idea the right way to do work? And if it is ok, how can I create and finish a PR request in build pipeline?
Upvotes: 5
Views: 13491
Reputation: 149
To add to the above answers - there's no need to create a PAT if your pipeline build agent has sufficient permissions.
- checkout: self
persistCredentials: true
- bash: |
git config --global user.email "[email protected]"
git config --global user.name "Azure DevOps Pipeline"
name: GitConfig
- pwsh: |
git checkout -b MyBranch main
New-Item test.txt //add something new
git add test.txt
git commit -m "Test commit"
git push origin "HEAD:MyBranch"
az repos pr create <INSERT ARGS HERE>
env:
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
The code required comes from the following sources
Upvotes: 2
Reputation: 1235
You can log in with the az devops cli via something like this:
echo $PersonalAccessToken | az devops login
az devops configure --defaults organization=https://dev.azure.com/$OrganizationName project=$ProjectName
https://learn.microsoft.com/en-us/azure/devops/cli/log-in-via-pat?view=azure-devops&tabs=windows
But then yeah, this should work just fine.
Upvotes: 1
Reputation: 1175
And if it is ok, how can I create and finish a PR request in build pipeline?
The easiest way would be to use Create Pull Request Task with "Set Auto Complete" option checked.
If you want to do it from CLI, generate PAT Token:
Save it as secret variable:
An use powershell to save it as env variable
$env:AZURE_DEVOPS_EXT_PAT = '$(token)'
And then use any az devops command, it should be authenticated.
Upvotes: 4
Reputation: 41545
Another solution is to install the Create Pull Request extension, it automatically creates a Pull Request for Azure DevOps or GitHub repository from Build or Release pipeline, supports also multi-target branch.
Disclaimer: I'm the author.
Upvotes: 3