Using Azure DevOps to run Cookiecutter templates
Ever wondered how you can scaffold repos in a yaml pipeline? This post will show how we could do this in Azure DevOps.
Scaffolding in Azure DevOps
One of the benefits of cookiecutter
is the ability to integrate it with other tools such as Azure Devops.
This is especially handy if we want to automate the process of scaffolding repos in a release pipeline, making it even easier for new repos to be set up.
The following is a very quick example that has been knocked together to give an understanding of how such a pipeline would look like. What is immediately obvious to anyone who’s worked with Azure DevOps pipelines is that it is fairly straightforward.
# Cookiecutter AzDo pipeline for data applications
trigger:
- none
pool:
vmImage: ubuntu-latest
steps:
- checkout: self
- checkout: git://<project_name>/<repo_name>@refs/heads/<branch_name>
path: scaffold
- task: UsePythonVersion@0
inputs:
versionSpec: '3.10'
displayName: 'Use Python 3.10'
- script: |
python -m pip install --upgrade pip
pip install cookiecutter
cookiecutter $(Agent.WorkFolder)/1/$TEMPLATE_DIR -o $TARGET_PROJ_NAME --no-input
displayName: 'Generate project from template'
workingDirectory: $(Build.ArtifactStagingDirectory)
env:
TEMPLATE_DIR: scaffold/cookiecutter-starter
TARGET_PROJ_NAME: data-project-01
- task: PublishBuildArtifacts@1
displayName: "Publish project"
inputs:
PathtoPublish: $(Build.ArtifactStagingDirectory)
ArtifactName: 'projects'
publishLocation: 'Container'
enabled: true
Key bits
- You need to have a cookiecutter template in place to reference in the pipeline.
- Once we call cookiecutter, we want to publish the output as an artifact but one could quite easily run a few git commands to check in the newly scaffolded repo.
- I have a slight preference for linux agents over windows due to speed.
By enabling cookiecutter to run in a release pipeline, this opens up opportunities for further automation that should enhance the end user experience.
Thanks for stopping by!