Skip to content

Commit

Permalink
Feature/added deployemnt for UI and Backend (#14)
Browse files Browse the repository at this point in the history
* updated deployment file

* updated port and resources in deployment.yaml

* Updated prompt and pie chart
  • Loading branch information
Kuldeep-knoldus authored Jul 1, 2024
1 parent 281dca4 commit 95d3c0e
Show file tree
Hide file tree
Showing 22 changed files with 355 additions and 150 deletions.
196 changes: 94 additions & 102 deletions .github/workflows/gcp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,131 +3,123 @@ name: GCP CI/CD Pipeline
on:
push:
branches:
- main
- '**'

env:
PROJECT_ID: '${{ secrets.GKE_PROJECT }}'
SERVICE_ACCOUNT_KEY: '${{ secrets.SERVICE_ACCOUNT_KEY }}'
CLUSTER_NAME: '${{ secrets.CLUSTER_NAME }}'
REGION: '${{ secrets.GKE_REGION }}'
REGISTRY_NAME: '${{ secrets.REGISTRY_NAME }}'
PROJECT_ID: ${{ secrets.GKE_PROJECT }}
SERVICE_ACCOUNT_KEY: ${{ secrets.SERVICE_ACCOUNT_KEY }}
CLUSTER_NAME: blogs-analyzer-cluster
REGION: ${{ secrets.GKE_ZONE }}
REGISTRY_NAME: blogs-analyzer
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
GOOGLE_GEMINI_API_KEY: ${{ secrets.GOOGLE_GEMINI_API_KEY }}

jobs:
build-and-deploy:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up JDK 19
uses: actions/setup-java@v3
uses: actions/checkout@v4

# Set up JDK for Java projects
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '19'
java-version: '21'
distribution: 'temurin'
server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
settings-path: ${{ github.workspace }}
- name: Set up gcloud CLI
uses: google-github-actions/[email protected]
cache: maven

# Set up Node.js for Angular projects
- name: Set up Node.js
uses: actions/setup-node@v4
with:
service_account_key: env.SERVICE_ACCOUNT_KEY
project_id: env.PROJECT_ID
export_default_credentials: true
- run: gcloud components update
- name: Read service names from file
id: read-service-names
node-version: '21'

# Build Java and Angular projects
- name: Build Projects
run: |
SERVICE_NAMES=$(cat projects-changes-deploy.txt)
echo "Service Names: $SERVICE_NAMES"
echo "::set-output name=service_names::$SERVICE_NAMES"
- name: Build and deploy services
run: |
for SERVICE_NAME in $(echo ${{ steps.read-service-names.outputs.service_names }} | tr ',' ' '); do
echo "Building and deploying $SERVICE_NAME"
# Set the build context to the root directory
cd $SERVICE_NAME
if [ $SERVICE_NAME != "blogs-analyzer-ui" ]; then
mvn clean install -B -V
for SERVICE_NAME in $(echo $SERVICE_NAMES | tr ',' ' '); do
echo "Building $SERVICE_NAME"
if [ -d "$SERVICE_NAME" ]; then
cd $SERVICE_NAME
if [ "$SERVICE_NAME" == "blogs-analyzer-ui" ]; then
npm install && npm test && npm install -g sonarqube-scanner && npm run sonar
else
mvn clean install -Psonar -B -V
fi
cd ..
fi
done
# Build Docker image
docker build -t ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REGISTRY_NAME }}/$SERVICE_NAME -f Dockerfile .
# Configure Docker
gcloud auth configure-docker ${{ env.REGION }}-docker.pkg.dev
# Install GKE gcloud Auth Plugin
gcloud components install gke-gcloud-auth-plugin
# Authenticate Docker to Google Artifact Registry
gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin https://${{ env.REGION }}-docker.pkg.dev
deploy:
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout code
uses: actions/checkout@v4

