Andrew
Andrew

Reputation: 794

Allow PR Build Validation builds without publishing artifacts

I'm trying to set up some build validation for PR's on our master branch (We're using Git and VSTS). I added some build policies to kick off various builds depending on what files have changed. For some background, we create PR's somewhat early in the process so we can track changes and add comments as our feature branch is developed.

Now, I want these builds to run and succeed in order for a PR to be completed, however, I don't want the output (artifacts) from these builds to be published. Our publishes look at the latest build artifact to deploy, and in this case, the latest build artifact would be from the PR. If we did allow these build artifacts from the PR to be published, then we'd be publishing feature branch code (which came from the PR) to our main site, which isn't desirable (we have a separate publish to handle our feature branch sites).

Essentially, when doing a PR, I just want to run the build to make sure that everything is kosher, and then throw away the build once it's completed.

Outside of creating a build definition solely for PR validation, I'm not sure what I can do. I've googled and asked around, but I'm either not asking the right questions, or the answer isn't as simple as I hope.

Hopefully I've included enough details to explain my problem.

TL;DR

How can I run build validations on a PR without having a publish pick up and deploy the build artifact that resulted from the validation build?

Edit (More details)

I tried adding conditions to the publish task (also tried putting the publish in a phase and conditionally running that). However, when I go to make a new release, it still sees the build definition that was run. Now, I imagine that if I tried to release that build definition, it would fail because no artifact was created. But I don't even want to see that build definition when I'm doing a release (don't want to accidentally select it).

Upvotes: 4

Views: 2323

Answers (2)

Marina Liu
Marina Liu

Reputation: 38106

There are multiple options you can follow to skip publishing artifacts for PR build validation.

Option 1: use a separate build definition for PR build validation

You can clone the current build definition, and use the cloned build definition for PR build validation separately.

enter image description here

In the cloned build definition, you can remove the tasks you do not want PR build validation execute (such as remove Publish Build Artifacts task).

Option 2: use customer condition to skip executing Publish Build Artifacts task

Or you can use custom conditions for the tasks you want to run conditionally (as Calidus and Daniel mentions).

Such as add below custom condition for Publish Build Artifacts task (if the build is for PR validation, then not execute the task):

ne(variables['Build.Reason'], 'PullRequest')

enter image description here

Upvotes: 4

Calidus
Calidus

Reputation: 1404

You could put the Publish Artifact task its own Phase at the end of your build definition then use 'Run this phase' with 'Custom condition using variable expressions'. You can also do this at the Task level as Dan suggested in the comment below.

not(eq(variables['Build.Reason'], 'PullRequest'))

https://learn.microsoft.com/en-us/vsts/pipelines/process/conditions?view=vsts

Upvotes: 7

Related Questions