I was recently asked by a colleague how to use templates within a YAML pipeline, as they wanted to template a part of the deployment, this was because they have the option to deploy to different Azure App Services for testing.
To do this we created a simple dummy pipeline:
trigger:- mainstages:- stage: Buildjobs:- job: Compilationpool:vmImage: ubuntu-lateststeps:- script: echo Build build build!displayName: 'Compile code'- stage: Test01jobs:- job: DeployToTest01pool:vmImage: ubuntu-lateststeps:- script: echo Steps to deploy to Test01displayName: 'Deploy'- job: RunTestsOnTests01dependsOn: DeployToTest01pool:vmImage: ubuntu-lateststeps:- script: echo Tests on Tests01displayName: 'Run tests'This shows the initial stage which would be used to build the code and another stage that would be for deploying to the App Service.
Everything in the Test01 stage needs to be duplicated for other test environments but ideally we didn't want to bloat the pipeline with a lot of duplication. Also, as Test01 is a deployment phase that should be updated as well.
We created a new YAML file in the repo called azure-environment.yaml:
parameters:- name: envtype: stringdefault: falsestages:- stage: Azure${{ parameters.env }}dependsOn: Buildjobs:- deployment: DeployTo${{ parameters.env }}Devenvironment: ${{ parameters.env }}pool:vmImage: ubuntu-lateststrategy:runOnce:deploy:steps:- script: echo Deploy to ${{ parameters.env }} DevdisplayName: 'Deploy'- job: RunTestsOn${{ parameters.env }}DevdependsOn: DeployTo${{ parameters.env }}Devpool:vmImage: ubuntu-lateststeps:- script: echo Tests on ${{ parameters.env }}DevdisplayName: 'Run tests'
The first four lines show that a parameter is expected called env, this is then used in the tasks to create the stage and deployment name.
This can then be used by updating the main yaml script:trigger:- mainstages:- stage: Buildjobs:- job: Compilationpool:vmImage: ubuntu-lateststeps:- script: echo Build build build!displayName: 'Compile code'- template: azure-environment.yamlparameters:env: Test01- template: azure-environment.yamlparameters:env: Test02This means that the template block can be easily added to branches if for a period of time the code needs to be deployed to another test environment.
As the deployment uses an environment these would need to be configured in Azure DevOps but it doesn't have to contain anything, although it does provide the functionality for approvals which could be useful if used for higher end deployments (such as PreProd and Production).
To create an environment simply select environments:
No comments:
Post a Comment