Skip to main content
← Back to workflows

How to Deploy with Serverless GitHub Action

serverless/github-action -
GitHub Action
v3.2.0
GitHub Org's stars
Contributors
Categories
CICube banner
The GitHub Actions dashboard helps you spot bottlenecks and optimize your CI pipeline with actionable insights.
Usage
name: Deploy Master Branch with Serverless
on:
push:
branches:
- master

jobs:
deploy:
name: Deploy to Serverless
runs-on: ubuntu-latest
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: Serverless deploy
uses: serverless/github-[email protected]
with:
args: deploy
env:
SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}
# or if using AWS credentials directly
# AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
# AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

serverless-action logo

Deploy with Serverless

A GitHub Action to deploy Serverless applications to AWS using Serverless Framework.

What is Serverless GitHub Action?

This GitHub Action wraps the Serverless Framework to enable deploying your serverless applications using GitHub workflows. It supports AWS credentials for direct deployment and can work with serverless plugins.

How to Specify the Node.js Version

To set the Node.js version in your workflow, use the setup-node action.

with:
node-version: 18.x

How to Deploy with Serverless

To deploy your application using Serverless, provide the necessary args for the Serverless command. For deployment, you can use the following setup:

with:
args: deploy
env:
SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}

If you're using AWS credentials directly, you can specify them in the env section.

env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

How to Use Serverless Plugins

If you need to install a Serverless plugin before deployment, you can modify your workflow like this:

with:
args: -c "serverless plugin install --name <plugin-name> && serverless deploy"
entrypoint: /bin/sh

How to Fix "This command can only be run in a Serverless service directory" Error

If you encounter the "This command can only be run in a Serverless service directory" error, update your workflow to navigate to the correct directory before deploying:

with:
args: -c "cd ./<your-dir> && serverless deploy"
entrypoint: /bin/sh

How to Use Different Serverless Versions

To use Serverless version 1 or 2 instead of the latest, modify the action:

uses: serverless/github-action@v1  # for version 1

or

uses: serverless/github-action@v2  # for version 2

Example Workflow with Serverless v3 Deployment

name: Deploy master branch
on:
push:
branches:
- master

jobs:
deploy:
name: Deploy to Serverless
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js 18.x
uses: actions/setup-node@v3
with:
node-version: 18.x
- run: npm ci
- name: Serverless deploy
uses: serverless/github-[email protected]
with:
args: deploy
env:
SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}

Monitoring GitHub Actions Workflows

CICube is a GitHub Actions monitoring tool that provides you with detailed insights into your workflows to further optimize your CI/CD pipeline. With CICube, you will be able to track your workflow runs, understand where the bottlenecks are, and tease out the best from your build times. Go to cicube.io now and create a free account to better optimize your GitHub Actions workflows!

CICube GitHub Actions Workflow Duration Monitoring