Azure DevOps pipeline template - how to concatenate a parameter
Asked Answered
U

2

7

All afternoon I have been trying to get my head around concatenating a parameter in an ADO template. The parameter is a source path and in the template a next folder level needs to be added. I would like to achieve this with a "simple" concatenation. The simplified template takes the parameter and uses it to form the inputPath for a PowerShell script, like this:

parameters:
  sourcePath: ''

steps:   
- task: PowerShell@2
  inputs:
    filePath: 'PSRepo/Scripts/MyPsScript.ps1'
    arguments: '-inputPath ''$(sourcePath)/NextFolder''

I have tried various ways to achieve this concatenation:

  • '$(sourcePath)/NextFolder'
    • see above
  • '$(variables.sourcePath)/NextFolder'
    • I know sourcePath is not a variable, but tried based on the fact that using a parameter in a task condition it apparently only works when referencing through variables
  • '${{ parameters.sourcePath }}/NextFolder'

And some other variations, all to no avail. I also tried to introduce a variables section in the template, but that is not possible.

I have searched the internet for examples/documentation, but no direct answers and other issues seemed to hint to some solution, but were not working.

I will surely be very pleased if someone could help me out.

Thanx in advance.

Upstroke answered 18/3, 2021 at 16:0 Comment(0)
U
2

Thanx, Yujun. In meantime did get it working. Apparently there must have been some typo that did block the script from executing right as the se solution looks like one of the options mentioned above.

parameters:
  sourcePath: ''

steps:   
- task: PowerShell@2
  inputs:
    filePath: 'PSRepo/Scripts/MyPsScript.ps1'
    arguments: '-inputPath ''$(sourcePath)/NextFolder''
Upstroke answered 19/3, 2021 at 10:5 Comment(3)
Try to change the filePath to use '../PSRepo/Scripts/MyPsScript.ps1' if you still have this issue. And by the way, if my answer will help you solve this issue, could you please accept it? This will help other developer to solve there similar issue. Thanks for your cooperation.Demodena
Thanx. I can mark your first reply, but only read that after I got the solution already is written in my previous comment. So I marked that as a solution.Upstroke
How did you get away with an uneven amount of single apostrophe's in the arguments line?Lapsus
D
6

We can add the variables in our temp yaml file and pass the sourcePath to the variable, then we can use it. Here is my demo script:

Main.yaml

resources:
  repositories:
    - repository: templates
      type: git
      name: Tech-Talk/template

trigger: none

variables:
  - name: Test
    value: TestGroup
    
pool:
  # vmImage: windows-latest
  vmImage: ubuntu-20.04

  
extends:
  template: temp.yaml@templates
  parameters:
    agent_pool_name: ''
    db_resource_path: $(System.DefaultWorkingDirectory)
    # variable_group: ${{variables.Test}}   

temp.yaml

parameters:
- name: db_resource_path
  default: ""   
# - name: 'variable_group'    
#   type: string    
#   default: 'default_variable_group'
- name: agent_pool_name
  default: ""
    
 
stages:
  - stage:      
    jobs:
    - job: READ
      displayName: Reading Parameters
      variables:
      - name: sourcePath
        value: ${{parameters.db_resource_path}}
#     - group: ${{parameters.variable_group}}
      steps:
      - script: |
          echo sourcePath: ${{variables.sourcePath}}
      - powershell: echo "$(sourcePath)"

Here, I just use the workingDirectory to as the test path. You can use the variables also. Attach my build result: enter image description here enter image description here

Demodena answered 19/3, 2021 at 3:7 Comment(0)
U
2

Thanx, Yujun. In meantime did get it working. Apparently there must have been some typo that did block the script from executing right as the se solution looks like one of the options mentioned above.

parameters:
  sourcePath: ''

steps:   
- task: PowerShell@2
  inputs:
    filePath: 'PSRepo/Scripts/MyPsScript.ps1'
    arguments: '-inputPath ''$(sourcePath)/NextFolder''
Upstroke answered 19/3, 2021 at 10:5 Comment(3)
Try to change the filePath to use '../PSRepo/Scripts/MyPsScript.ps1' if you still have this issue. And by the way, if my answer will help you solve this issue, could you please accept it? This will help other developer to solve there similar issue. Thanks for your cooperation.Demodena
Thanx. I can mark your first reply, but only read that after I got the solution already is written in my previous comment. So I marked that as a solution.Upstroke
How did you get away with an uneven amount of single apostrophe's in the arguments line?Lapsus

© 2022 - 2024 — McMap. All rights reserved.