If you are still manually running tests and deploying your code from your local terminal, you are wasting valuable time.
When I first started diving into DevOps and Cloud engineering, the concept of CI/CD (Continuous Integration / Continuous Deployment) felt incredibly intimidating. I thought I needed a complex Jenkins server or a massive AWS architecture just to automate my workflows.
It turns out, if your code is already on GitHub, you can build your first automated pipeline in under 10 minutes using GitHub Actions.
Today, I’ll show you exactly how to set up a basic workflow that automatically tests your code every time you push to your repository.
Prerequisites:
- A GitHub account
- A basic understanding of Git (git add, git commit, git push)
- A sample project (we will use a simple Node.js project for this example, but the concepts apply to any language).
Step 1: Create Your Workflow File
GitHub Actions looks for a very specific folder structure in your repository to know what to run.
In the root directory of your project, create a new folder called .github, and inside that, create a folder called workflows. Finally, create a YAML file inside it. You can name it whatever you want, but ci.yml is standard.
Your path should look like this: .github/workflows/ci.yml
Step 2: Write the YAML Configuration
YAML is the language of DevOps. It relies heavily on indentation, so make sure your spacing is exact!
Open your ci.yml file and paste the following code:
name: Node.js CI Pipeline
# 1. When should this workflow run?
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
# 2. What jobs should it execute?
jobs:
build-and-test:
# We need a virtual machine to run our code
runs-on: ubuntu-latest
# 3. What are the exact steps?
steps:
# Step A: Check out the code from our repository
- name: Checkout code
uses: actions/checkout@v3
# Step B: Set up the Node.js environment
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18.x'
# Step C: Install dependencies
- name: Install Dependencies
run: npm ci
# Step D: Run our tests
- name: Run Tests
run: npm test
Breaking Down the Code:
on: This tells GitHub exactly when to trigger the pipeline. In our case, it runs every time someone pushes code or opens a pull request to the main branch.runs-on: GitHub provisions a fresh, temporary Ubuntu server (a runner) specifically to execute your commands.steps: This is the chronological list of commands. We check out the code, install Node.js, install our npm packages, and finally, run the tests.
Step 3: Push and Watch the Magic
Save your file, commit it, and push it to GitHub:
git add .github/workflows/ci.yml
git commit -m "chore: add github actions CI pipeline"
git push origin main
Now, navigate to your repository on GitHub and click the "Actions" tab at the top.
You will see your workflow running in real-time! If your tests pass, you will get a satisfying green checkmark ✅. If they fail, you will get a red X and a log detailing exactly what broke, preventing bad code from making it to production.
The Takeaway
Congratulations! You just implemented Continuous Integration.
This is the foundational building block of modern DevOps. From here, you can expand this exact same file to automatically push Docker images to AWS, trigger serverless deployments, or send a Slack message when a build fails.
Automation is about letting the machines do the repetitive work so you can focus on building cool things.
I'll be sharing more DevOps and Cloud tutorials here as I build and learn. If this tutorial saved you some time, you can ☕ buy me a coffee here to support my work!
Top comments (0)