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
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
π 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)