0
votes

I created two yaml pipelines.

  1. CI yaml pipeline which produces artifact and upload the artifact to Nexus repository.
  2. CD yaml pipeline which I want to design to download artifact from Nexus Repository and deploy to multiple environments.

When I use the Classic Release Pipeline, I can link the published build artifact from the CI pipeline. enter image description here enter image description here

But when I use YAML CD pipeline, I don't know how to link the CI pipeline so that I can pull the $(Build.BuildNumber) from the CI build. This is what I have tried so far..

resources:
 pipelines:
  - pipeline: test
    source: DevOps_CI_YAML_Nexus

and tried to use this variable to get the BuildNumber from the CI build. $(resources.pipeline.test.Build.BuildNumber) but looks like this syntax is wrong.

Could anyone share if you know how to get this to work?

Thank you for your help in advance.

2
$(Build.TriggeredBy.BuildNumber) might be what you are looking for if your CD pipeline is automatically triggered by CI run. Documented HERELJ.
Not get your latest information, are the following answers helpful to you?Jane Ma-MSFT

2 Answers

1
votes

To link published build artifacts from CI pipeline, you can use a built-in task called "download build artifacts".

enter image description here

In the task settings, you can choose an artifacts by its project, build pipeline and build version, just like what you can do in Classic UI release pipeline.

Here is an example script of the task:

- task: DownloadBuildArtifacts@0
  name: {task name}
  inputs:
    buildType: 'specific'
    project: '{project id}'
    pipeline: '{pipeline id}'
    buildVersionToDownload: 'latest'
    downloadType: 'single'
    artifactName: '{artifact name}'
    downloadPath: '$(System.ArtifactsDirectory)'

And the task has an output variable BuildNumber. You can use {taskname}.BuildNumber to get the number of the build which the artifact was downloaded from.

0
votes

It looks that this info is not avialable in variables (yet). I tried these variables:

- script: |
    echo '$(Build.TriggeredBy.BuildNumber)'!
    echo '$(Build.TriggeredBy.BuildId)'!
    echo '$(Build.TriggeredBy.DefinitionId)'!
    echo '$(Build.TriggeredBy.DefinitionName)'!
    echo '$(Build.TriggeredBy.ProjectID)'!
    echo '$(Build.BuildNumber)'!
    echo '$(Build.RequestedFor)'!
    echo '$(Build.RequestedForEmail)'!
    echo '$(Build.RequestedForId)'!

and this is what I got:

$(Build.TriggeredBy.BuildNumber)!
$(Build.TriggeredBy.BuildId)!
$(Build.TriggeredBy.DefinitionId)!
$(Build.TriggeredBy.DefinitionName)!
$(Build.TriggeredBy.ProjectID)!
20200901.12!
Microsoft.VisualStudio.Services.TFS!
!
00000002-0000-8888-8000-000000000000!

So I got meaningful value only for $(Build.BuildNumber).

So to get build number of your pipeline trigger you may use REST API:

variables:
  devopsAccount : 'thecodemanual'
  projectName : 'DevOps Manual'

steps:
- pwsh: |
    # Get a build info
    $uri = "https://dev.azure.com/$(devopsAccount)/$(projectName)/_apis/build/builds/$(Build.BuildId)?api-version=6.0-preview.6&expand=all"

    Write-Host $uri

    # Invoke the REST call
    $result = Invoke-RestMethod -Uri $uri -Method Get -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}

    $buildNumber = $result.triggerInfo.version
    Write-Host $buildNumber

    echo "##vso[task.setvariable variable=triggeredByBuildNumber]$buildNumber"
- script: |
    echo '$(triggeredByBuildNumber)'!

With this approach I retrieved 20200901.14 which is correct value of my build trigger.

And trigger info has more details:

    "triggerInfo": {
        "artifactType": "Pipeline",
        "alias": "test",
        "projectId": "4fa6b279-3db9-4cb0-aab8-e06c2ad550b2",
        "pipelineTriggerType": "PipelineCompletion",
        "source": "kmadof.dm-so-46",
        "pipelineId": "3320",
        "version": "20200901.14"
    },

if you need some.