2
votes

I have this azure devops ci/cd pipeline using yaml. My yaml has two stages CI and CD. My CI stage has one job called BuildandDeploy. The CD stage has one deployment job. I am using universal artifacts to publish and downloading the same. In the CD phase I am using UniversalPackages devops task to download the artifact. The task has a input variable called vstsPackageVersion which is the package version that is shown in universal artifacts. I have known of two other variables that could be used $(Build.BuildId) and $(Build.BuildNumber). As a temporary work around I am hard coding the package version for the universal artifact.

I wasn't able to download the artifact with either of the built-in variables. Since the CI and CD are in the same pipeline, is there any way to store and retrieve the package version of the artifact? Is there a identifier like latest that I could use to get the latest artifact from universal package.

# specific branch build with batching
trigger:
  batch: true
  branches:
    include:
    - master

stages:
- stage: CI
  jobs:
  - job: BuildAndPublish
    pool:
      vmImage: 'Ubuntu-16.04'
    steps:
    - 
      script: |
          docker build -t $(dockerId).azurecr.io/$(imageName):$(version) .
          docker login -u $(dockerId) -p $(pswd) $(dockerId).azurecr.io 
          docker push $(dockerId).azurecr.io/$(imageName):$(version)

    - task: Bash@3
      displayName: Initialize Helm Client - create local repo
      inputs:
        targetType: 'inline'
        script: '
          helm init --client-only
        '
    - task: HelmDeploy@0
      displayName: Package helm chart 
      inputs:
        connectionType: 'Kubernetes Service Connection'
        command: 'package'
        chartPath: 'my-helm-dir'

    - task: UniversalPackages@0
      displayName: Publish helm package to my-company-artifacts
      inputs:
        command: 'publish'
        publishDirectory: '$(Build.ArtifactStagingDirectory)'
        feedsToUsePublish: 'internal'
        vstsFeedPublish: '$(my-feed-guid)'
        vstsFeedPackagePublish: 'my-artifact-name'
        versionOption: patch
        packagePublishDescription: 'My helm package descrition'

- stage: CD
  jobs:
  - deployment: DeployJob
    displayName: Deploy Job
    pool:
      vmImage: Ubuntu-16.04
    environment: dev
    strategy:
      runOnce:    
        deploy:
          steps:
          - task: UniversalPackages@0
            displayName: 'Universal download'
            inputs:
              command: download
              vstsFeed: '$(my-feed-name)'
              vstsFeedPackage: 'my-artifact-name'
              vstsPackageVersion: 0.0.32

          - task: ExtractFiles@1
            displayName: 'Extract files '
            inputs:
              archiveFilePatterns: '*.tgz'
              destinationFolder: 'my-folder'
              cleanDestinationFolder: true
1
Why not publish a build artifact instead of using a versioned universal package? Do you need access to the artifact from outside of the build/deployment process?Daniel Mann
It is published as a universal artifact as you see. I am trying to download the same. I am able to download the artifact, the problem is how do I get the artifact package version.randominstanceOfLivingThing
That didn't answer my question.Daniel Mann

1 Answers

5
votes

The Universal Packages task based on az artifacts universal cli that not support "latest version", but only specific version (by the way, this cli is on preview).

As workaround, you can use the Rest API to retrieve the latest version and set a new variable, then, in the download task use this variable.

For example, add a PowerShell task that get the version number and set the variable:

- powershell: |
   $head = @{ Authorization = "Bearer $env:TOKEN" }
   $url = "https://feeds.dev.azure.com/{organization}/_apis/packaging/Feeds/{feed-name}/packages/{package-guid}?api-version=5.0-preview.1"
   $package = Invoke-RestMethod -Uri $url -Method Get -Headers $head -ContentType application/json
   $latestVersion = ($package.versions.Where({ $_.isLatest -eq $True })).version
   Write-Host "The latest version is $latestVersion"
   Write-Host "##vso[task.setvariable variable=latestVersion]$latestVersion"
  env:
    TOKEN: $(system.accesstoken)

Now, in the download task use it:

vstsPackageVersion: $(latestVersion)

enter image description here

enter image description here