diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml deleted file mode 100644 index 58c79cde54..0000000000 --- a/.github/workflows/main.yml +++ /dev/null @@ -1,33 +0,0 @@ -name: Publish Docker image - -on: - push: - branches: ['master'] - -jobs: - push_to_registry: - name: Push Docker image to Docker Hub - runs-on: ubuntu-latest - steps: - - name: Check out the repo - uses: actions/checkout@v3 - - - name: Log in to Docker Hub - uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 - with: - username: ${{ secrets.DOCKER_USERNAME }} - password: ${{ secrets.DOCKER_PASSWORD }} - - - name: Extract metadata (tags, labels) for Docker - id: meta - uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 - with: - images: thetips4you/testjodejsapp - - - name: Build and push Docker image - uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc - with: - context: . - push: true - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml new file mode 100644 index 0000000000..c4915bd8b1 --- /dev/null +++ b/.github/workflows/node.js.yml @@ -0,0 +1,19 @@ +name: Deploy + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: self-hosted + steps: + + - name: Build Docker image for app + run: | + docker build -t my-app:latest -f /home/mnagaraju/actions-runner/Testproject-inno/zephus-dockerfile /home/mnagaraju/actions-runner/Testproject-inno + + - name: Build Docker image for node + run: | + docker build -t my-node:latest -f /home/mnagaraju/actions-runner/Testproject-inno/zephus-dokerfile-optimize /home/mnagaraju/actions-runner/Testproject-inno/ diff --git a/README.md b/README.md new file mode 100644 index 0000000000..6d26216298 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Node.js Application diff --git a/ansible/repo.yaml b/ansible/repo.yaml new file mode 100644 index 0000000000..c18ce6cf90 --- /dev/null +++ b/ansible/repo.yaml @@ -0,0 +1,44 @@ +--- +- name: Git Checkout, Docker Install, Build Image, Push to Artifact Registry, and Deploy to GKE + hosts: localhost + gather_facts: false + + tasks: + - name: Ensure git is installed + become: true + apt: + name: git + state: present + + - name: Ensure Docker dependencies are installed + become: true + apt: + name: "{{ item }}" + state: present + loop: + - containerd + - docker.io + + - name: Clone Node.js code from GitHub + git: + repo: https://github.com/mnagaraju5628/Testproject-INNO.git + dest: /home/ubuntu/actions-runner/Testproject-INNO + version: main + become: true + + - name: Build Docker image + command: "docker build -t us-central1-docker.pkg.dev/goldengate-1/test-project/node:latest /home/ubuntu/actions-runner/Testproject-INNO" + become: true + + - name: Push Docker image to artifact registry + command: "docker push us-central1-docker.pkg.dev/goldengate-1/test-project/node:latest" + become: true + + - name: Deploy to GKE + become: true + shell: | + cd /home/ubuntu/actions-runner/Testproject-INNO/ + gcloud container clusters get-credentials innotest-cluster --region us-central1-c + kubectl rollout restart deployment nodeapp-deployment + kubectl apply -f deployment.yaml + kubectl apply -f service.yaml diff --git a/credentials.enc b/credentials.enc new file mode 100644 index 0000000000..264ebaac15 --- /dev/null +++ b/credentials.enc @@ -0,0 +1 @@ +Salted__úÈ£-@–ÖÖXrd%¸%÷Ö¨ˆ>ÂÝ„¡g¸®ÿS¢4f ŒWÌà \ No newline at end of file diff --git a/decrypt.sh b/decrypt.sh new file mode 100644 index 0000000000..3b9c34df07 --- /dev/null +++ b/decrypt.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# Path variables +CREDENTIALS_ENC="/home/mnagaraju/actions-runner/Testproject-inno/credentials.enc" +ENCRYPTION_KEY="/home/mnagaraju/actions-runner/Testproject-inno/encryption_key.txt" +CREDENTIALS="/home/mnagaraju/actions-runner/Testproject-inno/credentials.txt" + +# Decrypt the credentials +openssl enc -aes-256-cbc -pbkdf2 -d -in $CREDENTIALS_ENC -out $CREDENTIALS -pass file:$ENCRYPTION_KEY + +if [ $? -ne 0 ]; then + echo "Decryption failed" + exit 1 +fi + +echo "Decryption succeeded" diff --git a/deployment.yml b/deployment.yaml similarity index 64% rename from deployment.yml rename to deployment.yaml index 8468bbd661..1f42e272e9 100644 --- a/deployment.yml +++ b/deployment.yaml @@ -12,10 +12,14 @@ spec: template: metadata: labels: - app: nodeapp + app: nodeapp spec: containers: - name: nodeserver - image: thetips4you/nodeapp:latest + image: us-central1-docker.pkg.dev/goldengate-1/demogcr-ar/node:latest ports: - containerPort: 3000 + imagePullPolicy: Always + strategy: + type: RollingUpdate + diff --git a/deploymentservice.yml b/deploymentservice.yml deleted file mode 100644 index ab6f0b707d..0000000000 --- a/deploymentservice.yml +++ /dev/null @@ -1,38 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: nodeapp-deployment - labels: - app: nodeapp -spec: - replicas: 1 - selector: - matchLabels: - app: nodeapp - template: - metadata: - labels: - app: nodeapp - spec: - containers: - - name: nodeserver - image: thetips4you/nodeapp:latest - ports: - - containerPort: 3000 - - ---- - -apiVersion: v1 -kind: Service -metadata: - name: nodeapp-service -spec: - selector: - app: nodeapp - type: LoadBalancer - ports: - - protocol: TCP - port: 5000 - targetPort: 3000 - nodePort: 31110 diff --git a/docker-compose-test.yml b/docker-compose-test.yml new file mode 100644 index 0000000000..20a11a0a26 --- /dev/null +++ b/docker-compose-test.yml @@ -0,0 +1,15 @@ +version: '3.8' + +services: + app: + build: + context: . + dockerfile: dockerfile + volumes: + - '/home/mnagaraju/actions-runner/Testproject-inno/credentials.txt:/app/config/credentials.txt' + environment: + - NODE_ENV=test + restart: always + ports: + - "4000:4000" + command: [ "node", "index.js" ] diff --git a/dockerfile b/dockerfile index bbb5d90735..31574547be 100644 --- a/dockerfile +++ b/dockerfile @@ -1,12 +1,24 @@ -FROM node:latest +FROM node:${NODE_VERSION:-14.19.2} AS deps -WORKDIR /usr/src/app +WORKDIR /usr/app -COPY package.json ./ +ENV DEBIAN_FRONTEND noninteractive -RUN npm install +RUN sed -i s/deb.debian.org/archive.debian.org/g /etc/apt/sources.list +RUN sed -i 's|security.debian.org|archive.debian.org/|g' /etc/apt/sources.list +RUN sed -i '/stretch-updates/d' /etc/apt/sources.list + +RUN apt update && apt -y dist-upgrade + +RUN apt update && apt install musl-dev -y && \ + ln -s /usr/lib/x86_64-linux-musl/libc.so /lib/libc.musl-x86_64.so.1 COPY . . +# RUN yarn install --frozen-lockfile + +RUN npm install pm2 -g + +RUN mkdir ~/.ssh && \ + ssh-keyscan -H github.com >> ~/.ssh/known_hosts -EXPOSE 4000 -CMD [ "node", "index.js" ] +CMD ["./index.js"] diff --git a/dockerfile-optimize b/dockerfile-optimize new file mode 100644 index 0000000000..1f7b87b6a6 --- /dev/null +++ b/dockerfile-optimize @@ -0,0 +1,32 @@ +FROM node:${NODE_VERSION:-14.19.2}-slim AS deps + +# Set up working directory and noninteractive mode for apt-get +WORKDIR /usr/app +ENV DEBIAN_FRONTEND=noninteractive + +# Update Debian sources, install necessary packages, and clean up in a single layer +RUN sed -i s/deb.debian.org/archive.debian.org/g /etc/apt/sources.list && \ + sed -i 's|security.debian.org|archive.debian.org/|g' /etc/apt/sources.list && \ + sed -i '/stretch-updates/d' /etc/apt/sources.list && \ + apt-get update && \ + apt-get -y dist-upgrade && \ + apt-get install -y --no-install-recommends musl-dev openssh-client && \ + ln -s /usr/lib/x86_64-linux-musl/libc.so /lib/libc.musl-x86_64.so.1 && \ + npm install -g pm2 && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /usr/share/doc /usr/share/man /tmp/* + +# Copy package files and install dependencies +COPY package.json package-lock.json ./ + +RUN yarn install --frozen-lockfile + +# Copy the remaining application source code +COPY . . + +# Configure SSH for GitHub access +RUN mkdir -p ~/.ssh && \ + ssh-keyscan -H github.com >> ~/.ssh/known_hosts + +# Start the application +CMD ["node", "index.js"] diff --git a/horizon-dockerfile b/horizon-dockerfile new file mode 100644 index 0000000000..00d9bbd645 --- /dev/null +++ b/horizon-dockerfile @@ -0,0 +1,40 @@ +# Build project files and generate assets +FROM node:16.17-alpine AS build + +# Set CI to true to catch warnings as errors +ENV CI true + +# Install system dependencies +RUN apk add --no-cache \ + git \ + libtool \ + util-linux + +WORKDIR /usr/app + +# Install project package dependencies +COPY package.json package-lock.json ./ +RUN npm ci + +# Build the project +COPY . . +#RUN npm run build + +# Serving static content from build +FROM nginx:1.23.4-alpine + +WORKDIR /usr/share/nginx/html + +# Update system packages to resolve vulnerabilities +RUN apk update && apk upgrade --available + +# Modify default behavior of web server +# to redirect requests to index.html +RUN sed -i \ + 's|location / {|location / {\n\t try_files $uri $uri/ /index.html;|' \ + /etc/nginx/conf.d/default.conf + +# Copy assets from build stage +#COPY --from=build /usr/app/build/ /usr/share/nginx/html/ + +HEALTHCHECK CMD curl --fail "http://localhost" || exit 1 diff --git a/horizon-dockerfile-optimize b/horizon-dockerfile-optimize new file mode 100644 index 0000000000..863078d7c7 --- /dev/null +++ b/horizon-dockerfile-optimize @@ -0,0 +1,37 @@ +# Stage 1: Build project files and generate assets +FROM node:16-alpine AS build + +# Set CI to true to catch warnings as errors +ENV CI=true + +# Install only essential system dependencies +RUN apk add --no-cache git + +WORKDIR /usr/app + +# Install project package dependencies +COPY package.json package-lock.json ./ +RUN npm ci && rm -rf /root/.npm + +# Build the project +COPY . ./ +#RUN npm run build + +# Stage 2: Serve static content with a minimal Nginx image +FROM nginx:alpine + +# Set the working directory for Nginx +WORKDIR /usr/share/nginx/html + +# Remove default Nginx configuration and add custom one +RUN rm /etc/nginx/conf.d/default.conf + +# Copy custom Nginx configuration +#COPY nginx.conf /etc/nginx/conf.d/ + +# Copy assets from the build stage +# COPY --from=build /usr/app/build/ /usr/share/nginx/html/ + + +# Define a health check (if needed) +HEALTHCHECK CMD curl --fail "http://localhost" || exit 1 diff --git a/index.js b/index.js index 668a9f8ed6..5b4e6a50a3 100644 --- a/index.js +++ b/index.js @@ -2,11 +2,11 @@ var express = require('express'); var app = express(); app.get('/', function (req, res) { - res.send('{ "response": "Hello From Thetips4you" }'); + res.send('{ "response": "Welcome to Github-action CI/CD via ansible " }'); }); app.get('/will', function (req, res) { - res.send('{ "response": "Hello World" }'); + res.send('{ "response": "Welcome" }'); }); app.get('/ready', function (req, res) { res.send('{ "response": " Great!, It works!" }'); diff --git a/main.tf b/main.tf new file mode 100644 index 0000000000..2b62b46e71 --- /dev/null +++ b/main.tf @@ -0,0 +1,37 @@ +provider "google" { + project = "goldengate-1" + region = "us-central1" + credentials = file("/home/mnagaraju/gcp-service-account.json") +} + +resource "google_compute_network" "custom_network" { + name = "test-network" + auto_create_subnetworks = false +} + +resource "google_compute_subnetwork" "custom_subnet" { + name = "test-subnet" + network = google_compute_network.custom_network.id + ip_cidr_range = "10.0.0.0/16" + region = "us-central1" +} + +resource "google_compute_instance" "vm_instance" { + name = "vm-terraform-testing" + machine_type = "e2-medium" + zone = "us-central1-a" + + boot_disk { + initialize_params { + image = "ubuntu-os-cloud/ubuntu-2204-lts" + } + } + + network_interface { + network = google_compute_network.custom_network.id + subnetwork = google_compute_subnetwork.custom_subnet.id + access_config { + # Assigns an ephemeral external IP + } + } +} diff --git a/service.yml b/service.yaml similarity index 81% rename from service.yml rename to service.yaml index 1a0ab51485..d26c2b0f39 100644 --- a/service.yml +++ b/service.yaml @@ -1,3 +1,4 @@ +--- apiVersion: v1 kind: Service metadata: @@ -8,6 +9,6 @@ spec: type: LoadBalancer ports: - protocol: TCP - port: 5000 + port: 8000 targetPort: 3000 - nodePort: 31110 + nodePort: 31000 diff --git a/test/test.js b/test/test.js index 2794ef1f03..bf4f29c332 100644 --- a/test/test.js +++ b/test/test.js @@ -1,7 +1,7 @@ var request = require('supertest'); var app = require('../index.js'); describe('GET /will', function() { - it('respond with hello world', function(done) { - request(app).get('/will').expect('{ "response": "Hello World" }', done); + it('respond with Welcome', function(done) { + request(app).get('/will').expect('{ "response": "Welcome" }', done); }); -}); \ No newline at end of file +}); diff --git a/zephus-dockerfile b/zephus-dockerfile new file mode 100644 index 0000000000..1cc49e515f --- /dev/null +++ b/zephus-dockerfile @@ -0,0 +1,17 @@ +FROM node:16.17-alpine + +ARG FONT_AWESOME_TOKEN +ENV FONT_AWESOME_AUTH_TOKEN=$FONT_AWESOME_TOKEN + +RUN apk update && apk upgrade --available + +RUN apk add --no-cache git && \ + npm install pm2 -g + +WORKDIR /app + +COPY . ./ +RUN npm ci +#npm run build + +CMD ["pm2-runtime", "start", "npm", "--", "start"] diff --git a/zephus-dokerfile-optimize b/zephus-dokerfile-optimize new file mode 100644 index 0000000000..fedff611a9 --- /dev/null +++ b/zephus-dokerfile-optimize @@ -0,0 +1,24 @@ +FROM node:16.17-alpine + +ARG FONT_AWESOME_TOKEN +ENV FONT_AWESOME_AUTH_TOKEN=$FONT_AWESOME_TOKEN + +# Update, upgrade, and install necessary packages in one layer +RUN apk add --no-cache git && \ + npm install -g pm2 && \ + npm cache clean --force + +WORKDIR /app + +# Copy only package.json and package-lock.json first to leverage Docker cache for dependencies +COPY package*.json ./ + +# Install project dependencies +RUN npm ci && \ + npm cache clean --force + +# Copy the remaining application files +COPY . . + +# Start the application using pm2-runtime +CMD ["pm2-runtime", "start", "npm", "--", "start"]