Working on Azure DevOps Pipelines you may notice some repetition over time, whether that be copying the same stage or task multiple times but changing some values. This will result in your pipeline growing and growing unnecessary over time. What can we do?
If you are coming from a developer perspective you will probably have heard of Don’t Repeat Yourself (DRY) as its considered to be a common principle – but like me; starting as an infrastructure engineer to all things DevOps, lets understand what is DRY.
To me, what does Don’t Repeat Yourself (DRY) mean?
- Don’t copy and paste within my pipelines
- Avoid duplication if possible, sometimes not!
- Use Variables
- Can I do this stage/task with fewer lines?
- Create dependsOn that allow me to write further logic
- I love logic, let me add more of that!
- What can I reuse in my next pipeline?
- Structure, structure and more structure!
The DRY principle is stated as “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system”.https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
I’ve mentioned what does DRY mean, so what are the benefits?
- Less code is good
- Saves time; whether that be to read or to write
- Easier to maintain in the long run, awesome parameters, great variables and no copy and pasting!
- You are not repeating or copy and pasting; this will reduce chances of any potential bugs or issues within your pipeline(s)
- Try to keep your code unique – if not, look at what you’ve created before. Try from your first pipeline creation to the tenth – keep a pattern and reuse what you have created before
Create a set of pipeline templates that you can reuse and refactor to create further pipelines, I do this quite often and a lot of my work is done from a set of template(s) that I created previously and use and refactor to make the pipeline(s) more specific to the current requirements and environments that I am working on.
Try to plan your pipeline before you begin writing it, think of it like a blog post – you want it to flow, why not follow that with your pipeline? Set the context and understand fully what you want that pipeline to do. Then begin to note down the stages, tasks – followed by where you can parameterise, variable usage and remove and potential duplication. Overtime
Should I use DRY in every pipeline?
I do think so but, sometimes it may not be possible due to various reasons, whether that be time to rewrite the existing pipeline; even sometimes – the cost involved required to test and create something new! But do try and work on any additional pipeline(s) or rewrite with DRY in mind and you will notice the difference it can make!
Questions to ask during reconfiguration of a pipeline
Some questions to think about, if you want to apply DRY to a current pipeline:-
- How many times has this config been repeated?
- Can I write this configuration with less lines?
- Find what is duplicated; could I write this and avoid and new duplication?
- Will refactoring make this pipeline easier to maintain in the future?
- Is there a copy/paste section I can change and remove?
I will leave you with this
- Less code means less code to maintain
- Try avoiding duplication – updating in one place is easier than several
- Make your code reusable
- Create a base template or set of principals that you will work with in every pipeline
- Remember sometimes, you may have to repeat your code
In the coming few weeks, I am going to be creating and writing more blog posts to help you with Keeping your pipelines DRY (Don’t Repeat Yourself); this is the kick-off and and I hope you enjoyed the intro 🙂