# Docker Image Tag
docker tag ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REGISTRY_NAME }}/$SERVICE_NAME:latest ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REGISTRY_NAME }}/$SERVICE_NAME:${{ github.sha }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

# Push Docker image to Google Artifact Registry
docker push ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REGISTRY_NAME }}/$SERVICE_NAME
# Deploy to GKE
gcloud container clusters get-credentials ${{ env.CLUSTER_NAME }} --region ${{ env.REGION }} --project ${{ env.PROJECT_ID }}
echo 'going to delete secret'
kubectl delete secret regcred &>/dev/null
echo 'creating secret'
kubectl create secret docker-registry regcred --docker-server=${{ env.REGION }}-docker.pkg.dev --docker-username=oauth2accesstoken --docker-password=$(gcloud auth print-access-token) [email protected]
echo 'adding secret to service account default'
kubectl patch serviceaccount default -p '{"imagePullSecrets": [{"name": "regcred"}]}'
# Apply role binding to default service account
#kubectl apply -f role-binding-default.yaml
- name: Setup Google Cloud SDK
uses: google-github-actions/[email protected]
with:
project_id: ${{ env.GKE_PROJECT }}
service_account_key: ${{ env.SERVICE_ACCOUNT_KEY }}

# Creates a kubernetes secret using the GitHub secret as the docker password
kubectl apply -f gcp-deploy-service.yaml --force
# Call the restart script
chmod +x restart_deployment.sh
sh restart_deployment.sh
done
sonarcloud:
name: SonarCloud
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 19
uses: actions/setup-java@v3
- name: Authenticate to Google Cloud
id: auth
uses: google-github-actions/[email protected]
with:
java-version: '19'
credentials_json: ${{ env.SERVICE_ACCOUNT_KEY }}

- name: Configure Docker for Google Container Registry
run: gcloud auth configure-docker

- name: Install gke-gcloud-auth-plugin
run: |
gcloud components install gke-gcloud-auth-plugin
# Setup for Java projects
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
cache: maven
- name: Read service names from file
id: read-service-names

