Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get variable values from pipeline resources in azure pipelines

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.

like image 855
Sainadh P Avatar asked Oct 25 '25 05:10

Sainadh P


1 Answers

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)'
like image 79
Cloudziu Avatar answered Oct 26 '25 23:10

Cloudziu