Search code examples
yamlenvironment-variablesazure-pipelinesazure-pipelines-release-pipelineazure-pipelines-tasks

Variable group not resolved in File Creator task in Azure Pipelines


I have a project in Azure DevOps to build and release e2e tests for other FE projects. We are moving from conventional GUI pipelines with task groups to YAML pipelines, but I am having issues when using variables from a variable group.

What I had:

  • a Build pipeline to build and publish the artifact --> this works fine in yaml too
  • a Build pipeline with a few tasks - download artifact, npm install, File Creator task to create an .env file (variables were defined directly there in the GUI), npm command to run tests
  • and release.

What I have now:

  • YAML template for the file creation and test running (template as I will be using it instead of a task group for other projects, too)
  • YAML file for the build and release - it builds the artifact then includes the template.

The variables should be read from a variable group in the library where my project resides. The variable group has access to the pipeline. The pipeline validates, but then the run tests task fails as the variables are not resolved. Am I referencing them in the correct way? I would like to avoid hardcoding the variables in the yml file.

Here is my multistage yml pipeline

variables:
- group: End2EndV3
trigger:
  branches:
    include:
    - refs/heads/main
stages:
- stage: BuildArtifact
  jobs:
  - job: Job_1
    displayName: Agent job 1
    pool:
      vmImage: windows-2019
    steps:
    - checkout: self
      fetchDepth: 1
    - task: NodeTool@0
      displayName: Use Node 10.x
      inputs:
        versionSpec: 10.x
    - task: CopyFiles@2
      displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
      inputs:
        TargetFolder: $(Build.ArtifactStagingDirectory)
    - task: PublishBuildArtifacts@1
      displayName: 'Publish Artifact: MyArtifact'
      inputs:
        ArtifactName: MyArtifact
- stage: RunTests
  jobs:
  - job: RunTests
    steps:
    - template: template-test-release.yml
    
...

and here the yml template causing issues

steps:
- task: eliostruyf.build-task.custom-build-task.file-creator@6
  displayName: 'Create cypress.env.json'
  inputs:
    filepath: 'somefilepath/MyArtifact/cypress.env.json'
    filecontent: |
     {
       "myVariable": "$(myVariableFromLibraryValue)",
     }
    fileoverwrite: true
    verbose: true

- task: Npm@1
  displayName: 'npm ci install'
  inputs:
    command: ci
    workingDir: '$(System.DefaultWorkingDirectory)/somefilepath/MyArtifact'
    verbose: false

- task: Npm@1
  displayName: 'npm run tests'
  inputs:
    command: custom
    workingDir: '$(System.DefaultWorkingDirectory)/somefilepath/MyArtifact'
    verbose: false
    customCommand: 'run cy:run:tests'

- task: PublishTestResults@2
  displayName: 'Publish Test Results *-test-output.xml'
  inputs:
    testResultsFiles: '*-test-output.xml'
    searchFolder: '$(System.DefaultWorkingDirectory)/somefilepath/MyArtifact/cypress/results'
    mergeTestResults: true


Solution

  • Try passing the variable from the variable group as a parameter to the template where these will be used.

    For example:

    Main pipeline
    variables:
    - group: End2EndV3
    
    # code omitted for brevity
    
    - stage: RunTests
      jobs:
      - job: RunTests
        steps:
        - template: template-test-release.yml
          parameters:
            foo: "$(myVariableFromLibraryValue)" # from variable group
    

    template-test-release.yml:

    parameters:
      - name: foo
        type: string
        displayName: 'Foo'
    
    steps:
    - task: eliostruyf.build-task.custom-build-task.file-creator@6
      displayName: 'Create cypress.env.json'
      inputs:
        filepath: 'somefilepath/MyArtifact/cypress.env.json'
        filecontent: |
         {
           "myVariable": "${{ parameters.foo }}",
         }
        fileoverwrite: true
        verbose: true
        
    # .....
    
    

    Recommended reading: