DEV Community

Cover image for Github Actions :- CI && CD
Sunny kumar Sinha
Sunny kumar Sinha

Posted on

Github Actions :- CI && CD

What is Github Actions ?
GitHub Actions is a CI/CD tool that automates workflows like building, testing, and deploying code directly from a GitHub repository based on events such as pushes or pull requests.

βš™οΈ Continuous Integration (CI)
Continuous Integration (CI) automatically builds and tests your code whenever you push changes to a repository, helping catch errors early.
πŸ‘‰ Example/Use Case: When you push code to GitHub, tests run automatically to ensure your new feature doesn’t break existing functionality.

πŸš€ Continuous Deployment (CD)

Continuous Deployment (CD) automatically deploys your code to production after it passes all tests and checks.
πŸ‘‰ Example/Use Case: After your code passes tests, it is automatically deployed to a live server like Vercel or Render without manual steps.


πŸ—οΈ GitHub Actions Architecture (Simple Explanation)

You can explain it like a flow πŸ‘‡

GitHub Actions works on an event-driven architecture where a trigger starts a workflow, which runs jobs made up of steps on a runner.

Overall workflow
Event β†’ Workflow β†’ Jobs β†’ Steps β†’ Runner

βš™οΈ 1. Event (Trigger)

This is what starts everything.

Examples:

push
pull_request
schedule

πŸ‘‰ Example:

When you push code to GitHub, it triggers the workflow.

πŸ“„ 2. Workflow
A YAML file inside .github/workflows/
Defines what should happen

πŸ‘‰ It contains:

Events
Jobs
🧩 3. Jobs
A workflow can have multiple jobs
Each job runs independently

πŸ‘‰ Example:

Job 1 β†’ Install dependencies
Job 2 β†’ Run tests
πŸ”Ή 4. Steps
Each job is made of steps
Steps are individual tasks

πŸ‘‰ Example:

Checkout code
Install Node.js
Run tests
πŸ–₯️ 5. Runner
A machine that executes the job

Types:

Ubuntu (most common)
Windows
macOS


πŸ’» Practical Example: Running Tests with GitHub Actions
To understand GitHub Actions better, let’s look at a simple real example.

In this project, I created a basic Python function that adds two numbers and wrote test cases to verify its correctness.
Here is addtion.py(python) file

def add(a, b):
    return a + b

def test_add():
    assert add(1, 2) == 3
    assert add(1, -1) == 0
Enter fullscreen mode Exit fullscreen mode

Here is the GitHub Actions workflow file:

name: My First GitHub Actions

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        python-version: [3.8, 3.9]

    steps:
    - uses: actions/checkout@v3

    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python-version }}

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install pytest

    - name: Run tests
      run: |
        cd src
        python -m pytest addition.py
Enter fullscreen mode Exit fullscreen mode

πŸ“ Where to Place the Workflow File

For GitHub Actions to work, the YAML file must be placed in this path:

.github/workflows/first-actions.yaml

πŸ‘‰ Explanation:

.github/ β†’ Special GitHub folder
workflows/ β†’ Stores all workflow files
.yaml file β†’ Your automation script

⚠️ If you place the file anywhere else, GitHub Actions will NOT run.


I actually implemented this in one of my projects to understand it better.
You can take a look at the full setup here:
πŸ‘‰ https://github.com/Sunny-2610/github_actions

Top comments (0)