Create a new repository from a template in Azure DevOps
Having a project template repository in Azure DevOps to clone when starting new projects is helpful for data science teams who are wanting to speed up the creation of new solutions or help onboard new data scientists
Automate project creation using templates in Azure DevOps
Having a project template repository in Azure DevOps to clone when starting new projects is helpful for data science teams who are wanting to speed up the creation of new solutions or help onboard new data scientists. By combining a template repository and a DevOps build pipeline, you will be able to quickly get up and running.
Project creation build pipeline in DevOps
Azure DevOps does not have an out of the box solution for this use case, so I use the following method which runs a build pipeline that clones the template repository, makes some changes, and then pushes it back to a newly created repository in DevOps.
The steps are as follows:
- Create a repository in Azure DevOps that you would like to use as your template. Let's call it 'project-template'
- Create a second repository to contain the build pipeline you will create. We will call this 'utilities'. This is a useful repository to have in general to house all your automation pipelines and other functions which improve the ease of development
- Create a starter build pipeline and host it within the utilities repository
- Create a variable within the pipeline UI called 'project-name', this will be used as the name of your newly created repository. Defining this variable within the UI allows us to choose a project name when we run the pipeline in DevOps
- Use the following code, making sure to replace 'devops-organisation', 'devops-project', 'email', and 'user' with your own values
trigger:
- none
pool:
vmImage: "ubuntu-latest"
steps:
# Define your Python version
- task: [email protected]
inputs:
versionSpec: '3.x'
architecture: 'x64'
# Install or upgrade pip
- script: python -m pip install --upgrade pip
displayName: 'Upgrade pip'
# Install the azure-cli package needed
- script: pip install --pre azure-cli --extra-index-url https://azurecliprod.blob.core.windows.net/edge
displayName: 'upgrade azure cli'
# Add the devops extension
- script: az extension add -n azure-devops
displayName: 'Install Azure DevOps Extension'
- script: |
# Login to DevOps using your system access token
echo $(System.AccessToken) | az devops login
# Create your new empty repo
az repos create --name $(project-name) --organization https://dev.azure.com/devops-organisation --project "devops-project"
# Clone the template repo to the agent
git clone -q https://$(System.AccessToken)@dev.azure.com/devops-organisation/devops-project/_git/project-template
# Set your git config email and user
cd project-template
git config --global user.email "[email protected]"
git config --global user.name "myname"
# Commit your changes
git commit -am "Copied from project template"
# Add your new remote
git remote rename origin template
git remote add origin https://$(System.AccessToken)@dev.azure.com/devops-organisation/devops-project/_git/$(project-name)
# Push the changes
git push -u origin --all
displayName: 'Creating new repo from project-template'
Troubleshooting
- You may have to grant extra permissions for your DevOps pipeline user within pipelines -> manage security
Related articles
Set up CI/CD pipelines for machine learning in Azure DevOps