Search code examples
gitazure-devopsazure-pipelines

Azure Devops pipeline with optional, manually triggered stage


I am using Azure DevOps YAML pipelines and have two stages - one is deploying to dev (is triggered by commit in master), and the second is checking pull requests (running tests and static analysis). These stages are executed based on variables, e.g. variables['Build.Reason'] or variables['Build.SourceBranchName'].

The pull request checks are set as required in branch policy, so the pipeline must pass before the author can merge it.

What I want is to allow the author to manually trigger deploy to dev from pull request (e.g. because there are changes in infrastructure, environment variables, etc.) to make sure everything works before the changes are merged upstream.

Example 1: I am changing just one function. I want the pipeline to run the static analysis and tests successfully and then merge the PR.

Example 2: I am integrating a new external service (e.g. emailing) and I want to make sure everything works. So the pipeline will trigger static analysis and tests and finish successfully (and would allow me to merge if I wanted to). However, because it is a bigger change with external integrations, at this point I want to trigger deploy to dev and check if everything works. This is still done from PR, not from the master, because I want to fix issues if they occur before the change is upstream.

In both cases (Example1 as well as Example2), after mege to master changes will be deployed to dev.

What I tried so far

  • Using ManualValidation@0 task: not applicable, because then the pipeline is not successful. As the pipeline is required to pass before the merge request, the author can't merge it without the deployment to dev.
    I didn't try deployment jobs, but I suspect these work similarly.
  • Use parameter, like DeployDevFromPR, that should indicate whether the pipeline should check to code, or deploy it. Then set two branch policies, one automatic and required (the checks) and one manual and optional (the deployment). However, there is no possibility to set the env variable from the PR view (unless the author requeue the pipeline again), so the manual step runs the check.
  • Decide based on variable variables['Build.QueuedBy']: the value is different when pipeline is triggered automatically. For user, there is its username, but for automatic triggers there is "Microsoft.VisualStudio.Services.TFS". However, this breaks if the user wants to rerun the check pipeline (then the QueuedBy will be the user).

Can I somehow get to the desired effect to allow user deploy from PR manually without blocking the merge?

Thank you


Solution

  • I am using Azure DevOps YAML pipelines and have two stages - one is deploying to dev (is triggered by commit in master), and the second is checking pull requests (running tests and static analysis)

    Instead of using a single pipeline with 2 stages consider splitting the pipeline in two:

    • Test pipeline: runs the tests and static analysis
    • Deploy pipeline: deploys the code

    After splitting the pipelines, create a build policy in the Build Validation section for each one:

    • Test pipeline policy should be set as required, i.e. the build must always run as part of the pull request validation
    • Deploy pipeline policy should be set as optional - it is up to the PR creator to decide if this build should be run as part of the pull request validation or not

    Example of a policy for the Deploy pipeline:

    Optional branch policy