Azure Pipelines how filter artifacts per stage for

2019-07-27 11:40发布

Let's say I have these 3 Stages: Dev, QC, Prod.

My requirements are:

  1. Artifacts only from specific branches(release/*) can be deployed to QC/Prod
  2. Artifacts from all branches can be deployed to Dev

I can achieve what I want using Artifact filters for "After stage" triggered Releases but I need this for "Manual only".

Is there a workaround that will let me control/filter which artifacts are available for deployment for specific stages/environments?

enter image description here

Basically, I need the Azure DevOps equivalent of Octopus Channels.

Update
I think I'm close to a solution.
In the "Pre-deployment conditions", I can add a new Deployment Gate which makes a Rest API call. e.g URL suffix=/Release/releases/76

Now, I just need to correctly parse the ApiResponse because the below Success criteria doesn't work

eq(root['artifacts[0].definitionReference.branch.id'], 'refs/heads/master')

Evaluation of expression 'eq(root['artifacts[0].definitionReference.branch.id'], 'refs/heads/master')' failed.

2条回答
Summer. ? 凉城
2楼-- · 2019-07-27 12:27

There is a workaround:

In the QC/Prod stages add a custom condition that the job will be executed only where the artifacts source branch is release/*:

startsWith(variables['Release.Artifacts.{Artifacts-Alias}.SourceBranch'], 'refs/heads/release')

enter image description here

Now, when you manually run the QC/Prod stages and the artifacts not came from the release the job not will be executed:

enter image description here

查看更多
爷、活的狠高调
3楼-- · 2019-07-27 12:35

As you said, you can do this using Deployment gates on your stages.

  1. Create a new Generic service connection from Project Settings -> Pipelines -> Service Connections.
    For service URL something like https://vsrm.dev.azure.com/{OrgName}/{ProjectName}/_apis

  2. On your stage, open the Pre-Deployment Conditions

  3. Enable the Gates option.

  4. Add a new Invoke REST API gate and set the Delay before evaluation to 0 minutes.
    4.1 Set the connection type to Generic.
    4.2 Select the service connection you created in step 1.
    4.3 Set the method to GET.
    4.4 Set the URL suffix to /Release/releases/$(Release.ReleaseId)
    4.5 On the Advanced area, set the Completion Event to ApiResponse.
    4.6 On the Advanced area, set the success criteria to (or startsWith)
    eq(root['artifacts'][0]['definitionReference']['branch']['id'],'refs/heads/master')

Now, if you try to deploy an artifact not from the master branch, the deployment will fail

查看更多
登录 后发表回答