Building a CI/CD Pipeline with GitHub Actions and Serverless Framework

Madhura Jayashanka
Enlear Academy
Published in
3 min readFeb 18, 2024

--

Building a CI/CD Pipeline with GitHub Actions and Serverless Framework

Imagine this: you push a code update, and within minutes, your serverless application is seamlessly deployed to production, ready to serve eager users. Sounds like a dream? With the dynamic duo of GitHub Actions and Serverless Framework, it’s a reality closer than you think.

This article delves into the heart of this powerful combination, guiding you through crafting a robust CI/CD pipeline that automates testing, deployment, and more. No more manual deployments, no more frantic scrambling after errors — just smooth sailing from commit to production.

Why Automate with GitHub Actions and Serverless Framework?

Unleash the Power of Efficiency: Forget tedious, error-prone manual deployments. This setup automates repetitive tasks, freeing you to focus on innovation.

  • Embrace Continuous Integration: With every push, your code undergoes rigorous testing, ensuring quality and preventing regressions before they reach production.
  • Enjoy Seamless Delivery: Streamline deployments to any environment — development, staging, or production — with a single command, reducing risk and accelerating releases.
  • Tap into Serverless Simplicity: The Serverless Framework simplifies serverless development, managing infrastructure and provisioning. GitHub Actions integrates seamlessly, orchestrating your entire workflow.

Setting the Stage: Your First CI/CD Pipeline

Now, let’s get our hands dirty! We’ll build a basic CI/CD pipeline that automatically deploys your serverless application to production on a push to the prod branch.

1. Prepare Your GitHub Repository:

  • Create a .github/workflows folder in your repository's root directory.
  • Within this folder, create a file named main.yml. This file defines your workflow.

2.YML Workflow

Paste the following code into your main.yml file, customizing it as needed:

main.yml

name: Deploy main branch

on:
workflow_dispatch:
push:
branches:
- prod #change this to your preffered branch

jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x]

steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
persist-credentials: false

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Npm install
run: npm install

- name: Serverless deploy
uses: serverless/github-action@master
with:
args: deploy
env:
LOG_LEVEL: debug
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

3. Configure Secrets:

If you don’t know how to get these credentials you can read my Getting Started with Serverless Framework and AWS Guide.

Head to your GitHub repository’s “Settings” -> “Secrets & variables” -> “Actions”. Create two secrets:

  • AWS_ACCESS_KEY_ID: Your AWS access key ID.
  • AWS_SECRET_ACCESS_KEY: Your AWS secret access key.

4. Push and see the Magic:

Push your updated code to the prod branch. Navigate to the "Actions" tab on your repository to see your workflow run. Once successful, your serverless application is live in production!

Beyond the Basics: Customizing Your Pipeline

The provided code offers a taste of what’s possible. Here’s how to elevate your workflow:

Trigger on Different Events: Instead of push events, trigger on pull requests, merged branches, or even scheduled intervals.

Run Unit and Integration Tests: Integrate testing frameworks like Jest into your workflow to ensure code quality before deployment.

Utilize Multiple Environments: Define staging and development environments for controlled deployments and rollbacks.

Pro Tip: Leverage environment variables to manage configuration specific to each environment.

Learn More

Contact Me:

Linkedin — https://www.linkedin.com/in/madhurajayashanka/

GitHub — https://github.com/madhurajayashanka

--

--