scripting for image building and pushing

This commit is contained in:
johnpccd 2025-05-23 23:05:10 +02:00
parent d2a7f17752
commit d117cf6965
8 changed files with 44 additions and 118 deletions

View File

@ -6,7 +6,7 @@ spec:
template: template:
spec: spec:
containers: containers:
- image: gcr.io/your-project/sereact:latest - image: gcr.io/gen-lang-client-0424120530/sereact-api:latest
ports: ports:
- containerPort: 8000 - containerPort: 8000
resources: resources:

View File

@ -1,19 +1,19 @@
steps: steps:
# Build the container image # Build the container image
- name: 'gcr.io/cloud-builders/docker' - name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/sereact:$COMMIT_SHA', '.'] args: ['build', '-t', 'gcr.io/$PROJECT_ID/sereact-api:$COMMIT_SHA', '.']
# Push the container image to Container Registry # Push the container image to Container Registry
- name: 'gcr.io/cloud-builders/docker' - name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/sereact:$COMMIT_SHA'] args: ['push', 'gcr.io/$PROJECT_ID/sereact-api:$COMMIT_SHA']
# Tag the image as latest # Tag the image as latest
- name: 'gcr.io/cloud-builders/docker' - name: 'gcr.io/cloud-builders/docker'
args: ['tag', 'gcr.io/$PROJECT_ID/sereact:$COMMIT_SHA', 'gcr.io/$PROJECT_ID/sereact:latest'] args: ['tag', 'gcr.io/$PROJECT_ID/sereact-api:$COMMIT_SHA', 'gcr.io/$PROJECT_ID/sereact-api:latest']
# Push the latest tag # Push the latest tag
- name: 'gcr.io/cloud-builders/docker' - name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/sereact:latest'] args: ['push', 'gcr.io/$PROJECT_ID/sereact-api:latest']
# Deploy to Cloud Run # Deploy to Cloud Run
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk' - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
@ -22,7 +22,7 @@ steps:
- 'run' - 'run'
- 'deploy' - 'deploy'
- 'sereact' - 'sereact'
- '--image=gcr.io/$PROJECT_ID/sereact:$COMMIT_SHA' - '--image=gcr.io/$PROJECT_ID/sereact-api:$COMMIT_SHA'
- '--region=us-central1' - '--region=us-central1'
- '--platform=managed' - '--platform=managed'
- '--allow-unauthenticated' - '--allow-unauthenticated'
@ -40,8 +40,8 @@ steps:
# Store images in Container Registry # Store images in Container Registry
images: images:
- 'gcr.io/$PROJECT_ID/sereact:$COMMIT_SHA' - 'gcr.io/$PROJECT_ID/sereact-api:$COMMIT_SHA'
- 'gcr.io/$PROJECT_ID/sereact:latest' - 'gcr.io/$PROJECT_ID/sereact-api:latest'
# Substitution variables to be set in the Cloud Build trigger # Substitution variables to be set in the Cloud Build trigger
substitutions: substitutions:

View File

