https://github.com/civicdatalab/opub-deploy
Guide to set up a Next.js project to automatically deploy to an EC2 instance using GitHub Actions
Science Score: 13.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
○.zenodo.json file
-
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (10.7%) to scientific vocabulary
Keywords
Repository
Guide to set up a Next.js project to automatically deploy to an EC2 instance using GitHub Actions
Basic Info
Statistics
- Stars: 0
- Watchers: 5
- Forks: 0
- Open Issues: 0
- Releases: 0
Topics
Metadata Files
README.md
NextJS Auto Deployment
This guide will help you set up a Next.js project to automatically deploy to an EC2 instance using GitHub Actions.
Quick Start
Use the following if you already have a project deployed on EC2. Otherwise, follow the detailed guide.
PM2 Setup
```bash // EC2 sudo apt update npm install pm2 -g
sudo ln -s "$NVMDIR/versions/node/$(nvm version)/bin/node" "/usr/local/bin/node" sudo ln -s "$NVMDIR/versions/node/$(nvm version)/bin/npm" "/usr/local/bin/npm" sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/pm2" "/usr/local/bin/pm2"
// project directory pm2 start npm -n deploy -- start ```
for custom port
pm2 start npm -n deploy -- start -- -p 3001
Adding SSH Key to GitHub
- Generate a SSH key in EC2 (skip if already have one):
bash
ssh-keygen -m PEM
- Move the public key to authorized_keys:
bash
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
- Copy the private key content:
bash
cat ~/.ssh/id_rsa
// Copy the content manually
- In GitHub, navigate to Repository > Settings > Secrets and variables > Actions and create the following three secrets:
EC2_HOST: Public IP of your EC2 instanceEC2_USERNAME: Username to SSH into EC2EC2_PRIVATE_KEY: Paste the private key content generated earlier.
Action Setup
Add the following code to .github/workflows/deploy.yml in your project:
```yaml name: Deploy App to EC2
on: push: branches: - main
jobs: deploy: runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install dependencies
run: npm install
- name: Build
run: npm run build
- name: Rename .next to .next2
run: mv .next .next2
- name: Send .next2 to EC2
uses: appleboy/scp-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
source: .next2
target: opub-deploy
- name: Update with new Build
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
script: rm -rf opub-deploy/.next; mv opub-deploy/.next2 opub-deploy/.next; pm2 restart opub-deploy
```
Replace all instances
opub-deploywith your project name in EC2.
Detailed Guide
GitHub Repository Setup
Create a GitHub repository, for example: opub-deploy.
Local Development Setup
- Generate a Next.js project:
bash
npx create-next-app@latest opub-deploy
cd opub-deploy
npm install
- Add the following code to
.github/workflows/deploy.ymlin your project:
```yaml name: Deploy Next.js to EC2
on: push: branches: - main
jobs: deploy: runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install dependencies
run: npm install
- name: Build
run: npm run build
- name: Rename .next to .next2
run: mv .next .next2
- name: Send .next2 to EC2
uses: appleboy/scp-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
source: .next2
target: opub-deploy
- name: Update with new Build
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
script: rm -rf opub-deploy/.next; mv opub-deploy/.next2 opub-deploy/.next; pm2 restart deploy
```
Replace all instances
opub-deploywith your project name in EC2.
- Initialize a Git repository:
bash
git init
git add .
git branch -M main
git remote add origin git@github.com:CivicDataLab/opub-deploy.git
git commit -m "first commit"
git push -u origin main
Server Setup
EC2 Setup
- Create an EC2 instance with Ubuntu 20.04.
- Create a new key pair and download the
.pemfile. - Create a new security group with the following inbound rules:
- SSH (22) from anywhere
- HTTP (80) from anywhere
- HTTPS (443) from anywhere
- Custom TCP (3000) from anywhere
- Connect to the EC2 instance using SSH:
bash
ssh -i "path/to/key.pem" username@publicIP
Add packages
Install Node.js and npm using NVM:
```bash sudo apt update
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
export NVMDIR="$([ -z "${XDGCONFIGHOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDGCONFIGHOME}/nvm")" [ -s "$NVMDIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" ```
Install PM2 globally:
bash
npm install pm2 -g
Create symbolic links
bash
sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/node" "/usr/local/bin/node"
sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/npm" "/usr/local/bin/npm"
sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/pm2" "/usr/local/bin/pm2"
Add Repository
Clone your GitHub repository and navigate to its directory:
bash
git clone https://github.com/CivicDataLab/opub-deploy.git
cd opub-deploy
Install the dependencies and start the server:
bash
npm install
npm run build
pm2 start npm -n deploy -- start
for custom port
pm2 start npm -n deploy -- start -- -p 3001
Adding SSH Key to GitHub
- Generate a SSH key:
bash
ssh-keygen -m PEM
- Move the public key to authorized_keys:
bash
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
- Copy the private key content:
bash
cat ~/.ssh/id_rsa
// Copy the content manually
In GitHub, navigate to Repository > Settings > Secrets and variables > Actions
Create the following three secrets to allow GitHub to SSH into EC2:
EC2_HOST: Public IP of your EC2 instanceEC2_USERNAME: Username to SSH into EC2EC2_PRIVATE_KEY: Paste the private key content generated earlier.
Conclusion
Now, every time you push to the main branch, the GitHub Actions workflow will build the project and deploy it to the EC2 instance without any manual intervention and downtime.
Owner
- Name: CivicDataLab
- Login: CivicDataLab
- Kind: organization
- Email: info@civicdatalab.in
- Location: India
- Website: https://www.civicdatalab.in/
- Twitter: CivicDataLab
- Repositories: 104
- Profile: https://github.com/CivicDataLab
Harnessing Data, Tech, Design and Social Science to strengthen the course of Civic Engagements in India.
GitHub Events
Total
Last Year
Issues and Pull Requests
Last synced: 12 months ago
All Time
- Total issues: 0
- Total pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Total issue authors: 0
- Total pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 0
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 0
- Pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels
Dependencies
- actions/checkout v2 composite
- appleboy/scp-action master composite
- appleboy/ssh-action v1.0.3 composite
- 974 dependencies
- @graphql-codegen/cli 3.3.1 development
- @graphql-codegen/client-preset 3.0.1 development
- @graphql-codegen/typescript ^3.0.4 development
- @graphql-codegen/typescript-graphql-request ^4.5.9 development
- @graphql-codegen/typescript-operations ^3.0.4 development
- @ianvs/prettier-plugin-sort-imports ^4.1.1 development
- @types/dom-to-image ^2.6.5 development
- autoprefixer ^10.4.14 development
- color2k ^2.0.2 development
- encoding ^0.1.13 development
- postcss ^8.4.23 development
- prettier ^3.0.3 development
- prettier-plugin-tailwindcss ^0.5.4 development
- style-dictionary ^3.8.0 development
- tailwind-merge ^1.12.0 development
- tailwindcss ^3.3.2 development
- typescript ^5.0.4 development
- @tabler/icons-react ^2.17.0
- @tanstack/react-query ^4.29.5
- @types/node ^20.1.3
- @types/react ^18.2.0
- @types/react-dom ^18.2.0
- dom-to-image ^2.6.0
- eslint 8.39.0
- eslint-config-next ^14.0.4
- graphql ^16.6.0
- graphql-request 5.1.0
- graphql-tag ^2.12.6
- next ^14.0.4
- next-intl ^3.4.0
- next-usequerystate ^1.8.1
- nextjs-toploader ^1.3.2
- opub-ui latest
- react ^18.2.0
- react-aria 3.22.0
- react-dom ^18.2.0
- react-error-boundary ^4.0.11
- sass ^1.69.5
- tailwindcss-animate ^1.0.7
- zustand ^4.4.1