azure-cli-extensions icon indicating copy to clipboard operation
azure-cli-extensions copied to clipboard

How do I run a specific stage in my Release pipeline using CLI?

Open prasad-sunkara opened this issue 4 years ago • 22 comments

[Enter feedback here] It seems az pipelines run doesn't work with any of the Release Pipelines. We use specific stages in our Release Pipeline to run automated tests and deploy code to various test environments. This functionality is needed to invoke specific stages from CLI.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

prasad-sunkara avatar May 17 '20 17:05 prasad-sunkara

pipelines

yonzhan avatar May 17 '20 23:05 yonzhan

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @narula0781, @ashishonce, @romil07.

ghost avatar May 26 '20 18:05 ghost

Hello. Is there an update on this functionality?

ASChatmon avatar Aug 12 '20 16:08 ASChatmon

devops service team should take a look

yonzhan avatar Aug 12 '20 22:08 yonzhan

@yonzhan What is happening here? It is quite important for us to be able to select the stages to be run in our pipelines.

adamnybergelli avatar Dec 25 '20 20:12 adamnybergelli

@adamnybergelli I mean az pipelines CLI commands are owned by devops service team. They should look into this issue.

yonzhan avatar Dec 26 '20 00:12 yonzhan

@yonzhan thanks, and how to I reach that team?

adamnybergelli avatar Dec 29 '20 15:12 adamnybergelli

There is msftbot commented above to route to the appropriate team.

yonzhan avatar Dec 30 '20 00:12 yonzhan

any update on this? @narula0781, @ashishonce, @romil07

adamnybergelli avatar Jan 05 '21 18:01 adamnybergelli

The following is a workaround to deploy a single stage, using the Azure Devops CLI and the Azure Devops REST API NOTE: This has only been tested using the new YAML pipelines.

  • First, get the ID for the pipeline you want to automate:

az pipelines list --query "[].{ID: id, Name:name}" --output tsv

  • Once you have the pipeline's ID, get the list of builds for the pipeline to get the build ID of the build you want to run:

az pipelines build list --definition-ids <Pipeline ID> --query "[].{Name:definition.name,Uri:uri}" --output tsv

The build list from the previous command will have a Uri column with the following signature: vstfs:///Build/Build/<Build ID>

  • Call the Devops REST API to retry a stage: NB: Documentation for the API call: https://docs.microsoft.com/en-us/rest/api/azure/devops/build/stages/update?view=azure-devops-rest-6.0

NB: Used this tutorial to get the resources needed to call the API through Powershell (Thank you, Olivier Miossec! @omiossec): https://dev.to/omiossec/getting-started-with-azure-devops-api-with-powershell-59nn

NB: I used the Developer Tools in Chrome to view the request when clicking the "ReRun" button on a build. The URL in the Network tab for the call gave me the ProjectGuid and BuildStage:

Full Script:

$BuildId = <Build ID>

$AzureDevOpsPAT = "<Personal access token from tutorial above>"
$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($AzureDevOpsPAT)"))}

$OrganizationName = "<Organization Name>"
$ProjectGuid = "<Project Guid. You may be able to use the ProjectName as well>"
$BuildStage = "<Build Stage>"

$requestBody = @{
state="retry"
}

$requestBodyJson = $requestBody | ConvertTo-Json

$uri = "https://dev.azure.com/$($OrganizationName)/$($ProjectGuid)/_apis/build/builds/$($BuildId)/stages/$($BuildStage)?api-version=6.0-preview.1"

Try
{
	Invoke-RestMethod -Uri $uri -Method patch -body $requestBodyJson -Headers $AzureDevOpsAuthenicationHeader -ContentType 'application/json'    
} Catch {
	Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__ 
	Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
}

Once you issue the command, you should be able to see the stage triggered in Azure Devops.

aucinc123 avatar Jan 28 '21 18:01 aucinc123

Despite of having a "hacky" workaround thanks to @aucinc123, I think this feature should be included in az pipelines release.

guillermosaez avatar Dec 02 '21 16:12 guillermosaez

Bump for it being VERY useful to be able to simply do the following in the CLI (or a Powershell equivalent):

az pipelines release deploy --definition-name --stage --release

