gitazure-devops

create a PR request in azure build pipeline


Say we have following branches:

  1. Master
  2. Sprint
  3. Stories

Stories can be merged to sprint (for sprint items) or Master (for hotfix) and we want is this:

  1. Sprint branch to be deployed to PreProd Environment
  2. If QA is happy, sprint branch will be merged to master and then deployed to UAT, then QA.

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?


Solution

  • And if it is ok, how can I create and finish a PR request in build pipeline?

    Solution 1

    The easiest way would be to use Create Pull Request Task with "Set Auto Complete" option checked.

    Solution 2

    If you want to do it from CLI, generate PAT Token:

    enter image description here

    Save it as secret variable:

    enter image description here

    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.