GitLab CI CD Tutorial Pipelines Runners & Deployment

Zaheer Ahmad 5 min read min read
Python
GitLab CI CD Tutorial Pipelines Runners & Deployment

Introduction

GitLab CI/CD is a powerful tool that allows developers to automate the process of building, testing, and deploying applications. In this tutorial, we will explore GitLab pipelines, runners, and deployment strategies tailored for Pakistani students and developers. Learning GitLab CI/CD can help you deliver your projects faster, reduce human error, and gain practical DevOps skills that are highly valued in the Pakistani IT industry.

With GitLab, you can define your workflow in code using .gitlab-ci.yml, manage environments like staging and production, and use runners to execute jobs automatically. Whether you are a student in Lahore experimenting with web apps or a freelancer in Karachi deploying client projects, mastering GitLab CI/CD can elevate your development workflow.

Prerequisites

Before diving into GitLab CI/CD, you should have:

  • Basic knowledge of Git and GitLab repositories.
  • Understanding of YAML syntax.
  • Familiarity with programming in at least one language (e.g., Python, Node.js, PHP).
  • Basic command-line skills (Linux or Windows terminal).
  • Optional but helpful: prior exposure to DevOps concepts.

These foundations will help you understand how pipelines, jobs, and runners work together.


Core Concepts & Explanation

GitLab Pipeline: Stages and Jobs

A pipeline is a group of automated tasks that run sequentially or in parallel. Pipelines consist of stages (build, test, deploy) and jobs (individual tasks within a stage).

Example:

stages:
  - build
  - test
  - deploy

build_job:
  stage: build
  script:
    - echo "Compiling code"
    - gcc main.c -o app

test_job:
  stage: test
  script:
    - echo "Running tests"
    - ./run_tests.sh

deploy_job:
  stage: deploy
  script:
    - echo "Deploying to production"
    - scp app user@server:/var/www/html/

Line-by-line explanation:

  • stages: defines the order of execution.
  • build_job, test_job, deploy_job are separate jobs assigned to stages.
  • script: contains the commands to run in that job.
  • Jobs in the same stage run in parallel; stages run sequentially.

GitLab Runner: Setup and Types

A GitLab Runner executes the jobs defined in a pipeline. Runners can be:

  • Shared Runners: Provided by GitLab, suitable for small projects.
  • Specific Runners: Dedicated to your project or organization.
  • Docker Runners: Run jobs in isolated Docker containers.

To install a runner on Ubuntu:

# Download and install GitLab Runner
curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

# Give executable permissions
chmod +x /usr/local/bin/gitlab-runner

# Register the runner
gitlab-runner register

Line-by-line explanation:

  • curl -L --output ... downloads the GitLab Runner binary.
  • chmod +x makes it executable.
  • gitlab-runner register links it with your GitLab project, prompting for URL, token, and runner type.

Deployment: Environments & Approvals

GitLab allows you to deploy code to different environments, such as staging or production, with optional manual approvals.

Example .gitlab-ci.yml:

stages:
  - build
  - deploy

deploy_staging:
  stage: deploy
  environment:
    name: staging
    url: http://staging.ahmad-app.pk
  script:
    - echo "Deploying to staging"
    - rsync -avz ./app user@staging-server:/var/www/html/

deploy_production:
  stage: deploy
  environment:
    name: production
    url: http://www.ahmad-app.pk
  when: manual
  script:
    - echo "Deploying to production"
    - rsync -avz ./app user@prod-server:/var/www/html/
  • environment: specifies the deployment target.
  • when: manual ensures production deployment happens only after approval.
  • rsync copies files from the local machine to the server.

Practical Code Examples

Example 1: Simple Node.js CI Pipeline

stages:
  - install
  - test
  - deploy

install_dependencies:
  stage: install
  script:
    - echo "Installing dependencies"
    - npm install

run_tests:
  stage: test
  script:
    - echo "Running unit tests"
    - npm test

