Rozmaryn
Rozmaryn

Reputation: 331

Azure DevOps Pipeline: same template twice in one stage

In my main pipeline in one stage, I call the same (deployment) template twice with just a bit different data:

//pipeline.yml

- stage: dev
   condition: and(succeeded(), eq('${{ parameters.environment }}', 'dev'))
   variables: 
      getCommitDate: $[ stageDependencies.prepare_date.set_date.outputs['setCommitDate.rollbackDate'] ]
   jobs:        
     - template: mssql/jobs/liquibase.yml@templates
       parameters:
         command: update
         username: $(username_dev)
         password: $(password_dev)
         environment: exampleEnv
         databaseName: exampleDB
         databaseIP: 123456789
         context: dev
         checkoutStep:
           bash: git checkout ${{parameters.commitHash}} -- ./src/main/resources/objects
  
     - template: mssql/jobs/liquibase.yml@templates
       parameters:
         command: rollbackToDate $(getCommitDate)
         username: $(username_dev)
         password: $(password_dev)
         environment: exampleEnv
         databaseName: exampleDB
         databaseIP: 123456789
         context: dev
//template.yml

parameters:
- name: command
  type: string
- name: environment
  type: string
- name: username
  type: string
- name: password
  type: string
- name: databaseName
  type: string
- name: databaseIP
  type: string
- name: context
  type: string
- name: checkoutStep
  type: step
  default: 
    checkout: self

jobs:
- deployment: !MY PROBLEM!
  pool: 
    name: exampleName
    demands:
    - agent.name -equals example
  environment: ${{ parameters.environment }}
  container: exampleContainer
  strategy:
    runOnce:
      deploy:
        steps:
          ...

My problem is that the deployment cannot have the same name twice. It is not possible to use the ${{parameters.command}} to distinguish between deployments names, because it contains forbidden characters. Only ${{parameters.command}} differs between two calls.

My question is whether it is possible to distinguish the name of a deployment other way than passing another parameter (e.g. jobName: ). I have tried various conditions and predefined variables but without success.

Additionally, I should add DependsOn so that the second template is called for sure after the first.

Upvotes: 5

Views: 4722

Answers (2)

Radzag
Radzag

Reputation: 56

To solve this issue, the job identifier should be empty in a template:

- job: # Empty identifier

More informations available HERE

Upvotes: 3

Krzysztof Madej
Krzysztof Madej

Reputation: 40909

It is not possible because getCommitDate and thus command parameter in your second stage contains runtime expression and job name needs compile time expression. So if you use command as job name at compile you have there rollbackToDate $(getCommitDate).

Upvotes: 1

Related Questions