Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 25: NodeJS CICD #26

Merged
merged 23 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/nodejs-cicd-pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Node.js CI/CD with Docker

on:
pull_request:
branches:
- main
paths:
- 'projects/nodejs-cicd-pipeline/**'
- '.github/workflows/nodejs-cicd-pipeline.yml'

jobs:
nodejs-cicd:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Build Docker image
run: |
cd projects/nodejs-cicd-pipeline/
docker build -t node-demo:${{ github.sha }} .

- name: Run tests
run: |
cd projects/nodejs-cicd-pipeline/
# Run tests here, replace with your actual test commands
echo "TODO: Run test here"

- name: Push Docker image
run: |
# Check if the branch is main and push image if true
if [ ${{ github.ref }} != 'refs/heads/main' ]; then
echo "Skip push image for ${{ github.ref }}"
exit 0
else
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKERHUB_TOKEN }}
docker tag node-demo:${{ github.sha }} ${{ secrets.DOCKERHUB_USERNAME }}/node-demo:${{ github.sha }}
docker images
echo "TODO: Push image here..."
fi
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ override.tf.json
# Ignore CLI configuration files
.terraformrc
terraform.rc

# NodeJS
.env
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ Collection of DevOps project
| 01 | Create k8s cluster aws with kubeadm | [create-k8s-cluster-aws-ec2](./projects/create-k8s-cluster-aws-ec2/) | ✔️ Done |
| 02 | Dockerize python application | [dockerize-python-application](./projects/dockerize-python-application/) | ✔️ Done |
| 03 | Nginx Static Website Local | [nginx-static-website-local](./projects/nginx-static-website-local/) | ✔️ Done |
| 04 | NodeJS CICD | [nodejs-cicd-pipeline](./projects/nodejs-cicd-pipeline/) | ✔️ Done |
5 changes: 5 additions & 0 deletions projects/nodejs-cicd-pipeline/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PGUSER="admin"
PGPASSWORD="pass"
PGDATABASE="database"
PGHOST="localhost"
PGPORT=5432
12 changes: 12 additions & 0 deletions projects/nodejs-cicd-pipeline/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM node:latest

WORKDIR /usr/src/app

COPY package*.json ./
RUN npm install

COPY . .

EXPOSE 3001

CMD ["node", "server.js"]
17 changes: 17 additions & 0 deletions projects/nodejs-cicd-pipeline/Dockerfile.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Use an official Node.js runtime as the base image
FROM node:latest

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install Node.js dependencies (including devDependencies for testing)
RUN npm install

# Copy the entire project to the working directory
COPY . .

# Run tests using the npm test command (modify as per your project setup)
CMD ["npm", "test"]
1 change: 1 addition & 0 deletions projects/nodejs-cicd-pipeline/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# CICD for nodejs
9 changes: 9 additions & 0 deletions projects/nodejs-cicd-pipeline/demo_project.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

# Backend
pwd
echo "Build backend"
pushd backend
docker build -t node-backend .
docker run -d -p 3001:3001 node-backend
popd
16 changes: 16 additions & 0 deletions projects/nodejs-cicd-pipeline/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "pern-backend",
"version": "1.0.0",
"dependencies": {
"express": "^4.18.2",
"pg": "^8.11.3",
"dotenv": "^16.3.1"
},
"devDependencies": {
"jest": "^27.5.1",
"supertest": "^6.2.1"
},
"scripts": {
"test": "jest"
}
}
33 changes: 33 additions & 0 deletions projects/nodejs-cicd-pipeline/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require('dotenv').config();
const express = require('express');
const app = express();
const { Pool } = require('pg');
require('dotenv').config();

// PostgreSQL database connection setup
const pool = new Pool({
user: process.env.PGUSER,
password: process.env.PGPASSWORD,
database: process.env.PGDATABASE,
host: process.env.PGHOST || 'localhost',
port: process.env.PGPORT || 5432,
});

// Define API routes
app.get('/api/tasks', async (req, res) => {
try {
const { rows } = await pool.query('SELECT * FROM tasks');
res.json(rows);
} catch (error) {
res.status(500).json({ error: 'Server error `${error}`' });
}
});

// Other CRUD operations (POST, PUT, DELETE) for tasks

const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});

module.exports = { app, pool }; // Export app and other necessary elements