-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/added deployemnt for UI and Backend (#14)
* updated deployment file * updated port and resources in deployment.yaml * Updated prompt and pie chart
- Loading branch information
1 parent
281dca4
commit 95d3c0e
Showing
22 changed files
with
355 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.