skip to Main Content

TL;DR: How to get all the artifact names in deploy pipeline?

Hi,

I have multiple branches, and a single generic build pipeline to build their artifacts

artifact name example: TestProject_Feature1Branch_BuildNumber70_Date20240725

I’d like to create a generic deploy pipeline. How can I easily pick which artifact to deploy?

In the deploy pipeline, I could parameterize the artifact name but having to dig into each build artifact to copy & paste, or manually typing in the name seems to be annoying and error-prone. Ideally, I’d like a dropdown with all the artifact names from the build pipeline.

Thank you!

2

Answers


  1. You can get the all the artifact names of a build pipeline in a script using the REST API.

    First, get the successful runs’ ids of the pipeline with Builds – List. Then, get the artifact names for the build runs with Artifacts – List.

    Here is the sample PowerShell script:

    # Define variables
    $organization = ""
    $project = ""
    $pipelineDefinitionId = "106"
    $personalAccessToken = ""
    
    # Base64 encode the PAT
    $Authentication = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$personalAccessToken"))
    $Headers = @{Authorization = ("Basic {0}" -f $Authentication)}
    
    
    # Step 1: List the runs of a pipeline definition
    $buildsUrl = "https://dev.azure.com/$organization/$project/_apis/build/builds?definitions=$($pipelineDefinitionId)&resultFilter=succeeded&api-version=7.2-preview.5"
    $buildsResponse = Invoke-RestMethod -Uri $buildsUrl -Method Get  -Headers $Headers
    
    # Step 2: Get the ID of each run
    $buildIds = $buildsResponse.value | ForEach-Object { $_.id }
    
    # Step 3: Get the artifact name of each run
    $artifactNames = @()
    foreach ($buildId in $buildIds) {
        $artifactsUrl = "https://dev.azure.com/$organization/$project/_apis/build/builds/$buildId/artifacts?api-version=7.2-preview.5"
        $artifactsResponse = Invoke-RestMethod -Uri $artifactsUrl -Method Get  -Headers $Headers
        $artifactNames += $artifactsResponse.value | ForEach-Object { $_.name }
    }
    
    # Output artifact names
    $artifactNames
    
    # Output each artifact name with the format of parameters values
    foreach ($artifact in $artifactNames) {
        Write-Output "- $artifact"
    }
    
    

    After running the script, you can copy the output of the script and use it as parameters values in the deploy pipeline YAML.

    Login or Signup to reply.
  2. I’d like to create a generic deploy pipeline. How can I easily pick which artifact to deploy?

    Based on your description, I suggest that you can use the Pipeline resource in deploy Pipeline.

    Here is an example:

    resources:
      pipelines:
      - pipeline: MyCIAlias
        project: projectName
        source: PipelineName
    
    steps:
    
    - download: MyCIAlias
    

    When you run the Pipeline, you can click the Resources tab and select the target artifacts in target Pipeline Run.

    For example:

    enter image description here

    Note: By default, it will show the Build Number, branch, date information. You can select the artifacts based on the information. Or you can custom the Build number variable based on the artifact name.

    For more detailed info, you can refer to this doc: Pipelines resource definition

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search