Github Actions: The Easiest Way to Deploy Serverless Applications

Pamal Jayawickrama
3 min readJan 5, 2024

Introduction:

In the fast-paced world of software development, automating workflows is essential. This article explores a streamlined method for deploying serverless projects using GitHub Actions. If your code resides on GitHub, you can define a straightforward build step in a YAML configuration file at the root level of your repository. GitHub Actions will then automatically pick up the latest changes and initiate the deployment process. This approach eliminates the need for complex pipeline setups, making it particularly attractive for startups seeking efficient and automated workflows.

How to Implement:

Step 1: Setting up GitHub Actions Workflow

Start by creating a .github/workflows directory in your GitHub repository if it doesn't already exist.

Step 2: Managing Secrets

Securely manage access keys and secret access keys by storing them in GitHub Secrets. In this example, a specific environment (e.g., ‘prod’) is created for the production environment, and the necessary keys are stored securely.

Step 3: Creating the Workflow File

Inside the newly created directory, craft a file named github-action-demo.yml. This YAML file will define the steps of your GitHub Actions workflow.

Step 4: Utilizing Serverless Workflow

Visit the GitHub Marketplace and search for the Serverless workflow. Copy the provided YAML code and paste it into your github-action-demo.yml file.

name: Deploy product-serverless
on:
push:
branches:
- main

jobs:
productionDeploy:
name: production-deploy
runs-on: ubuntu-latest
environment:
name: prod
strategy:
matrix:
node-version: [18.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- name: Install Plugin and Deploy
uses: serverless/github-action@v3.2
with:
args: -c "serverless plugin install --name serverless-dotenv-plugin && serverless plugin install --name serverless-offline && serverless deploy --stage prod"
entrypoint: /bin/sh
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

This configuration file deploys a Serverless application to AWS automatically whenever code is pushed to the main branch.Secret keys related to the environment are retrieved from GitHub if the environment is specified in the YAML file. If plugins are used in the Serverless project, they should be included in the args section in the YAML file, as shown above.

Conclusion:

By adopting GitHub Actions for deploying your serverless projects, you can achieve a seamless and automated continuous delivery pipeline. This simplified approach allows startups and developers to focus more on building innovative features while GitHub Actions takes care of the deployment process. Streamline your workflow today and harness the power of automation with GitHub Actions.

If you have any questions or doubts regarding the code implementation, feel free to explore the GitHub repository associated with this project.

Thank You and Happy Coding !

--

--