GitLab CI Templates Save Time with Reusable Pipelines

Day 40: Reusing Code with GitLab CI Templates - Stop Copy-Pasting Your Pipeline! Friends, yesterday we talked about Conditional Jobs. Today, let's discuss something that will save you SO much time every week. Think about it - you have 5-10 projects, and each one needs the same build, test, deploy steps. What do we usually do? We copy-paste the .gitlab-ci.yml file again and again. And when something changes, you have to update all those files. So much work, na? This is exactly where GitLab CI Templates come in handy. What are GitLab CI Templates? Simply put, templates are like a saved recipe. You write your pipeline steps ONCE, and then you can use that same recipe in any project. No copy-pasting at all! Simple Example: Instead of writing the same test job in every project: test:  stage: test  script:   - npm install   - npm test You create a template file and just include it: include:  - local: '.gitlab-ci-templates/test.yml' Now your .gitlab-ci.yml is short and clean! Types of Templates in GitLab: 1. Local Templates   - Stored in your own project   - Best for common jobs within a project   - Easy to use and manage 2. Project Templates   - Stored in a separate GitLab project   - Any project can include it   - Perfect for company-wide standards 3. Remote Templates   - Stored on any Git server (GitHub, GitLab, etc.)   - Good for open-source or multiple organizations 4. Built-in Templates   - GitLab gives you some ready-made templates   - Useful for common frameworks like Node.js, Python, Java Real-World Example: Let's say your company has a standard security scan that must run on EVERY project. Old way (copy-paste): You write the security scan job in 20 different .gitlab-ci.yml files. When the scan tool changes, you update 20 files. So painful! New way (template): You create ONE template file with the security scan. All 20 projects just include that one template. When the scan tool changes, you update just ONE file. So easy! How to Create and Use Templates: Step 1: Create a template file .gitlab-ci-templates/common-build.yml: build_job:  stage: build  script:   - echo "Building application"   - npm install   - npm run build Step 2: Include it in your pipeline .gitlab-ci.yml: include:  - local: '.gitlab-ci-templates/common-build.yml' deploy:  stage: deploy  script:   - echo "Deploying to production" Done! Your build job now comes from the template. Pro Tips: 1. Keep templates small - one job per file 2. Name them clearly - like build-node.yml, test-python.yml 3. Use project templates for company standards 4. Version your templates just like your code 5. Always test template changes in a non-production project first So friends, remember - templates help you write less code and make it easier to maintain. No more copy-pasting pipelines everywhere! That's it for Day 40! Tomorrow we will learn about GitLab CI Environments and Deployments. #GitLab #CICD #DevOps #Templates

To view or add a comment, sign in

Explore content categories