Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Allow to transfer parameters as ENV variables to task #252

Closed
Rabattkarte opened this issue Feb 10, 2022 · 5 comments
Closed

Allow to transfer parameters as ENV variables to task #252

Rabattkarte opened this issue Feb 10, 2022 · 5 comments

Comments

@Rabattkarte
Copy link

In our pipelines, we have the following syntax on multiple occasions. This is because we are using the replacetokens task in pipeline step templates, where you are unable to define a variables: block and we must not call templates with magic numbers.

Please have a look at the following example:

replaceTemplate.yaml

parameters:
  - name: myParam1
    type: string
  - name: myParam1
    type: string
  - name: myTargetFile
    type: string

steps:
  - checkout: self

  - bash: |
      echo "Using myParam1: ${{ parameters.myParam1 }}"
      echo "##vso[task.setvariable variable=myParam1]${{ parameters.myParam1 }}"

      echo "Using myParam1: ${{ parameters.myParam2 }}"
      echo "##vso[task.setvariable variable=myParam1]${{ parameters.myParam2 }}"
    displayName: Transfer params to vars for replacetokens task

  - task: replacetokens@4
    inputs:
      targetFiles: '${{ parameters.myTargetFile }}'
    displayName: 'Replace tokens in: ${{ parameters.myTargetFile }}'

My proposal would be a input parameter similar to the Bash task's bashEnvValue:, using the example above:

replaceTemplate.yaml

parameters:
  - name: myParam1
    type: string
  - name: myParam1
    type: string
  - name: myTargetFile
    type: string

steps:
  - checkout: self

  - task: replacetokens@4
    inputs:
      targetFiles: '${{ parameters.myTargetFile }}'
      replaceMapping:
        - myParam1: ${{ parameters.myParam1 }}
        - myParam2: ${{ parameters.myParam2 }}
    displayName: 'Replace tokens in: ${{ parameters.myTargetFile }}'
@Rabattkarte
Copy link
Author

I guess this relates to #237 and should be possible to implement since the rendering takes place before the task is created.

So the proposed replaceMapping object can be used by the code running the replacetokens task.

@qetza
Copy link
Owner

qetza commented Feb 11, 2022

Hi @Rabattkarte,
Thanks for the feedback, i will think about the feasibility to add a parameter with a list of key/value and inject this as variables which are available for replacement.

@kwaazaar
Copy link

Sounds like a good plan @Rabattkarte.

I'm the author of #237 and run into a similar issue today. I'm doing the replacetokens from a template that receives the 'data' als parameters. In template files you cannot specify variables (unless used as a variables-template, but that's another thing). Specifying a mappingtable would be very handy for me, as I need to replace just 3 tokens.

@kwaazaar
Copy link

I just solved it by using an external variables file which I generate in a previous step using Bash. Works too, but a bit cumbersome.

@qetza
Copy link
Owner

qetza commented Mar 14, 2022

Hi @Rabattkarte, @kwaazaar,
The latest version of the task allows you to supply inline variables using the YAML syntax so you can inject parameter values now like this:

- task: qetza.replacetokens-dev.replacetokens-task.replacetokens@4
   displayName: 'inline variables'
   inputs:
     targetFiles: '*.json'
     inlineVariables: |
       myParam1: '${{ parameters.myParam1 }}'
       myParam2: '${{ parameters.myParam2 }}'

@qetza qetza closed this as completed Apr 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants