Reputation: 569
Basically I wanted to reuse some common steps in one of my azure build pipeline definitions (a simple example being I have one copy file step and one command line step) that I want to reuse across several new build pipelines that I plan to create. Can I create these commonly used steps as a template and reuse them rather than manually creating them for every build definition I create?
I do know we can use templates to impose standards and security checks. But wanted to check if we can use this to combine frequently used steps across different build definitions. Acting as a single template and can be reused.
I also know we can easily achieve this via Task group but I wanted to create this as a yaml template so as to version control them.
Upvotes: 2
Views: 3185
Reputation: 19471
You can copy content from one YAML and reuse it in a different YAMLs. This saves you from having to manually include the same logic in multiple places. The include-steps.yml
file template contains steps that are reused in azure-pipelines.yml
.
For example : First create a template yaml file(include-steps.yml
) in your source repo.
# File: include-steps.yml
steps:
- task: CopyFiles@2
inputs:
SourceFolder: '$(agent.builddirectory)'
Contents: '**'
TargetFolder: '$(build.artifactstagingdirectory)'
displayName: 'Run a one-line script'
- task: CmdLine@2
inputs:
script:
echo Hello world
Then reference this template.yml file in your azure-pipelines.yml
.
# File: azure-pipelines.yml
jobs:
- job: Linux
pool:
vmImage: 'ubuntu-latest'
steps:
- template: templates/include-steps.yml # Template reference
Upvotes: 2