Tutorial: Creating a GitHub CI/CD Action to deploy a Next.js Docker application to AWS EC2
Continuous Integration and Continuous Deployment (CI/CD) are essential practices for modern software development. By setting up GitHub Actions to automate your CI/CD pipeline, you can ensure that your code is tested and deployed efficiently. This guide will walk you through setting up GitHub Actions for deploying a Next.js project to an AWS EC2 instance.
Prerequisites
Before we begin, ensure you have the following:
- A GitHub account
- An AWS account
- An EC2 instance running on AWS
- Nodejs and NPM Installed
- Yarn installed (for faster and more secure deployments)
- Docker installed
- Docker-compose installed
- Basic knowledge of Git, Docker, and Next.js
Step 1: Create a Next.js Application
First, create a new Next.js application locally using Yarn.
yarn create next-app
You can test the application locally to make sure everything is working:
yarn dev
Step 2: Dockerize the Next.js Application
Create a Dockerfile
in the root of your project:
Build and run the Docker container locally to ensure it's working:
# Build a docker image. By using '.', you're telling Docker to look in the current directory for the Dockerfile
docker build -t nextjs-cicd-sample .
# Run docker and mapping port 3000 from the container to port 3000 on your host machine
docker run -p 3000:3000 nextjs-cicd-sample
Step 3: Create a Docker-Compose YAML File
Create a docker-compose.yml
file at root user:
Step 4: Upload your application to GitHub
You can either use Github CLI or install GitHub Desktop to upload your application to GitHub
If using Github CLI:
# Initialize a new Git repository in the current directory
git init
# Stage all files in the current directory for commit
git add .
# Create the first commit with the message "Initial commit"
git commit -m "Initial commit"
# Rename the current branch to 'main'
git branch -M main
# Add a remote named 'origin' pointing to your GitHub repository
git remote add origin <YOUR_GITHUB_REPO_URL>
# Push the 'main' branch to the 'origin' remote and set it as the upstream branch
git push -u origin main
Upload using GitHub Desktop:
Add local repository to your github account using GitHub Desktop:
Publish repository to GitHub:
Step 5: Set Up GitHub Actions for CI/CD
- Create a
.github/workflows/deploy.yml
file in your project
- Store GitHub Secrets
In your GitHub repository, go to Settings > Security > Secrets and variables
and add the following secrets:
SSH_KEY
: The private SSH key to connect to your EC2 instance (use the contents of your.pem
file)SSH_HOST
: The public IP address of your EC2 instanceSSH_USER
: The username for your EC2 instance (usuallyubuntu
for Amazon Ubuntu)
- Clone your repository using SSH
- Install Git (if not already installed):
sudo apt update
sudo apt install git
- Generate an SSH key (if you don't have one):
ssh-keygen -t ed25519 -C "[email protected]"
If you're using an older system that doesn’t support the ed25519
algorithm, you can use rsa
:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
- To Add the SSH key to your GitHub account:
- Copy the SSH key to your clipboard:
cat ~/.ssh/id_ed25519.pub
(Orcat ~/.ssh/id_rsa.pub
forrsa
.)
- Copy the SSH key to your clipboard:
- Log in to your GitHub account.
- Navigate to Settings > SSH and GPG keys.
- Click New SSH key, provide a descriptive title, and paste your key into the "Key" field.
- Click Add SSH key.
- Test the SSH connection:
ssh -T
[email protected]
You should see a message like:
Step 6: Push your code to GitHub main branch
Finally, push your changes to the main
branch of your GitHub repository. The action defined in deploy.yml
will automatically trigger and deploy your code to the EC2 instance.
Step 7: Verify the Deployment
After pushing your changes, GitHub Actions will start running the workflow. You can monitor the progress in the Actions tab of your repository on GitHub.
- Check the Action Logs
Navigate to theActions
tab in your repository and select the latest workflow run. You can view the logs for each step to ensure everything is running smoothly. - Verify on Your Server
Once the workflow completes, verify that your application is running on your EC2 instance by visiting the public IP address of your EC2 instance in a browser.
Conclusion
By following these steps, you've set up a robust CI/CD pipeline using GitHub Actions and AWS EC2. This setup ensures that your Next.js application is automatically deployed whenever you push changes to the main branch, streamlining your development workflow and reducing the risk of human error.
With this automation in place, you can focus more on developing features and less on the intricacies of deployment. Happy coding!
Do you enjoy this blog post?