Last active
October 25, 2024 12:29
-
-
Save maxkostinevich/9672966565aeb749d41e49673be4e7fa to your computer and use it in GitHub Desktop.
Github Actions - Deploy Serverless Framework (AWS)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Github Actions for Serverless Framework | |
# | |
# Create AWS_KEY and AWS_SECRET secrets in Github repository settings | |
# If you're using env.yml file, store its content as ENV Github secret | |
# | |
# Master branch will be deployed as DEV and every new tag starting with "v**" (e.g. v1.0, v1.2, v2.0, etc) will be deployed as PROD | |
# | |
# Learn more: https://maxkostinevich.com/blog/how-to-deploy-serverless-applications-using-github-actions/ | |
# | |
name: Deploy Dev | |
on: | |
push: | |
branches: | |
- master | |
jobs: | |
deploy-dev: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v1 | |
- uses: actions/setup-node@v1 | |
with: | |
node-version: '10.x' | |
- name: Install Serverless Framework | |
run: npm install -g serverless | |
- name: Serverless AWS authentication | |
run: sls config credentials --provider aws --key ${{ secrets.AWS_KEY }} --secret ${{ secrets.AWS_SECRET }} | |
- name: Create env file | |
run: | # cp sample.env.yml env.yml | |
cat > env.yml << EOF | |
${{ secrets.ENV }} | |
EOF | |
- name: Install NPM dependencies | |
run: npm install | |
# Optional | |
#- name: Build assets | |
# run: npm run assets-dev | |
- name: Deploy Lambda functions | |
run: sls deploy -s dev | |
# Optional (to use with serverless-finch serverless plugin) | |
#- name: Deploy assets to S3 | |
# run: sls client deploy --no-delete-contents --no-confirm -s dev | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Github Actions for Serverless Framework | |
# | |
# Create AWS_KEY and AWS_SECRET secrets in Github repository settings | |
# If you're using env.yml file, store its content as ENV Github secret | |
# | |
# Master branch will be deployed as DEV and every new tag starting with "v**" (e.g. v1.0, v1.2, v2.0, etc) will be deployed as PROD | |
# | |
# Learn more: https://maxkostinevich.com/blog/how-to-deploy-serverless-applications-using-github-actions/ | |
# | |
name: Deploy Prod | |
on: | |
push: | |
tags: # Deploy tag (e.g. v1.0) to production | |
- 'v**' | |
jobs: | |
deploy-prod: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v1 | |
- uses: actions/setup-node@v1 | |
with: | |
node-version: '10.x' | |
- name: Install Serverless Framework | |
run: npm install -g serverless | |
- name: Serverless AWS authentication | |
run: sls config credentials --provider aws --key ${{ secrets.AWS_KEY }} --secret ${{ secrets.AWS_SECRET }} | |
- name: Create env file | |
run: | # cp sample.env.yml env.yml | |
cat > env.yml << EOF | |
${{ secrets.ENV }} | |
EOF | |
- name: Install NPM dependencies | |
run: npm install | |
# Optional | |
#- name: Build assets | |
# run: npm run assets-prod | |
- name: Deploy Lambda functions | |
run: sls deploy -s prod | |
# Optional (to use with serverless-finch serverless plugin) | |
#- name: Deploy assets to S3 | |
# run: sls client deploy --no-delete-contents --no-confirm -s prod |
Concur with above. Had same issue and resolved it by moving npm i up the chain to just before aws auth.
solid solution. thanks
Try the full flag name --stage
instead of -s
. I was having trouble getting different stages to build properly with the short version.
exactly the thing i was looking for. thanks.
Made some small tweaks to get it to work for me. Notably, using pnpm
in a monorepo-style system. This workaround is necessary because the Serverless team's official repo (https://github.com/serverless/github-action) doesn't seem very well maintained, and it's not a very complex operation anyway.
name: Deploy `main` Branch
on:
push:
branches:
- main
paths:
- apps/**
- lib/**
- .github/workflows/**
jobs:
deploy:
name: deploy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 8
- uses: actions/setup-node@v3
with:
node-version: 18
cache: pnpm
- run: pnpm i
- run: pnpm i -g serverless
- run: sls config credentials --provider aws --key ${{ secrets.AWS_ACCESS_KEY_ID }} --secret ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- run: sls deploy --stage prod
working-directory: apps/core/server
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this, works great!
The only thing I had to adjust was to move
npm install
before authenticating serverless with AWS. This is due to me using several serverless plugins and it didn't like me not having them installed.