Setting Up GitHub Actions for Elastic Beanstalk Deployment on Commits to Dev/Main Branches
In this tutorial, we'll walk you through how to set up GitHub Actions to automatically deploy your web application to AWS Elastic Beanstalk whenever there are new commits to the dev
or main
branches.
Prerequisites
- An AWS account with Elastic Beanstalk and S3 bucket configured.
- GitHub repository containing your web application.
- AWS Access Key ID and Secret Access Key with appropriate permissions for deploying to Elastic Beanstalk.
Step 1: Define Environment Variables in GitHub
Go to your GitHub repository, then click on "Settings" > "Secrets" and add the following secrets:
AWS_ACCESS_KEY_ID
: Your AWS access key IDAWS_SECRET_ACCESS_KEY
: Your AWS secret access key
Also, ensure you define the following environment variables:
BUCKET_NAME
: The name of your S3 bucket where the application will be uploaded.DEV_APP_NAME
: The name of the Elastic Beanstalk application for the development environment.MAIN_APP_NAME
: The name of the Elastic Beanstalk application for the main environment.DEV_ENV_NAME
: The name of the Elastic Beanstalk environment for the development deployment.MAIN_ENV_NAME
: The name of the Elastic Beanstalk environment for the main deployment.
Step 2: Create GitHub Actions Workflow File
Create a file in your repository at /.github/workflows/deploy.yml
and paste the following content:
name: Deploy to Elastic Beanstalk
on:
push:
branches:
- main
- dev
env:
BUCKET_NAME: your-bucket-name
DEV_APP_NAME: your-dev-app-name
MAIN_APP_NAME: your-main-app-name
DEV_ENV_NAME: your-dev-environment-name
MAIN_ENV_NAME: your-main-environment-name
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '16'
- name: Install dependencies
run: npm install
working-directory: myapp
- name: Build Next.js application
run: npm run build
working-directory: myapp
- name: Zip the application
run: zip -r application.zip . -x '*.git*' 'node_modules/*'
working-directory: myapp
- name: Move the ZIP file to root directory
run: mv myapp/application.zip ./
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-west-2 # Change to your desired region
- name: Deploy to Dev Elastic Beanstalk on commit to dev branch
if: github.ref == 'refs/heads/dev'
run: |
aws s3 cp application.zip s3://$BUCKET_NAME/dev/application.zip
aws elasticbeanstalk create-application-version --application-name $DEV_APP_NAME --version-label v1 --source-bundle S3Bucket="$BUCKET_NAME",S3Key="dev/application.zip"
aws elasticbeanstalk update-environment --environment-name $DEV_ENV_NAME --version-label v1
- name: Deploy to Main Elastic Beanstalk on commit to main branch
if: github.ref == 'refs/heads/main'
run: |
aws s3 cp application.zip s3://$BUCKET_NAME/main/application.zip
aws elasticbeanstalk create-application-version --application-name $MAIN_APP_NAME --version-label v1 --source-bundle S3Bucket="$BUCKET_NAME",S3Key="main/application.zip"
aws elasticbeanstalk update-environment --environment-name $MAIN_ENV_NAME --version-label v1
Make sure to update the environment variables with the correct values for your Elastic Beanstalk and S3 configurations.
Step 3: Define the Deployment Steps
The YAML file already contains all the necessary steps for deploying your application to Elastic Beanstalk, including:
- Checking out the code from the repository
- Setting up Node.js
- Installing dependencies
- Building the Next.js application
- Zipping the application
- Moving the ZIP file to the root directory
- Configuring AWS credentials
- Deploying to the development or main environment based on the branch that was pushed
You will need to make sure the working directory (myapp
in this example) matches your repository's structure.
Step 4: Push Changes to Trigger Deployment
Once you have committed and pushed the deploy.yml
file to your repository, GitHub Actions will automatically deploy your application to the appropriate Elastic Beanstalk environment whenever you push to the dev
or main
branches.
Conclusion
By leveraging GitHub Actions and AWS Elastic Beanstalk, you can automate the deployment of your web application and ensure a seamless, continuous delivery process. Make sure that the names of your applications and environments in Elastic Beanstalk exactly match the variables in your GitHub Actions workflow to avoid any errors during deployment.