qetza / replacetokens-task

Azure Pipelines task to replace tokens in files with variables.
MIT License
11 stars 2 forks source link

Azure DevOps system variables were not found after migrating to version 6 in release stage #15

Closed fbuser1 closed 5 months ago

fbuser1 commented 5 months ago

values.yaml

app: __BUILD_REPOSITORY_NAME__
svc: __BUILD_DEFINITIONNAME__
tier: api

replacetokens@4 task configuration

  - task: qetza.replacetokens.replacetokens-task.replacetokens@4
    condition: succeeded()
    displayName: 'Replacetokens'
    inputs:
      rootDirectory: '$(Pipeline.Workspace)/drop'
      targetFiles: '**/*.yaml,**/appsettings.${{ parameters.env }}.json'
      actionOnMissing: fail
      tokenPattern: rm

I can't solve the problem of replacing default (system) Azure DevOps variables after migrating tasks to version 6

replacetokens@6 task configuration

  - task: qetza.replacetokens.replacetokens-task.replacetokens@6
    condition: succeeded()
    displayName: 'Replacetokens'
    inputs:
      root: '$(Pipeline.Workspace)/drop'
      sources: |
        **/*.yaml
        **/appsettings.${{ parameters.env }}.json
      missingVarLog: error
      tokenPattern: doubleunderscores
Starting: Replacetokens
==============================================================================
Task         : Replace Tokens
Description  : Replace tokens in text based files
Version      : 6.0.2
Author       : Guillaume ROUCHON
Help         : [Learn more about this task](https://github.com/qetza/replacetokens-task/blob/master/tasks/ReplaceTokensV6/README.md) (v6.0.2)
==============================================================================
 loading variables
 replacing tokens in '/home/vsts/work/1/drop/values.yaml'
##[error]variable 'BUILD_REPOSITORY_NAME' not found
##[error]variable 'BUILD_BUILDNUMBER' not found

Also, I have the same issue with the BUILD_DEFINITIONNAME variable. After rolling back task to version 4, everything works again as expected.

qetza commented 5 months ago

Hi @fbuser1, The issue you are facing is because the systems variables real names don't have _ but .. It's the internal mechanism of Azure Pipelines which replaces dots by underscores so that it can set them as environment variables.

In the previous versions of the task as the code was only meant for Azure Pipelines using the modified name with underscore was not an issue because when getting a variable value i was using the MS library which was doing the name conversion internally.

With version 6 I've moved the logic of the task in a reusable library so in the this version I'm using another MS function to first get all defines variables with their values and this function returns the real variable names with dots.

In your case I see 2 options:

  1. migrate to version 5 which uses the old logic and is still supported
  2. migrate to version 6 but change your files to use the real names of the variables with dots
    app: __BUILD.REPOSITORY.NAME__
    svc: __BUILD.DEFINITIONNAME__
    tier: api
fbuser1 commented 5 months ago

Hi @qetza, thank you so much for your assistance! Your suggestion to replace underscores with dots in the variable names worked perfectly and resolved my issue. I really appreciate your prompt and clear explanation!

qetza commented 5 months ago

Hi @fbuser1, I've release a new patch on v6 (6.0.5) with a reworked core lib which now allows to used normalized names in your token as in the previous versions. So using __BUILD_REPOSITORY_NAME__ and __BUILD_DEFINITIONNAME__ in your files will now be replaced correctly.