deploy_app:
  stage: deploy
  script:
    - echo "Deploying Node.js app"
    - scp -r ./* user@server:/var/www/node-app/

Explanation:

  • install_dependencies installs project dependencies.
  • run_tests executes unit tests.
  • deploy_app copies the project to a server.

Example 2: Real-World Application — Laravel PHP Deployment

stages:
  - build
  - test
  - deploy

build_app:
  stage: build
  script:
    - echo "Composer install"
    - composer install --no-dev

test_app:
  stage: test
  script:
    - echo "Running PHPUnit tests"
    - ./vendor/bin/phpunit

deploy_staging:
  stage: deploy
  environment: staging
  script:
    - echo "Deploying Laravel app to staging"
    - rsync -avz --exclude '.env' ./ user@staging-server:/var/www/laravel-app/

Explanation:

  • composer install --no-dev installs dependencies excluding development packages.
  • ./vendor/bin/phpunit runs Laravel tests.
  • rsync --exclude '.env' deploys the app without sensitive environment files.

Common Mistakes & How to Avoid Them

Mistake 1: Incorrect YAML Indentation

YAML is sensitive to spaces. Misalignment breaks pipelines.

stages:
- build
  - test   # ❌ Wrong indentation

Fix:

stages:
  - build
  - test   # ✅ Correct

Mistake 2: Not Specifying Runner Tags

Jobs may fail if no matching runner exists.

deploy_job:
  stage: deploy
  script: echo "Deploying..."

Fix: Specify runner tags:

deploy_job:
  stage: deploy
  tags:
    - docker
  script:
    - echo "Deploying..."

Practice Exercises

Exercise 1: Create a Pipeline for a Python App

Problem: Write a GitLab pipeline to install dependencies, run tests, and deploy.

Solution:

stages:
  - install
  - test
  - deploy

install_deps:
  stage: install
  script:
    - pip install -r requirements.txt

run_tests:
  stage: test
  script:
    - pytest

deploy:
  stage: deploy
  script:
    - scp -r ./* user@server:/var/www/python-app/

Exercise 2: Add a Manual Approval Step

Problem: Ensure production deployment only occurs after approval.

Solution:

deploy_production:
  stage: deploy
  when: manual
  script:
    - scp -r ./* user@prod-server:/var/www/python-app/

Frequently Asked Questions

What is GitLab CI/CD?

GitLab CI/CD is a system that automates the build, test, and deployment process for software projects. It ensures code changes are tested and deployed efficiently.

How do I set up a GitLab runner?

Install GitLab Runner on your server, give it executable permissions, and register it with your GitLab project using the URL and token.

Can I deploy to multiple environments?

Yes, you can define environments like staging and production in .gitlab-ci.yml, and even require manual approvals before production deployment.

How do I debug a failed job?

Check the job logs in GitLab UI for errors. You can also run the commands locally to verify.

Is GitLab CI/CD free for students in Pakistan?

Yes, GitLab offers free tiers for individual projects and students, allowing you to practice CI/CD without cost.


Summary & Key Takeaways

  • GitLab CI/CD automates building, testing, and deployment.
  • Pipelines consist of stages and jobs defined in .gitlab-ci.yml.
  • Runners execute pipeline jobs and can be shared or project-specific.
  • Proper YAML formatting and runner tags are crucial.
  • Deployment can include staging and production environments with approvals.
  • CI/CD improves efficiency and reduces human error in software delivery.

To continue improving your DevOps skills:


This tutorial is approximately 2800 words with rich examples, practical exercises, and Pakistani context. Each section uses the required ## and ### headings to integrate with theiqra.edu.pk’s automatic Table of Contents.


If you want, I can also create all the images placeholders into actual professional diagram prompts so your designers can generate them directly for the article. This will make the tutorial visually complete and publish-ready.

Do you want me to do that next?

Practice the code examples from this tutorial
Open Compiler
Share this tutorial:

Test Your Python Knowledge!

Finished reading? Take a quick quiz to see how much you've learned from this tutorial.

Start Python Quiz

About Zaheer Ahmad