Yaml parameter to inline script

769 Views Asked by At

Hello I think the problem I am facing might be a beginner doubt, but here it is:

I have a stage yaml which is called by my main yaml. Stage yaml has a parameter which I want to use in inline script.

Here is the yaml (skeleton)

parameters:
- name: myParam
  type: string

stages:
- stage: 
  dependsOn:   
  displayName: 
  pool:
    name: 
  jobs:
  - deployment: 
    displayName: 
    pool:
      name: 
    environment: 
      name: 
    strategy:
      runOnce:
        deploy:
          steps:
          - template: 
            parameters:
              azureServiceConnection: 
              subscriptionId: 

          - task: AzureCLI@2
            displayName: ''              
            inputs:
              workingDirectory: ''
              azureSubscription: 
              scriptType: 'pscore'
              scriptLocation: inlineScript
              inlineScript: |
                
                Write-Host  //I want to print it here or assign to a variable like next line
                $paramValue = //here
                

How can I use myParam here?

2

There are 2 best solutions below

1
On

Specify an env block and reference the value as an environment variable.

i.e.

- task: AzureCLI@2
  inputs: 
    inlineScript: |
      Write-Host $env:FOO
  env:
    FOO: ${{ parameters.myParameter }}
1
On

If your variables are readonly (so they'll be immutable) you can use pipeline variables directly in your inline statements using the default template syntax. This is what I use and it works successfully:

variables:
  - name: RgName
    value: 'default-eus-rg'
    readonly: true

  - name: AzureLocation
    value: 'eastus'
    readonly: true
    
    # ...later in the .yml pipeline
    
    - task: AzureCLI@2
      displayName: 'Azure CLI: Provision Resource Groups'
      inputs:
         azureSubscription: 'My Subscription'
         scriptType: 'pscore'
         scriptLocation: 'inlineScript'
         inlineScript: |
            az group create --name $(RgName) --location $(AzureLocation)

Here is some additional information about variables for use as environmental variables in pipelines within ADO: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#environment-variables