@ -3,7 +3,7 @@ set -e
# Configuration # Configuration
PROJECT_ID=$(gcloud config get-value project) PROJECT_ID=$(gcloud config get-value project)
IMAGE_NAME="sereact" IMAGE_NAME="sereact-api"
REGION="us-central1" REGION="us-central1"
SERVICE_NAME="sereact" SERVICE_NAME="sereact"
@ -103,7 +103,7 @@ if [ "$BUILD" = true ]; then
LATEST_IMAGE_NAME="gcr.io/$PROJECT_ID/$IMAGE_NAME:latest" LATEST_IMAGE_NAME="gcr.io/$PROJECT_ID/$IMAGE_NAME:latest"
echo "Building image: $FULL_IMAGE_NAME" echo "Building image: $FULL_IMAGE_NAME"
docker build -t "$FULL_IMAGE_NAME" -t "$LATEST_IMAGE_NAME" . docker build -t "$FULL_IMAGE_NAME" -t "$LATEST_IMAGE_NAME" -f Dockerfile .
echo "Pushing images to Container Registry..." echo "Pushing images to Container Registry..."
docker push "$FULL_IMAGE_NAME" docker push "$FULL_IMAGE_NAME"
@ -122,7 +122,17 @@ if [ "$DEPLOY" = true ]; then
# Check if we have an existing service.yaml to use # Check if we have an existing service.yaml to use
if [ -f "$(dirname "$0")/cloud-run/service.yaml" ]; then if [ -f "$(dirname "$0")/cloud-run/service.yaml" ]; then
echo "Deploying using service.yaml configuration..." echo "Deploying using service.yaml configuration..."
gcloud run services replace "$(dirname "$0")/cloud-run/service.yaml" --region="$REGION" # Update image in service.yaml before deploying
YAML_FILE="$(dirname "$0")/cloud-run/service.yaml"
# Create a temporary file for sed operation
TMP_FILE=$(mktemp)
# Replace the image line in the YAML file
sed "s|image: gcr.io/[^/]*/[^:]*:.*|image: $LATEST_IMAGE_NAME|" "$YAML_FILE" > "$TMP_FILE"
# Replace the original file
cat "$TMP_FILE" > "$YAML_FILE"
rm "$TMP_FILE"
# Deploy with the updated YAML
gcloud run services replace "$YAML_FILE" --region="$REGION"
else else
echo "Deploying directly..." echo "Deploying directly..."
gcloud run deploy "$SERVICE_NAME" \ gcloud run deploy "$SERVICE_NAME" \

View File

