stephen fernandes
stephen fernandes

Reputation: 133

Inheritance for the parameters defined in azure pipelines

I have multiple azure pipelines which have similar azure YAML files. The parameters defined in those YAML files are repetitive. Can it be inherited, if I define those parameters in a common YAML file?

##saving-account.yml 
name: savings
parameters:
 -name:run_mode
  type:string
  default:custom
 -name:app_version
  type:string
  default:uat

stages:

-stage:RunScript
 jobs:
  -job: script
   steps: script
     -script: /mylocation/savings.sh -r ${{parameters.run_mode }} -a ${{ parameters.app_version}}

This is the saving account pipeline. Similarly, I have for loan/mortgage accounts
##loans-account.yml 
name: savings
parameters:
 -name:run_mode
  type:string
  default:custom
 -name:app_version
  type:string
  default:uat
stages: -stage:RunScript jobs: -job: script steps: script -script: /mylocation/loans.sh -r ${{parameters.run_mode }} -a ${{ parameters.app_version}}
and this is true for other scripts too. So, if we see both the examples, the parameters remain the same, and the jobs->steps->script file changes. Can I pull out the parameters from the individual files and put them in a common file and then extend that file somehow in other files. I have seen this example,
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops
It talks about inheriting processes as templates, but I could not find an example for using the common parameters. I don't want to use variables, since I am not sure they can be changed while execution externally from azure-pipelines UI.

Upvotes: 0

Views: 1756

Answers (2)

DreadedFrost
DreadedFrost

Reputation: 2978

Alternate option is to use a variable yaml template under the jobs. This template can reference the same file between pipelines to reduce redundancies. Would be switching ${{parameters.}} to ${{variables.}} Can even switch the variable group depending on a parameter, in this case environment.

Would look like:

 jobs:
 -job: script
  variables:
  - template: ../variables/${{ parameters.environmentName }}.yml
  steps: script
  - script: /mylocation/loans.sh -r ${{variables.run_mode }} -a ${{ variables.app_version}}

The variable.yml file might look like:

variables:
 -name:run_mode
  type:string
  value:custom
 -name:app_version
  type:string
  value:uat

Doesn't exactly fit your ask but is an alternative to having the same values defined multiple times.

This variable file can be centrally located in another repo to.

Upvotes: 2

Krzysztof Madej
Krzysztof Madej

Reputation: 40779

Am afraid that I have a short answer. You cannot do that. You cannot reuse common parametrs. You can just nest temlates but actually nothing more.

Upvotes: 0

Related Questions