diff --git a/Pipeline Setup.docx b/Pipeline Setup.docx new file mode 100644 index 0000000..cc31933 Binary files /dev/null and b/Pipeline Setup.docx differ diff --git a/cloudbuildgcp.yaml b/cloudbuildgcp.yaml new file mode 100644 index 0000000..0d9dafd --- /dev/null +++ b/cloudbuildgcp.yaml @@ -0,0 +1,55 @@ +#code for Google Cloud Build --> deployment.yaml script +steps: + # Set up variables + - name: 'gcr.io/cloud-builders/git' + id: 'Get Commit SHA' + entrypoint: 'bash' + args: + - '-c' + - | + echo "COMMIT_SHA=$(git rev-parse --short HEAD)" >> $BUILD_ENV + env: + - 'BUILD_ENV=/workspace/build.env' + + # Build and push Docker image + - name: 'maven:3.8.7-openjdk-17' + id: 'Build and Push Image' + entrypoint: 'bash' + args: + - '-c' + - | + source /workspace/build.env + IMAGE_TAG=${_REGION}-docker.pkg.dev/${_PROJECT_ID}/${_REPO}/${_IMAGE}:$COMMIT_SHA + mvn compile jib:build -Dimage=$IMAGE_TAG + echo "IMAGE_TAG=$IMAGE_TAG" >> /workspace/build.env + env: + - 'BUILD_ENV=/workspace/build.env' + + # Substitute image tag in deployment.yaml + - name: 'gcr.io/cloud-builders/gcloud' + id: 'Substitute Image Tag' + entrypoint: 'bash' + args: + - '-c' + - | + source /workspace/build.env + sed "s|IMAGE_PLACEHOLDER|$IMAGE_TAG|g" k8s/deployment.yaml > k8s/deployment-sub.yaml + + # Authenticate kubectl and deploy to GKE + - name: 'gcr.io/cloud-builders/gcloud' + id: 'Deploy to GKE' + entrypoint: 'bash' + args: + - '-c' + - | + gcloud container clusters get-credentials ${_CLUSTER_NAME} --region ${_REGION} --project ${_PROJECT_ID} + kubectl apply -f k8s/deployment-sub.yaml + +substitutions: + _PROJECT_ID: 'DFS project ID' + _REGION: 'us-east' + _REPO: 'git-repo' + _IMAGE: 'docker-image-name' + _CLUSTER_NAME: 'gke-cluster' + +timeout: '3600s' diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 0000000..d8698b9 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,55 @@ +#!/bin/bash +set -e + +# --- CONFIGURATION --- +PROJECT_ID="your-gcp-project-id" +REGION="your-region" # e.g., us-central1 +REPO="your-artifact-registry-repo" +IMAGE="your-image-name" +CLUSTER_NAME="your-gke-cluster" +K8S_MANIFEST="k8s/deployment.yaml" +SERVICE_ACCOUNT_KEY_PATH="path/to/your-service-account-key.json" # Path to your service account JSON key + +# --- STEP 0: Check Required Tools --- +for cmd in gcloud kubectl mvn git; do + if ! command -v $cmd &> /dev/null; then + echo "Error: $cmd is not installed or not in PATH." + exit 1 + fi +done + +# --- STEP 1: Authenticate gcloud with Service Account --- +gcloud auth activate-service-account --key-file="$SERVICE_ACCOUNT_KEY_PATH" +gcloud config set project "$PROJECT_ID" + +# --- STEP 2: Authenticate Docker --- +gcloud auth configure-docker "${REGION}-docker.pkg.dev" --quiet + +# --- STEP 3: Authenticate kubectl with GKE --- +gcloud container clusters get-credentials "$CLUSTER_NAME" --region "$REGION" --project "$PROJECT_ID" + +# --- STEP 4: Authenticate git --- +if ! git ls-remote &> /dev/null; then + echo "Error: git authentication failed. Ensure credentials are available in CI/CD environment." + exit 1 +fi + +# --- STEP 5: Get current commit SHA --- +COMMIT_SHA=$(git rev-parse --short HEAD) +IMAGE_TAG="${REGION}-docker.pkg.dev/${PROJECT_ID}/${REPO}/${IMAGE}:${COMMIT_SHA}" + + +# --- STEP 6: Build and push Docker image with Maven Jib --- +mvn compile jib:build -Dimage="$IMAGE_TAG" + +# --- STEP 7: Update Kubernetes manifest with new image tag --- +cp "$K8S_MANIFEST" "${K8S_MANIFEST}.bak" +sed -i "s|IMAGE_PLACEHOLDER|$IMAGE_TAG|g" "$K8S_MANIFEST" + +# --- STEP 8: Deploy to GKE --- +kubectl apply -f "$K8S_MANIFEST" + +# --- STEP 9: Clean up (restore manifest) --- +mv "${K8S_MANIFEST}.bak" "$K8S_MANIFEST" + +echo "Pipeline finished." diff --git a/deployment.yaml b/deployment.yaml new file mode 100644 index 0000000..eea7bae --- /dev/null +++ b/deployment.yaml @@ -0,0 +1,31 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: DFS + labels: + app: DFS +spec: + replicas: 2 + selector: + matchLabels: + app: DFS + template: + metadata: + labels: + app: DFS + spec: + containers: + - name: DFS + image: IMAGE_PLACEHOLDER # replace with all code in pipeline + ports: + - containerPort: 8080 + resources: + requests: + cpu: "250m" + memory: "512Mi" + limits: + cpu: "500m" + memory: "1Gi" + env: + - name: ENVIRONMENT + value: "production" diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..50c1a07 --- /dev/null +++ b/pom.xml @@ -0,0 +1,56 @@ + + 4.0.0 + + com.example + my-app + 1.0.0 + jar + + DFS IAM + + + 17 + 3.2.5 + 3.4.1 + + + + + + org.springframework.boot + spring-boot-starter-web + ${spring-boot.version} + + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot.version} + + + + com.google.cloud.tools + jib-maven-plugin + ${jib-maven-plugin.version} + + + eclipse-temurin:17-jre + + + + 8080 + + + production + + + + + + +