Trigger Build for PR but Prevent CD Until PR Completes

Viewed 1079

My team and I are struggling to implement a particular CI/CD pattern in ADO.

We have defined a Build Pipeline called "Develop" and a Release Pipeline with the same name. We have set a Build Policy on our development branch that requires the "Develop" Build Pipeline succeeds before the PR can be completed. We also have policies for reviews/approvals, comment resolution, and linked work items.

As expected, when a PR is raised, the build referenced in our policy is kicked off. Also as expected, the PR cannot be completed until that build succeeds. The problem we had, though, was that because we had a CD trigger set up on the Release Pipeline, the artifacts generated by the PR-triggered build would always get deployed, regardless of the status of the rest of the PR policies.

The sequence we want is this:

  1. Raising a PR causes our "Develop" Build Pipeline to execute.
    • This build must succeed before the PR can be completed.
    • This build's artifacts are stored such that the Build Pipeline does not have to run again after the PR is completed, just to do the deployment (the build takes a while).
  2. The Build Pipeline successfully executes.
    • The PR still cannot be completed until other policies are satisfied.
  3. The remaining branch policies are satisfied.
  4. The PR is completed.
    • Only now can the artifacts generated from the Build Pipeline be deployed.
  5. The "Develop" Release Pipeline is executed, deploying the artifacts to our testing environment.

After fiddling around with variations of our Pipelines, I tried simply setting the "Pull request trigger" instead of the "Continuous deployment trigger" on the Release Pipeline. I assumed this would require that a PR had resulted in the creation of artifacts, and the PR completed. My assumption was incorrect. With this new setup, raising the PR triggers the Build Pipeline, and success of the Build Pipeline triggers the Release Pipeline, even if the PR has not yet been completed.

Our Build Pipeline builds the solution, runs tests, runs some PowerShell scripts, and finally publishes artifacts to Azure Pipelines. Perhaps this is doing too much. Perhaps there is some way to build, run the tests and scripts, and then somehow wait for the PR to complete before passing the artifacts along to the Release Pipeline. I just can't seem to figure it out. Is there any way to accomplish the sequence I listed above?

Requirements, in a nutshell:

  • The build should start as soon as the PR is raised.
  • The build should not have to execute more than once (it takes a long time, and we can't wait for two builds just to satisfy a build policy before the "real" build).
  • The PR completion should trigger the Release Pipeline.
1 Answers

There is some misunderstanding of PR trigger from your description.

You can configure a pull request trigger that will create a new release when a pull request uploads a new version of the artifact. Enable the trigger and add the branches targeted by pull requests that you want to activate this trigger.

Pull request triggers

However for CD triggers, you could add build branch filters if you want to create the release only when the build is produced by compiling code from certain branches (only applicable when the code is in a TFVC, Git, or GitHub repository) or when the build has certain tags. These can be both include and exclude filters. For example, use features/* to include all builds under the features branch. You can also include custom variables in a filter value.

Alternatively, you can specify a filter to use the default branch specified in the build pipeline. This is useful when, for example, the default build branch changes in every development sprint. It means you don't need to update the trigger filter across all release pipelines for every change - instead you just change the default branch in the build pipeline.

More details please take a look Jeff Shepler's reply in this similar question: VSTS release pull request build trigger

Related