For those of us whose corporate policy disallows CD, with all deployments having to go through gates, etc, our releases are all "Manual Only" rather than "On Build". I have CLI scripts that run pipelines, that create releases... but I still have to go back to the DevOps website to actually deploy the stage in question, after the release is created.

ADBjester avatar Jun 15 '22 13:06 ADBjester

...wanted to add a few comments onto last few posts...

Def agree the ability to use cli to run specific stages would be VERY helpful... esp with 'multi-stage' .yaml pipelines that have dependencies (or not for that matter). Additionally, it would be nice to be able to select to run ANY particular stage - not JUST a deployment stage, AND ALSO - take into account CD approvals/checks (associated with environments) - some way to approve from command line IF you have permissions...

edwin-traffk avatar Jun 19 '22 16:06 edwin-traffk

Is this still being investigated? I see a huge need for this.

JDoornink avatar Aug 10 '22 22:08 JDoornink

Agreed still a huge need for this. Any updates?

myehya-up avatar Oct 18 '22 01:10 myehya-up

Bump for it being VERY useful to be able to simply do the following in the CLI (or a Powershell equivalent):

az pipelines release deploy --definition-name --stage --release

For those of us whose corporate policy disallows CD, with all deployments having to go through gates, etc, our releases are all "Manual Only" rather than "On Build". I have CLI scripts that run pipelines, that create releases... but I still have to go back to the DevOps website to actually deploy the stage in question, after the release is created.

And most of the time these exact corporates get attracted to Microsoft software like flies to shit. And you go through the rabbit hole of Microsoft to finally end up here knowing it is not possible. These corporates also hire excellent employees that create 20 "microservices" that needs to be deployed with manual approval process. Let me get back to happy clicking in the evening to cover up for the time I "wasted" trying to automate this. Microsoft just rocks Note1: My gratitude to the person who created this issue report and ending my chase to find the command in the Microsoft "documentation". Thanks for making me not waste more time trying to find the missing documentation. Note2: I already wasted a lot of time before this trying to make the azure devops python sdk to work for this. That is a total waste of time with no documentation at all. Microsoft is just marvelous

sijucm avatar Nov 02 '22 14:11 sijucm

yes, please

janPORG avatar Nov 18 '22 13:11 janPORG

still waiting)

yura-kutsiy avatar Nov 23 '22 18:11 yura-kutsiy

I found a workaround which is not painfull.

In your pipeline add paratmeter

parameters:
  - name: Build1
    displayName: Build1?
    type: boolean
    default: true

And for stage add condition

stages:
- stage: 'Build1'
  displayName: 'Build1' #Name displayed when viewing in Azure DevOps
  condition: ${{ parameters.Build1 }}
  dependsOn:

CLI supports parameters for pipeline execution. So you can pass this and it works in similar way as with stages selected "normal way".

maciej-egermeier avatar Dec 28 '22 09:12 maciej-egermeier

Is there any update? I am waiting ......

MofaggolHoshen avatar Feb 09 '23 08:02 MofaggolHoshen

I am looking for az cli pipeline+stage specific triggering as well, specifically being able to execute the cli commands from another pipeline, leveraging the calling account ADO permissions so that we can avoid PAT token generation and management.

bryanhunwardsen avatar Oct 20 '23 17:10 bryanhunwardsen

If you don't want to increase number of parameters you can use variables defined on pipeline level configuration (Edit pipeline -> Variables)

Then you can set defaults for these each stage variables, like:

image

When you add new variable enable overwriting it:

image

In pipeline YAML file condition in stage will look like:

### Dev environment ###
- stage: dev_deployment
  displayName: Dev Deployment
  condition: eq(variables['stage_dev'], 'true')
  ...

### QA environment ###
- stage: qa_deployment
  displayName: QA Deployment
  condition: eq(variables['stage_qa'], 'true')
  ...

Running Dev stage only (Default stage_dev and stage_qa variables):

az pipelines run --organization $ORG --project $PROJECT_NAME --name $PIPELINE_NAME --branch $BRANCH --parameters deploy=true version=1.2.3

Running Dev and QA stages:

az pipelines run --organization $ORG --project $PROJECT_NAME --name $PIPELINE_NAME --branch $BRANCH --parameters deploy=true version=1.2.3 --variables stage_dev=true stage_qa=true

dardyjak avatar Jan 09 '24 17:01 dardyjak