How to deploy a serverless application using Github Actions

This article will explain how to deploy a serverless application using GitHub Actions.

GitHub repository

  1. Create a new repository on GitHub, or choose an existing repository that you want to set up the action for.

  2. In the .github/workflows directory of your repository, create a new file for your action. You can name this file anything you like, but it should have the .yml file extension.

  3. In the action file, define a new job with the name and on properties. The on property specifies when the job should be run, such as when a push event is made to some branch of the repository or when a pull request is made.

  4. In the job, define a steps section that contains one or more steps. Each step is an individual task that the job should perform.

  5. For the first step, use the uses property to specify an action. First, we are using an action named actions/checkout@v2. This is an action provided by GitHub that will check out your repository onto the runner, so that it can be built and tested.

  6. Then, we are using another action named serverless/github-action@v3.1. This is an action provided by the Serverless team that wraps the Serverless Framework to enable common Serverless commands.

  7. For subsequent steps, you can use the run property to run a command using the Serverless Framework. For example, you can use run: serverless deploy to deploy your serverless application to the cloud.

  8. Set up any necessary environment variables that the Serverless Framework needs. For example, you may need to set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables to provide the action with access to your AWS account.

  9. Commit and push your changes to the repository. This will trigger the GitHub Action to run.

Example file

Here is an example action file that demonstrates these steps:

Copy codename: Deploy Serverless Application

on:
  push:
    branches: [ main ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Deploy Serverless Application
      uses: serverless/github-action@v3.1
      with:
        args: deploy
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

This action will run when a push event is made to the main branch, and it will deploy the serverless application using the serverless deploy command.

I hope this helps! Let me know if you have any questions.

Did you find this article valuable?

Support Sonia Manoubi by becoming a sponsor. Any amount is appreciated!