diff --git a/.github/workflows/nodejs-cicd-pipeline.yml b/.github/workflows/nodejs-cicd-pipeline.yml new file mode 100644 index 0000000..b2289be --- /dev/null +++ b/.github/workflows/nodejs-cicd-pipeline.yml @@ -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 diff --git a/.gitignore b/.gitignore index 094b383..903ebb5 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,6 @@ override.tf.json # Ignore CLI configuration files .terraformrc terraform.rc + +# NodeJS +.env diff --git a/README.md b/README.md index b0566ed..8c871e7 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/projects/nodejs-cicd-pipeline/.env b/projects/nodejs-cicd-pipeline/.env new file mode 100644 index 0000000..d3caf17 --- /dev/null +++ b/projects/nodejs-cicd-pipeline/.env @@ -0,0 +1,5 @@ +PGUSER="admin" +PGPASSWORD="pass" +PGDATABASE="database" +PGHOST="localhost" +PGPORT=5432 diff --git a/projects/nodejs-cicd-pipeline/Dockerfile b/projects/nodejs-cicd-pipeline/Dockerfile new file mode 100644 index 0000000..36a2e76 --- /dev/null +++ b/projects/nodejs-cicd-pipeline/Dockerfile @@ -0,0 +1,12 @@ +FROM node:latest + +WORKDIR /usr/src/app + +COPY package*.json ./ +RUN npm install + +COPY . . + +EXPOSE 3001 + +CMD ["node", "server.js"] diff --git a/projects/nodejs-cicd-pipeline/Dockerfile.test b/projects/nodejs-cicd-pipeline/Dockerfile.test new file mode 100644 index 0000000..f841fd7 --- /dev/null +++ b/projects/nodejs-cicd-pipeline/Dockerfile.test @@ -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"] diff --git a/projects/nodejs-cicd-pipeline/README.md b/projects/nodejs-cicd-pipeline/README.md new file mode 100644 index 0000000..fdda999 --- /dev/null +++ b/projects/nodejs-cicd-pipeline/README.md @@ -0,0 +1 @@ +# CICD for nodejs diff --git a/projects/nodejs-cicd-pipeline/demo_project.sh b/projects/nodejs-cicd-pipeline/demo_project.sh new file mode 100644 index 0000000..1beb1d2 --- /dev/null +++ b/projects/nodejs-cicd-pipeline/demo_project.sh @@ -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 diff --git a/projects/nodejs-cicd-pipeline/package.json b/projects/nodejs-cicd-pipeline/package.json new file mode 100644 index 0000000..d49844d --- /dev/null +++ b/projects/nodejs-cicd-pipeline/package.json @@ -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" + } +} diff --git a/projects/nodejs-cicd-pipeline/server.js b/projects/nodejs-cicd-pipeline/server.js new file mode 100644 index 0000000..249f2d6 --- /dev/null +++ b/projects/nodejs-cicd-pipeline/server.js @@ -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