CI/CD Pipeline Setup: Tutorial

author

By Freecoderteam

Aug 27, 2025

1

image

CI/CD Pipeline Setup: A Comprehensive Tutorial

Continuous Integration (CI) and Continuous Deployment (CD) are essential practices in modern software development, enabling teams to deliver high-quality software faster and more reliably. In this tutorial, we'll walk through the process of setting up a CI/CD pipeline step by step. We'll cover the basics, best practices, and provide practical examples to help you implement a robust CI/CD workflow.

Table of Contents


Introduction to CI/CD

CI/CD is a software development practice that automates the integration, testing, and deployment of code changes. Here's a quick overview of the key components:

  • Continuous Integration (CI): Developers frequently merge their code changes into a central repository, and automated builds and tests are run to catch integration issues early.
  • Continuous Deployment (CD): Once the code passes all tests, it is automatically deployed to production (or a staging environment).

CI/CD pipelines help reduce manual errors, improve code quality, and accelerate the delivery of new features.


Prerequisites

Before setting up a CI/CD pipeline, ensure you have the following:

  1. Version Control System (VCS): Use a tool like Git to manage your codebase.
  2. CI/CD Tool: Choose a platform like GitHub Actions, Jenkins, GitLab CI, or CircleCI.
  3. Automated Tests: Unit tests, integration tests, and end-to-end tests are essential for catching bugs early.
  4. Deployment Environment: A staging or production environment where your application will be deployed.

Step 1: Choose a CI/CD Tool

There are several popular CI/CD tools available:

  • GitHub Actions: Built into GitHub, ideal for open-source projects.
  • Jenkins: Highly customizable and widely used in enterprise environments.
  • GitLab CI: Integrated with GitLab, offering a seamless experience.
  • CircleCI: Known for its simplicity and speed, great for small to medium teams.

For this tutorial, we'll use GitHub Actions as it's free for open-source projects and easy to set up.


Step 2: Set Up Your Repository

  1. Create a Repository: If you don't already have one, create a GitHub repository for your project.
  2. Initialize Your Code: Add your codebase to the repository. Ensure you have a README.md and a .gitignore file.
  3. Add Tests: Write unit tests and integration tests for your application. For example, if you're using Python, you might use pytest.

Step 3: Define Your CI/CD Workflow

CI/CD workflows are defined using configuration files. For GitHub Actions, this is done using a workflow file in the .github/workflows directory.

CI Workflow Example

Here's a simple CI workflow that runs tests whenever code is pushed to the main branch:

# .github/workflows/ci.yml
name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.9'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: Run tests
        run: |
          pytest

CD Workflow Example

Once the tests pass, you can set up a deployment workflow. Here's an example that deploys to a server using SSH:

# .github/workflows/cd.yml
name: CD

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up SSH key
        uses: appleboy/ssh-action@v1
        with:
          host: your-server-ip
          username: your-username
          key: ${{ secrets.SSH_PRIVATE_KEY }}

      - name: Deploy to server
        run: |
          ssh your-username@your-server-ip "cd /path/to/your/app && git pull && npm install && npm start"

Step 4: Implement Automated Testing

Automated testing is a critical part of CI/CD. Here's how you can set it up:

  1. Unit Tests: Write tests for individual components of your application. For example, in Python:

    # tests/test_example.py
    def test_addition():
        assert 1 + 1 == 2
    
  2. Integration Tests: Test how different components work together. For example, in JavaScript:

    // tests/integration.test.js
    const { add } = require('./app');
    
    test('adds 1 + 1 to equal 2', () => {
        expect(add(1, 1)).toBe(2);
    });
    
  3. End-to-End Tests: Use tools like Selenium or Cypress to test the entire application flow.


Step 5: Configure Deployment

Deployment can vary based on your infrastructure. Here are some common approaches:

  • Docker: Use Docker to containerize your application and deploy it to a container orchestration platform like Kubernetes.
  • Server: Use SSH to deploy directly to a server.
  • Cloud Services: Deploy to cloud platforms like AWS, Azure, or Google Cloud.

For example, if you're deploying a Node.js application to a server, you might use the following workflow:

# .github/workflows/cd.yml
name: CD

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

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

      - name: Install dependencies
        run: npm install

      - name: Build application
        run: npm run build

      - name: Deploy to server
        uses: appleboy/ssh-action@v1
        with:
          host: your-server-ip
          username: your-username
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          script: |
            cd /path/to/your/app
            git pull
            npm install
            npm run build
            pm2 restart app

Best Practices for CI/CD

  1. Keep Pipelines Fast: Optimize your tests and build processes to reduce pipeline execution time.
  2. Parallelize Tasks: Run tests in parallel to speed up the pipeline.
  3. Use Secrets Safely: Store sensitive information like API keys and SSH keys as repository secrets.
  4. Monitor Pipeline Health: Use tools like Prometheus or Datadog to monitor pipeline performance.
  5. Regularly Review and Update: Keep your pipelines up to date with the latest tools and practices.

Conclusion

Setting up a CI/CD pipeline is a powerful way to improve your software development process. By automating integration, testing, and deployment, you can deliver high-quality software faster and with fewer errors. Start by choosing a CI/CD tool, setting up your repository, defining your workflows, implementing automated tests, and configuring deployment.

Remember, CI/CD is an ongoing process. Continuously refine your pipelines based on feedback and new requirements to ensure they remain effective and efficient.

Happy coding! 🚀


If you have any questions or need further assistance, feel free to reach out!

Subscribe to Receive Future Updates

Stay informed about our latest updates, services, and special offers. Subscribe now to receive valuable insights and news directly to your inbox.

No spam guaranteed, So please don’t send any spam mail.