@ -39,7 +39,7 @@ resource "google_firestore_database" "database" {
} }
# Container Registry - no explicit resource needed, just enable the API # Container Registry - no explicit resource needed, just enable the API
# You'll push images to gcr.io/${var.project_id}/sereact # You'll push images to gcr.io/${var.project_id}/sereact-api
# Cloud Run service # Cloud Run service
resource "google_cloud_run_service" "sereact" { resource "google_cloud_run_service" "sereact" {
@ -49,18 +49,17 @@ resource "google_cloud_run_service" "sereact" {
template { template {
spec { spec {
containers { containers {
# Using a public placeholder image that exists to avoid deployment failure # Use our optimized image
# You'll need to update this later with your actual image image = "gcr.io/${var.project_id}/sereact-api:latest"
image = "gcr.io/google-samples/hello-app:1.0"
ports { ports {
container_port = 8080 container_port = 8000
} }
resources { resources {
limits = { limits = {
cpu = "1" cpu = "1"
memory = "512Mi" memory = "1Gi"
} }
} }

View File

@ -4,9 +4,10 @@ set -e
# Set defaults # Set defaults
IMAGE_NAME=${IMAGE_NAME:-"sereact-api"} IMAGE_NAME=${IMAGE_NAME:-"sereact-api"}
IMAGE_TAG=${IMAGE_TAG:-"latest"} IMAGE_TAG=${IMAGE_TAG:-"latest"}
PROJECT_ID=${PROJECT_ID:-"gen-lang-client-0424120530"}
# Allow custom registry (defaults to DockerHub) # Allow custom registry (defaults to DockerHub)
REGISTRY=${REGISTRY:-""} REGISTRY=${REGISTRY:-"gcr.io/${PROJECT_ID}"}
REGISTRY_PREFIX="" REGISTRY_PREFIX=""
if [ -n "$REGISTRY" ]; then if [ -n "$REGISTRY" ]; then
REGISTRY_PREFIX="${REGISTRY}/" REGISTRY_PREFIX="${REGISTRY}/"

View File

@ -1,57 +0,0 @@
#!/bin/bash
set -e
# Default values
PROJECT_ID=${PROJECT_ID:-""}
REGION=${REGION:-"us-central1"}
SERVICE_CONFIG=${SERVICE_CONFIG:-"deployment/cloud-run/service.yaml"}
IMAGE_NAME=${IMAGE_NAME:-"sereact-api"}
IMAGE_TAG=${IMAGE_TAG:-"latest"}
REGISTRY=${REGISTRY:-"gcr.io"}
# Validate required parameters
if [ -z "$PROJECT_ID" ]; then
echo "Error: PROJECT_ID environment variable is required"
echo "Usage: PROJECT_ID=your-project-id ./scripts/deploy-to-cloud-run.sh"
exit 1
fi
# Full image reference
FULL_IMAGE_NAME="${REGISTRY}/${PROJECT_ID}/${IMAGE_NAME}:${IMAGE_TAG}"
# Check if service config exists
if [ ! -f "$SERVICE_CONFIG" ]; then
echo "Error: Service configuration file not found at $SERVICE_CONFIG"
exit 1
fi
# Build the image if BUILD=true
if [ "${BUILD:-false}" = "true" ]; then
echo "Building Docker image: ${FULL_IMAGE_NAME}"
docker build -t "${FULL_IMAGE_NAME}" -f Dockerfile .
echo "Build completed successfully"
fi
# Push the image if PUSH=true
if [ "${PUSH:-false}" = "true" ]; then
echo "Pushing image to registry: ${FULL_IMAGE_NAME}"
docker push "${FULL_IMAGE_NAME}"
echo "Image pushed successfully"
fi
# Update the image in the service configuration
echo "Updating image reference in service configuration..."
TMP_CONFIG=$(mktemp)
sed "s|image: .*|image: ${FULL_IMAGE_NAME}|g" "$SERVICE_CONFIG" > "$TMP_CONFIG"
echo "Deploying to Cloud Run using configuration..."
gcloud run services replace "$TMP_CONFIG" \
--project="$PROJECT_ID" \
--region="$REGION" \
--platform=managed
rm "$TMP_CONFIG"
echo "Deployment completed successfully"
echo "Service URL: $(gcloud run services describe sereact --region=${REGION} --project=${PROJECT_ID} --format='value(status.url)')"
echo "To view logs: gcloud logging read 'resource.type=cloud_run_revision AND resource.labels.service_name=sereact' --project=$PROJECT_ID --limit=10"

View File

@ -1,43 +0,0 @@
#!/bin/bash
set -e
# Source the build environment to reuse variables
source "$(dirname "$0")/build.sh"
# Push the Docker image to the registry
echo "Pushing image: ${FULL_IMAGE_NAME} to registry..."
docker push "${FULL_IMAGE_NAME}"
echo "Image pushed successfully"
# Check if we need to deploy to Cloud Run
DEPLOY_TO_CLOUD_RUN=${DEPLOY_TO_CLOUD_RUN:-false}
if [ "$DEPLOY_TO_CLOUD_RUN" = true ]; then
echo "Deploying to Cloud Run..."
# Cloud Run settings
PROJECT_ID=${PROJECT_ID:-""}
REGION=${REGION:-"us-central1"}
SERVICE_NAME=${SERVICE_NAME:-"sereact-api"}
if [ -z "$PROJECT_ID" ]; then
echo "Error: PROJECT_ID environment variable is required for Cloud Run deployment"
exit 1
fi
# Deploy to Cloud Run
gcloud run deploy "${SERVICE_NAME}" \
--image="${FULL_IMAGE_NAME}" \
--platform=managed \
--region="${REGION}" \
--project="${PROJECT_ID}" \
--allow-unauthenticated \
--port=8000
echo "Deployment to Cloud Run completed"
echo "Service URL: $(gcloud run services describe ${SERVICE_NAME} --region=${REGION} --project=${PROJECT_ID} --format='value(status.url)')"
else
echo ""
echo "To deploy to Cloud Run:"
echo "DEPLOY_TO_CLOUD_RUN=true PROJECT_ID=your-project-id ./scripts/deploy.sh"
fi

16
scripts/push.sh Normal file
View File

@ -0,0 +1,16 @@
#!/bin/bash
set -e
# Source the build environment to reuse variables
source "$(dirname "$0")/build.sh"
# Push the Docker image to the registry
echo "Pushing image: ${FULL_IMAGE_NAME} to registry..."
docker push "${FULL_IMAGE_NAME}"
echo "Image pushed successfully"
echo ""
echo "Image pushed to: ${FULL_IMAGE_NAME}"
echo ""
echo "To deploy to Cloud Run:"
echo "./scripts/deploy.sh"