- name: Build and Push Docker Images
run: |
COMMIT_SHA=$(git rev-parse --short HEAD)
SERVICE_NAMES=$(cat projects-changes-deploy.txt)
echo "Service Names: $SERVICE_NAMES"
echo "::set-output name=service_names::$SERVICE_NAMES"
- name: SonarCloud Scan
for SERVICE_NAME in $(echo $SERVICE_NAMES | tr ',' ' '); do
echo "Building and pushing Docker image for $SERVICE_NAME"
if [ -d "$SERVICE_NAME" ]; then
cd $SERVICE_NAME
if [ "$SERVICE_NAME" != "blogs-analyzer-ui" ]; then
mvn clean install -DskipTests
fi
IMAGE_NAME=gcr.io/gen-lang-client-0999974873/$SERVICE_NAME:$COMMIT_SHA
docker build -t $IMAGE_NAME .
docker push $IMAGE_NAME
cd ..
fi
done
- name: Deploy to GKE
run: |
for SERVICE_NAME in $(echo ${{ steps.read-service-names.outputs.service_names }} | tr ',' ' '); do
echo "Processing Service: $SERVICE_NAME"
#ls -al
cd $SERVICE_NAME
#pwd
# Check if it's the ReactJS project
if [ "$SERVICE_NAME" == "blogs-analyzer-ui" ]; then
# Angular-specific commands
npm install
# Run tests and generate code coverage reports (replace with your test command)
npm test -- --coverage
# Install SonarScanner for Node.js
npm install -g sonarqube-scanner
# Run SonarScanner for Node.js
sonar-scanner -Dsonar.host.url=https://sonarcloud.io -Dsonar.organization=nashtech -Dsonar.branch.name=master
else
# Maven Sonar
mvn clean verify sonar:sonar -Dsonar.host.url=https://sonarcloud.io -Dsonar.organization=nashtech -Dsonar.branch.name=master
fi
echo "Getting credentials for cluster ${{ env.CLUSTER_NAME }} in zone ${{ env.REGION }}"
gcloud container clusters get-credentials blogs-analyzer-cluster --region asia-south1 --project gen-lang-client-0999974873
COMMIT_SHA=$(git rev-parse --short HEAD)
SERVICE_NAMES=$(cat projects-changes-deploy.txt)
echo "Service Names: $SERVICE_NAMES"
for SERVICE_NAME in $(echo $SERVICE_NAMES | tr ',' ' '); do
echo "Deploying $SERVICE_NAME to GKE"
if [ -d "$SERVICE_NAME/k8s" ]; then
sed -i "s|gcr.io/gen-lang-client-0999974873/$SERVICE_NAME:latest|gcr.io/gen-lang-client-0999974873/$SERVICE_NAME:$COMMIT_SHA|g" $SERVICE_NAME/k8s/deployment.yaml
kubectl apply -f $SERVICE_NAME/k8s/
fi
done
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Blogs-Analyzer-UI is an Angular application used to display the results on a use
- [Dependencies](#dependencies)
- [Endpoints](#endpoints)
- [Running the Application](#running-the-application)
- [Tech Stack](#tech-stack)

## Prerequisites

Expand Down
38 changes: 38 additions & 0 deletions blogs-analyzer-ui/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Stage 1: Build the Angular application
FROM node:21 AS build

# Set the working directory in the container
WORKDIR /app

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

# Install Angular CLI globally
RUN npm install -g @angular/cli

# Install dependencies
RUN npm install

# Copy the rest of the application code to the working directory
COPY . .

# Build the Angular app in production mode
RUN ng build

# Stage 2: Use a smaller, nginx-based image to serve the app
FROM nginx:1.21-alpine

# Copy custom nginx configuration
COPY nginx.conf /etc/nginx/nginx.conf

# Create log directory configured in nginx.conf
RUN mkdir -p /var/log/app_engine

# Copy the built app from the previous stage to the nginx web server directory
COPY --from=build /app/dist/blogs-analyzer-ui /usr/share/nginx/html

# Expose port 80 to the Docker environment
EXPOSE 80

# Command to run nginx when the container starts
CMD ["nginx", "-g", "daemon off;"]
21 changes: 19 additions & 2 deletions blogs-analyzer-ui/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"allowedCommonJsDependencies": [
"highcharts"
],
"outputPath": "dist/blogs-analyzer-ui",
"index": "src/index.html",
"main": "src/main.ts",
Expand All @@ -25,6 +28,7 @@
],
"tsConfig": "tsconfig.app.json",
"inlineStyleLanguage": "scss",
"aot": true,
"assets": [
"src/favicon.ico",
"src/assets"
Expand All @@ -38,11 +42,24 @@
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"sourceMap": false,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
"maximumWarning": "5000kb",
"maximumError": "10mb"
},
{
"type": "anyComponentStyle",
Expand Down
27 changes: 27 additions & 0 deletions blogs-analyzer-ui/k8s/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: blogs-analyzer-ui
spec:
replicas: 3
selector:
matchLabels:
app: blogs-analyzer-ui
template:
metadata:
labels:
app: blogs-analyzer-ui
spec:
containers:
- name: blogs-analyzer-ui
image: gcr.io/gen-lang-client-0999974873/blogs-analyzer-ui:latest
ports:
- containerPort: 80
resources:
requests:
memory: "512Mi"
cpu: "500m"
limits:
memory: "1Gi"
cpu: "1"
imagePullPolicy: Always
12 changes: 12 additions & 0 deletions blogs-analyzer-ui/k8s/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: blogs-analyzer-ui
spec:
selector:
app: blogs-analyzer-ui
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
27 changes: 27 additions & 0 deletions blogs-analyzer-ui/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
events{}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;

# Logs will appear on the Google Developer's Console when logged to this
# directory.
access_log /var/log/app_engine/app.log;
error_log /var/log/app_engine/app.log;

gzip on;
gzip_disable "msie6";
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
}
1 change: 1 addition & 0 deletions blogs-analyzer-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"bootstrap": "^5.3.0",
"highcharts": "^11.4.3",
"highcharts-angular": "^4.0.0",
"ngx-markdown": "^16.0.0",
"rxjs": "~7.8.0",
"sonarqube-scanner": "^3.0.1",
"tslib": "^2.3.0",
Expand Down
Loading

0 comments on commit 95d3c0e

Please sign in to comment.