Unboxing GIT Fundamentals: Part 9 of 10

Anand
5 min readJun 11, 2024

--

Part 9: Integrating Git with CI/CD Pipelines on GitHub and GitLab

Welcome back to our Git learning series! In the previous part, we explored different Git workflows to manage your projects efficiently. In this installment, we will dive into integrating Git with Continuous Integration (CI) and Continuous Deployment (CD) pipelines using GitHub Actions and GitLab CI/CD. These integrations will automate your build, test, and deployment processes, improving efficiency and reliability.

1. Introduction to CI/CD

CI/CD is a method to frequently deliver apps to customers by introducing automation into the stages of app development. The main concepts are:

  • Continuous Integration (CI): The practice of merging all developer working copies to a shared mainline several times a day. Automated testing verifies each integration.
  • Continuous Deployment (CD): Extends CI by automatically deploying all code changes to a testing or production environment after the build stage.

2. Setting Up CI/CD with GitHub Actions

GitHub Actions is GitHub’s built-in CI/CD tool that allows you to automate workflows directly in your repository.

Steps to Set Up GitHub Actions:

  1. Create a Workflow File:
  • Create a .github/workflows directory in your repository.
  • Add a YAML file for your workflow (e.g., ci.yml).

2. Define Your Workflow:

name: CI  
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'

- name: Install dependencies
run: npm install

- name: Run tests
run: npm test

Example Workflow Explained:

  • name: The name of the workflow.
  • on: Specifies the events that trigger the workflow (e.g., push, pull_request).
  • jobs: Defines the jobs to run.
  • runs-on: Specifies the environment (e.g., ubuntu-latest).
  • steps: Steps to perform, such as checking out the code, setting up Node.js, installing dependencies, and running tests.

Benefits of GitHub Actions:

  • Integrated directly into GitHub.
  • Supports a wide range of actions and reusable workflows.
  • Easy to configure and customize.

3. Setting Up CI/CD with GitLab CI/CD

GitLab CI/CD is GitLab’s integrated tool to automate the software development lifecycle. It uses a .gitlab-ci.yml file to define the pipeline.

Steps to Set Up GitLab CI/CD:

  1. Create a .gitlab-ci.yml File:
  • Add a .gitlab-ci.yml file to the root of your repository.

2. Define Your Pipeline:

stages:
- build
- test
- deploy

build-job:
stage: build
script:
- echo "Compiling the code..."
- npm install
- npm run build

test-job:
stage: test
script:
- echo "Running tests..."
- npm test

deploy-job:
stage: deploy
script:
- echo "Deploying application..."
- npm run deploy
only:
- main

Example Pipeline Explained:

  • stages: Defines the stages of the pipeline.
  • build-job: A job that runs in the build stage, responsible for compiling the code and installing dependencies.
  • test-job: A job that runs in the test stage, responsible for running tests.
  • deploy-job: A job that runs in the deploy stage, responsible for deploying the application. It runs only on the main branch to ensure deployments are made from the production-ready code.

Benefits of GitLab CI/CD:

  • Deep integration with GitLab repositories.
  • Powerful and flexible configuration options.
  • Built-in support for multiple environments and deployments.

4. Best Practices for CI/CD

To get the most out of your CI/CD pipelines, follow these best practices:

  • Keep Pipelines Fast: Optimize your pipelines to run quickly by caching dependencies, parallelizing jobs, and running only necessary tests.
  • Fail Fast: Configure your pipelines to fail quickly when errors are detected. This helps identify issues early in the development process.
  • Use Environments: Leverage staging, testing, and production environments to manage deployments and ensure code quality at each stage.
  • Secure Secrets: Store sensitive data, like API keys and passwords, securely using secret management features provided by GitHub and GitLab.
  • Monitor and Improve: Continuously monitor the performance and reliability of your pipelines, and make improvements as needed.

5. Example: Setting Up a Full CI/CD Pipeline for a Node.js Application

Let’s walk through a full example of setting up a CI/CD pipeline for a Node.js application using both GitHub Actions and GitLab CI/CD.

GitHub Actions Example

  1. Create a .github/workflows/ci.yml File:
name: CI

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'

- name: Install dependencies
run: npm install

- name: Run tests
run: npm test

deploy:
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'

steps:
- uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'

- name: Install dependencies
run: npm install

- name: Deploy
env:
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
run: npm run deploy

2. Push the Workflow File to Your Repository:

git add .github/workflows/ci.yml
git commit -m "Add CI workflow"
git push origin main

GitLab CI/CD Example

  1. Create a .gitlab-ci.yml File:
stages:
- build
- test
- deploy

build-job:
stage: build
script:
- echo "Compiling the code..."
- npm install
- npm run build

test-job:
stage: test
script:
- echo "Running tests..."
- npm test

deploy-job:
stage: deploy
script:
- echo "Deploying application..."
- npm run deploy
only:
- main

2. Push the Pipeline File to Your Repository:

git add .gitlab-ci.yml
git commit -m "Add CI/CD pipeline"
git push origin main

Conclusion

In this part of our Git learning series, we’ve explored how to integrate Git with CI/CD pipelines using GitHub Actions and GitLab CI/CD. By automating your build, test, and deployment processes, you can improve efficiency and reliability in your development workflow.

In the final part of our series, Unboxing GIT fundamentals: Part 10 of 10, we will discuss strategies for debugging and troubleshooting common Git issues. Stay tuned, and happy learning!

Explore All Content of Unboxing GIT Fundamentals

  1. Part 1 of 10: Introduction to Git and Version Control
  2. Part 2 of 10: Basic Git Commands and Operations
  3. Part 3 of 10: Branching and Merging in Git
  4. Part 4 of 10: Remote Repositories in Git
  5. Part 5 of 10: Collaboration Workflows in Git
  6. Part 6 of 10: Advanced Git Commands and Techniques
  7. Part 7 of 10: Git Best Practices for Repository Management
  8. Part 8 of 10: Exploring Git Workflows — Git Flow, GitHub Flow, and GitLab Flow
  9. Part 9 of 10: Integrating Git with CI/CD Pipelines on GitHub and GitLab
  10. Part 10 of 10: Debugging and Troubleshooting Common Git Issues

About Author: An experienced IT professional worked in major IT companies for more than 20+ years as Solution Architect with core expertise on DevOps and Cloud. To get trained in DevOps from experts like Anand visit https://www.svsitsolutions.in or contact in WhatsApp

Other Interesting Blogs from the Author:

# 10 Surprising Facts About Git That You Probably Didn’t Know

# Hidden Secrets of Git : That You Never Knew

# Learning Teaser — Git in a Few Hours: A Comprehensive Training Guide

# Cinematic Experience with GIT : Lights, Camera, Action for Your Code!

--

--