Search code examples
templatesazure-pipelines

Runtime compiled template expressions in Azure Pipelines


I have a use case where it is needed to first create a certain pipeline and only then refer to it in a template expression. My question is, is this already supported or isn't this something that would work? Take the following code example.

stages:
  - stage: A
    jobs:
    - job: Tryout
      steps:
      - task: AzureCLI@2
        displayName: '...'
        inputs:
          azureSubscription: ...
          failOnStandardError: false
          scriptType: bash
          scriptLocation: inlineScript
          inlineScript: |
            cat > ./pipeline.yml <<EOF
            steps:
              - script: echo hi
            EOF    
      - publish: ./pipeline.yml
        artifact: pipeline
  - stage: B
    dependsOn: A
    jobs:
    - job: Tryout
      steps:
      - download: current
        artifact: pipeline
      - task: PowerShell@2
        inputs:
          targetType: 'inline'
          script: |
            Write-Output "##vso[task.setvariable variable=filePath]\"$(Pipeline.Workspace)\pipeline\pipeline.yml\""
      - template: $(variables.filePath)

I would expect that when $(Pipeline.Workspace)\pipeline\pipeline.yml is created, it would run the template. Does this already exist, if so, how could I implement it?


Solution

  • I would expect that when $(Pipeline.Workspace)\pipeline\pipeline.yml is created, it would run the template. Does this already exist, if so, how could I implement it?

    I am afraid that the process doesn't supported by Azure DevOps for the time being.

    When you define the YAML template, the template needs to be expanded at compile time.

    Based on your YAML sample, the pipeline.yml file will be created at Pipeline runtime.

    In this case, the templated file: pipeline.yml cannot be read by Pipeline. When you run the Pipeline, it should get an error that the template file cannot be found.

    I can fully understand your requirement. You can submit a suggest ticket in the site: Developer Community to report this feature.