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)