|
| 1 | +# Node.js REST API Deployment on AWS EC2 with GitHub Actions CI/CD Pipeline |
| 2 | + |
| 3 | +This repository demonstrates a streamlined approach to deploying a Node.js REST API on an AWS EC2 instance using GitHub Actions for continuous integration and continuous deployment (CI/CD). |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +In this project, we utilize GitHub Actions workflows to automate the CI/CD pipeline for deploying changes to our Node.js application on an EC2 instance. The workflow consists of: |
| 8 | + |
| 9 | +- **Continuous Integration (CI):** Automatically building and testing the application on each push to the `main` branch. |
| 10 | +- **Continuous Deployment (CD):** Automatically deploying changes to the EC2 instance upon successful completion of CI. |
| 11 | + |
| 12 | +## Setup Instructions |
| 13 | + |
| 14 | +Follow these steps to set up the deployment pipeline for your Node.js REST API on AWS EC2: |
| 15 | + |
| 16 | +1. **Create an EC2 Instance:** |
| 17 | + - Create a new EC2 instance in your AWS account or use an existing one. |
| 18 | + - Generate or use an existing SSH key pair for accessing the instance. |
| 19 | + |
| 20 | +2. **Git Repository Setup:** |
| 21 | + - Create a new Git repository and push your Node.js code to it. |
| 22 | + |
| 23 | +3. **GitHub Actions Setup:** |
| 24 | + - Navigate to your repository settings on GitHub and select Actions. |
| 25 | + - Add a self-hosted runner and follow the setup instructions. |
| 26 | + |
| 27 | +4. **Environment Setup for GitHub Actions:** |
| 28 | + - Create a `.env` file with your environment variables and add them as secrets in your GitHub repository settings. |
| 29 | + |
| 30 | +5. **CI/CD Workflows:** |
| 31 | + - Define CI/CD workflows in the `.github/workflows` directory. |
| 32 | + - Customize workflows to suit your project requirements. |
| 33 | + |
| 34 | +6. **Environment Setup in EC2 Instance:** |
| 35 | + - Install Node.js and Nginx on your EC2 instance. |
| 36 | + - Configure Nginx as a reverse proxy for your Node.js application. |
| 37 | + - Install PM2 to manage your Node.js process. |
| 38 | + |
| 39 | +## Workflow Explanation |
| 40 | + |
| 41 | +- **CI Process:** On each push to the `main` branch, the workflow executes CI tasks such as checking out code, setting up the Node.js environment, installing dependencies, and running tests (if applicable). |
| 42 | +- **CD Process:** Upon successful CI, the workflow triggers CD tasks including SSH into the EC2 instance, pulling the latest changes, restarting the Node.js application, and verifying deployment. |
| 43 | + |
| 44 | +## Directory Structure |
| 45 | + |
| 46 | +- `src/`: Contains the source code for the Node.js application. |
| 47 | +- `.github/workflows/`: Contains CI/CD workflow configuration files. |
| 48 | +- `.env`: Stores environment variables for the application. |
| 49 | +- `server.js`: Entry point for the Node.js application. |
| 50 | +- `package.json`: Dependency configuration for npm. |
| 51 | + |
| 52 | +## Usage |
| 53 | + |
| 54 | +1. Clone the repository: |
| 55 | + |
| 56 | + ```bash |
| 57 | + git clone https://github.com/HGSChandeepa/nodejs-rest-api-EC2 |
| 58 | + ``` |
| 59 | + |
| 60 | +2. Customize the Node.js application code in the `server.js` file according to your requirements. |
| 61 | + |
| 62 | +3. Push changes to the `main` branch. GitHub Actions will automatically trigger the CI/CD pipeline. |
| 63 | + |
| 64 | +## Contributions |
| 65 | + |
| 66 | +Contributions to improve this CI/CD setup or add additional features are welcome! Feel free to submit pull requests or open issues. |
| 67 | + |
| 68 | +## License |
| 69 | + |
| 70 | +This project is licensed under the [MIT License](LICENSE). |
0 commit comments