GitHub Actions: A Beginner's Guide
Learn how to automate your workflows and streamline your development process with GitHub Actions. This beginner-friendly guide covers the basics and best practices.
π GitHub Actions for Beginners
GitHub Actions is a handy tool for automating tasks in your GitHub projects. π οΈ It helps you create workflows with simple YAML files. π These workflows run tasks like building, testing, and deploying code.
You can set triggers to start these tasks. For example, they can run when you push code or create a pull request. This automation saves you time and reduces errors. β It also keeps your code consistent and reliable. π
β οΈ Challenges Without GitHub Actions
- π₯ Inconsistent Code Quality: When many people work together, keeping everyone on the same page with coding standards can be hard. Some might forget formatting rules, making the code look messy.
- β³ Time-Consuming Manual Checks: Checking code by hand takes a lot of time and slows everything down. Plus, itβs easy to miss mistakes when doing things over and over.
- π Difficulty in Testing Across Multiple Languages: Different programming languages often have their own testing tools. This can create headaches when trying to merge code from different sources.
π How GitHub Actions Can Help
Using GitHub Actions can solve these problems effectively:
- π Consistent Code Quality: GitHub Actions automatically checks coding styles and rules for every pull request, making sure everything meets the standards before merging.
- π Automated Testing: Set up workflows to run tests automatically whenever code is updated. This helps catch any issues early, saving you time and effort later.
- π¦ Unified Workflows Across Languages: You can create specific workflows for each programming language in the same repository. This keeps everything organized and makes teamwork easier.
π Examples of Open-Source Projects using CI/CD
Below are two of the most popular projects that effectively utilize GitHub Actions for continuous integration and delivery.
π Node.js: Continuous Testing
- β¨ Overview: Node.js runs automated tests for each pull request using GitHub Actions. This checks that new contributions donβt break existing functionality.
- π‘οΈ Impact: Developers can merge code confidently, knowing that the automated tests ensure high code quality.
π Django: Cross-Version Compatibility
- π Overview: Django employs GitHub Actions to execute its test suite on multiple Python versions for every code push.
- β Impact: This guarantees that all contributions work seamlessly across supported environments, reducing bugs and increasing reliability.
π Key Advantages of CI/CD in Open Source
π Understanding The Automation Framework
Letβs dive into the framework itself. Below is a simple repository structure needed to trigger an action.
ποΈ Complete Repository Structure
1
2
3
4
5
6
ποΈ your-repo/
βββ .github/
β βββworkflows/
β βββmain.yml
βββ src/
βββmain.py
Below shows simple yaml file for example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
name: CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Run tests
run: python -m unittest discover
For further details on YAML syntax, you can refer to the YAML documentation.
βοΈ Understanding the Workflow
π Workflow Name
- Description: This defines the workflow as βCIβ (Continuous Integration). It helps in automating the testing and integration of code changes to ensure stability.
π Trigger Events
- Details: The workflow runs on two key events:
push
: Activates when new code is pushed to the repository.pull_request
: Activates when a pull request is opened or updated, allowing for early feedback on code changes.
π οΈ Job Definition
- Build Job: This job runs on the latest Ubuntu environment. It provides a consistent and up-to-date platform for testing.
ποΈ Steps in the Job
Checkout Code:
- Function: Uses
actions/checkout@v2
to access the repositoryβs files. - Example: Allows the workflow to work with the latest code changes before executing tests.
- Function: Uses
Run Tests:
- Function: Executes unit tests using the command
python -m unittest discover
. - Importance: Ensures that any new code changes do not break existing functionality, maintaining high code quality.
- Function: Executes unit tests using the command
π Benefits
- Automating these processes helps teams identify issues early, promotes collaboration, and ensures consistent code quality across contributions.
Here is the pictorial representation
graph TD;
A([π Workflow: CI]):::workflow --> B((π― Trigger Events)):::trigger
B --> C([π Events: Push & Pull Request]):::event
C --> D([π Job: Build on Ubuntu]):::job
D --> E((π Steps)):::steps
E --> F([π Checkout Code]):::checkout
F --> G([π§ͺ Run Tests]):::tests
classDef workflow fill:#f39c12,stroke:#e67e22,stroke-width:3px,stroke-dasharray:5;
classDef trigger fill:#3498db,stroke:#2980b9,stroke-width:2px;
classDef event fill:#e74c3c,stroke:#c0392b,stroke-width:2px,stroke-dasharray:3;
classDef job fill:#27ae60,stroke:#16a085,stroke-width:2px;
classDef steps fill:#8e44ad,stroke:#6c3483,stroke-width:3px;
classDef checkout fill:#f1c40f,stroke:#d4ac0d,stroke-width:2px;
classDef tests fill:#9b59b6,stroke:#8e44ad,stroke-width:2px;
π Practical Section: Letβs Get Our Hands Dirty
Open your terminal.
Clone an empty GitHub repository or create a new folder:
1 2 3 4 5
git clone https://github.com/yourusername/your-repo.git cd your-repo # or create a new folder mkdir your-repo cd your-repo
Create the folder structure:.
1
mkdir -p .github/workflows src
Create the
main.yml
workflow file::.1
vi .github/workflows/main.yml
Add the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
name: CI on: [push, pull_request] jobs: build: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Run script run: python src/main.py
Save and exit. π
Create a simple Python script
main.py
:1
vi src/main.py
Add the following content:
1 2 3 4 5
def add(a, b): return a + b result = add(3, 4) print(f"The result of adding 3 and 4 is: {result}")
Save and exit. π
Create the
verify.py
script:1
vi src/verify.py
and paste the below content in it.
1 2 3 4 5 6 7 8 9 10
from main import add print("Verifying the add function...") expected = 7 actual = add(3, 4) if actual == expected: print("Test passed! π") else: print("Test failed! β")
Save and exit. πΎ
Stage and Commit and push Your Changes:
1 2 3
git add . git commit -m "Initial commit with workflow and Python files" git push origin main
Where to Look for Actions Running:
Go to your GitHub repository page.
Click on the βActionsβ tab.
This will show the status of your workflows triggered by the push! π
π Useful Resources for Your CI/CD Journey
π GitHub Actions Documentation:
A comprehensive guide to setting up and mastering GitHub Actions π, including workflows, triggers, and more.π YAML Syntax:
A resource to help you write structured.yml
files for your workflows with ease.- π GitHub Actions: Checkout:
Learn how to use theactions/checkout
to access your repoβs code in workflows. - π‘ Git Commands Cheat Sheet:
A quick and handy reference for essential Git commands.
π₯ Recap of Our CI/CD Journey
We walked through the process of setting up GitHub Actions from scratch, explored key elements like workflow triggers, jobs, and steps, and even got our hands dirty by creating a simple Python verification script. We also pushed changes to GitHub and learned where to check the status of actions.
π― Final Thoughts
Embracing CI/CD streamlines your workflow, ensuring quality code and faster releases. π Now itβs your turn to implement this in your projects!
π¨οΈ What did you think?
Let us know in the comments below! β¬οΈ