-
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.
Added files and updated flow for deployment
- Loading branch information
1 parent
281dca4
commit 81bea02
Showing
8 changed files
with
254 additions
and
106 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,185 @@ 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 }} | ||
|
||
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 | ||
|
||
# Setup 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] | ||
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 | ||
cache: maven | ||
|
||
# Build Java services | ||
- name: Build Java services | ||
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" | ||
for SERVICE_NAME in $(echo $SERVICE_NAMES | tr ',' ' '); do | ||
echo "Building $SERVICE_NAME" | ||
# Set the build context to the root directory | ||
cd $SERVICE_NAME | ||
if [ $SERVICE_NAME != "blogs-analyzer-ui" ]; then | ||
if [ -d "$SERVICE_NAME" ]; then | ||
cd $SERVICE_NAME | ||
if [ "$SERVICE_NAME" != "blogs-analyzer-ui" ]; then | ||
mvn clean install -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 | ||
# 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 }} | ||
# 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 | ||
# 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 | ||
# Setup for Angular projects | ||
- name: Install Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: '21' | ||
|
||
# Install dependencies for Angular projects | ||
- name: Install dependencies for Angular projects | ||
run: | | ||
SERVICE_NAMES=$(cat projects-changes-deploy.txt) | ||
echo "Service Names: $SERVICE_NAMES" | ||
for SERVICE_NAME in $(echo $SERVICE_NAMES | tr ',' ' '); do | ||
echo "Processing Service: $SERVICE_NAME" | ||
if [ -d "$SERVICE_NAME" ]; then | ||
cd $SERVICE_NAME | ||
if [ "$SERVICE_NAME" == "blogs-analyzer-ui" ]; then | ||
npm install | ||
fi | ||
cd .. | ||
fi | ||
done | ||
deploy: | ||
runs-on: ubuntu-latest | ||
needs: build | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v2 | ||
|
||
- name: Setup Google Cloud SDK | ||
uses: google-github-actions/[email protected] | ||
with: | ||
project_id: ${{ env.GKE_PROJECT }} | ||
service_account_key: ${{ env.SERVICE_ACCOUNT_KEY }} | ||
|
||
- name: Authenticate to Google Cloud | ||
id: auth | ||
uses: google-github-actions/[email protected] | ||
with: | ||
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 | ||
- name: Build and push Docker images | ||
run: | | ||
SERVICE_NAMES=$(cat projects-changes-deploy.txt) | ||
echo "Service Names: $SERVICE_NAMES" | ||
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 | ||
IMAGE_NAME=gcr.io/gen-lang-client-0999974873/$SERVICE_NAME:latest | ||
docker build -t $IMAGE_NAME . | ||
docker push $IMAGE_NAME | ||
cd .. | ||
fi | ||
done | ||
- name: Deploy to GKE | ||
run: | | ||
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 | ||
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" ]; then | ||
cd $SERVICE_NAME/k8s | ||
kubectl apply -f . | ||
cd ../.. | ||
fi | ||
done | ||
sonarcloud: | ||
name: SonarCloud | ||
name: SonarCloud Analysis | ||
runs-on: ubuntu-latest | ||
needs: build | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up JDK 19 | ||
uses: actions/setup-java@v3 | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
# Setup for Java projects | ||
- name: Set up JDK 21 | ||
uses: actions/setup-java@v4 | ||
with: | ||
java-version: '19' | ||
java-version: '21' | ||
distribution: 'temurin' | ||
cache: maven | ||
- name: Read service names from file | ||
id: read-service-names | ||
|
||
# Setup for Node.js | ||
- name: Install Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: '21' | ||
|
||
# Sonar Analysis for Java projects | ||
- name: Sonar Analysis for Java projects | ||
run: | | ||
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 "Processing Service: $SERVICE_NAME" | ||
if [ -d "$SERVICE_NAME" ]; then | ||
cd $SERVICE_NAME | ||
if [ "$SERVICE_NAME" != "blogs-analyzer-ui" ]; then | ||
mvn clean verify sonar:sonar -Dsonar.host.url=https://sonarcloud.io -Dsonar.organization=nashtech -Dsonar.branch.name=master | ||
fi | ||
cd .. | ||
fi | ||
done | ||
# Sonar Analysis for Angular projects | ||
- name: Sonar Analysis for Angular projects | ||
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 | ||
SERVICE_NAMES=$(cat projects-changes-deploy.txt) | ||
echo "Service Names: $SERVICE_NAMES" | ||
for SERVICE_NAME in $(echo $SERVICE_NAMES | tr ',' ' '); do | ||
echo "Processing Service: $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 | ||
fi | ||
cd .. | ||
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,19 @@ | ||
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 |
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 |
Oops, something went wrong.