4

Basically, I have 2 Individual Azure Pipelines (CI, CD). And, I have enabled trigger only for the CI pipeline. Once the CI (build) pipeline was completed, it should trigger the CD pipeline. And, I am passing the parameters and variables in the CI pipeline. But, I want to utilize the same CI parameter values in the CD pipeline also.

### CI Pipeline ###
parameters:
- name: environment
  displayName: 'Choose Environment'
  type: string
  default: Dev
  values:
    - Dev
    - Qa
    - Stage
    - Demo
    - Live

variables:
- group: PoC-Web
- group: PoC-Secrets
- name: version

In the CI Pipeline, am using the environment parameter and declaring the version variable. Now, I want to Utilize these values in the CD Pipeline.

### CD Pipeline ###
resources:
  pipelines:
  - pipeline: web-ci
    source: PoC-Web-CI
    trigger:
     branches: 
      include:
        - cicd/azure-pipelines
        - develop
stages:
- stage: 'Package_Deploy'
  jobs:
  - deployment: 'Deploy_to_Server'
    environment: 'PoC-Web.PoC_WEB_SERVER'
    strategy:
      rolling:
        deploy:
          steps:
          - task: CmdLine@2
            inputs:
              script: |              
                echo "Fetching CI Pipeline Environment Variables"
                echo "Pipeline-ID: $(resources.pipeline.web-ci.pipelineID)"
                echo "Source-Commit: $(resources.pipeline.web-ci.sourceCommit)"
                echo "Version: $(resources.pipeline.web-ci.version)"
                echo "Version: $(resources.pipeline.web-ci.variables.version)"
                echo "environment: $(resources.pipeline.web-ci.parameters.environment)"
                echo "environment: $(resources.pipeline.web-ci.templateParameters.environment)"

In the above CD pipeline, am able to get the pipeline-Id & Source-Commit (i.e., predefined variables). But unable to fetch the user-defined variables and parameter values like version and parameter.

An early reply would be appreciated.

Sainadh P
  • 43
  • 4

2 Answers2

2

For your case I would use Pipeline Artifacts. Full description you can find here https://tsuyoshiushio.medium.com/how-to-pass-variables-with-pipeline-trigger-in-azure-pipeline-5771c5f18f91

In CI Pipeline

Export all necessary variables to a textfile, next publish it as an Artifact.

  - task: CmdLine@2
    displayName: Create artifact from variables
    inputs:
      script: |
        echo "##vso[task.setvariable variable=FOO;]$(FOO)" >  $(Build.ArtifactStagingDirectory)/pipeline.env
  - task: PublishBuildArtifacts@1
    displayName: publish variables
    inputs:
      PathtoPublish: '$(Build.ArtifactStagingDirectory)'
      ArtifactName: 'variables'
      publishLocation: 'Container'

In CD Pipeline

Create a task, which will download the Artifact containing the variables.

- task: DownloadBuildArtifacts@0
  displayName: 'Download variables'
  inputs:
    buildType: 'specific'
    project: '<YOUR_PROJECT_ID_HERE>'
    pipeline: '<YOUR_PIPELINE_ID_HERE>'
    buildVersionToDownload: 'latest'
    downloadType: 'specific'
    downloadPath: '$(System.ArtifactsDirectory)'
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Cloudziu
  • 475
  • 3
  • 8
  • Hello Cloudziu, Appreciate your answer. It's working fine, if, we are using azure hosted pipelines. In my case, am using rolling strategy deployment (i.e environment.machine_name) which is not working fine – Sainadh P Oct 25 '21 at 06:32
  • how to use this artifact after downloading it in CD? – gipcu Nov 07 '22 at 13:28
0

Building on @Cloudziu's excellent work:

According to the Microsoft docs, PublishArtifacts is deprecated, and MS recommends using the publish task instead. An analogous download task exists, but for transferring information between stages or between pipelines, you must still use the DownloadPipelineArtifacts task.

If you have two pipelines, pipeline-one which triggers pipeline-two upon completion, you can pass info from one to the other like this:

To publish from pipeline-one:

- powershell: |
    $json = @"
    {
        'build_id': '$(Build.BuildID)',
        'build_number': '$(Build.BuildNumber)',
        'build_type': '$(Build.Reason)',
        'source_repo': '$(Build.Repository.Name)',
        'source_branch': '$(Build.SourceBranchName)',
        'source_commit_id': '$(Build.SourceVersion)'
    }
    "@
    $f = '$(Pipeline.Workspace)/s/ansible.json'
    Add-Content -Path $f -Value $json
  displayName: Create artifact from variables

- publish: ansible.json
  artifact: theAnsible
  displayName: Publish the artifact

To read the artifact in pipeline-two:

resources:
  pipelines:
  - pipeline: pipeline1
    source: xyzzy.pipeline-one
    trigger:
      enabled: true
      branches:
        include:
        - develop
        - release_*
    
- task: DownloadPipelineArtifact@2
  displayName: Download the artifact
  inputs:
    source: 'specific'
    project: 'the-project-name'
    pipeline: 'xyzzy.pipeline-one' # this works
    # pipeline: 12 # or use the pipeline number instead
    preferTriggeringPipeline: 'true'
    runVersion: 'latest'
    artifact: theAnsible
    path: '$(Pipeline.Workspace)/s/'

- powershell: |
      $f = "$(Pipeline.Workspace)/s/ansible.json"
      if( Test-Path $f ) {
        Get-Content $f
      } else {
        Write-Host '$f not found'
      }
  displayName: Read the artifact
Ray Depew
  • 573
  • 1
  • 9
  • 22