Skip to content

Deploy to Cloud Run #21

Deploy to Cloud Run

Deploy to Cloud Run #21

Workflow file for this run

name: Deploy to Cloud Run
on:
workflow_dispatch:
inputs:
environment:
description: "Deployment environment (e.g., staging, production)"
required: true
default: "staging"
branch:
description: "Branch to deploy (e.g., main, develop)"
required: true
default: "main"
env:
SERVICE_NAME: ${{ secrets.CLOUD_RUN_SERVICE }}
REGION: ${{ secrets.REGION }}
PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
jobs:
deploy:
runs-on: ubuntu-latest
steps:
# Step 1: Checkout the code from the target branch
- name: Checkout repository
uses: actions/checkout@v3
with:
ref: ${{ github.event.inputs.branch }}
# Step 2: Authenticate with Google Cloud
- name: Authenticate with Google Cloud
uses: google-github-actions/auth@v1
with:
project_id: ${{ env.PROJECT_ID }}
credentials_json: ${{secrets.GCP_SERVICE_ACCOUNT_KEY}}
# Step 3: Configure gcloud CLI
- name: Set up gcloud CLI
run: |
gcloud config set project ${{ secrets.GCP_PROJECT_ID }}
gcloud auth configure-docker
# Step 4: Get Git commit hash for versioning
- name: Get short Git commit hash
id: commit
run: |
echo "GIT_COMMIT_HASH=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
# Step 5: Build Docker image with versioning
- name: Build Docker image
run: |
IMAGE_NAME=gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{env.SERVICE_NAME}}-${{ github.event.inputs.environment }}
IMAGE_TAG="${{ github.event.inputs.branch }}-${{ github.sha }}"
docker build -t $IMAGE_NAME:$IMAGE_TAG .
# Step 6: Push Docker image with tag to GCR
- name: Push Docker image to GCR
run: |
IMAGE_TAG="${{ github.event.inputs.branch }}-${{ github.sha }}"
IMAGE_NAME=gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{env.SERVICE_NAME}}-${{ github.event.inputs.environment }}
docker push $IMAGE_NAME:$IMAGE_TAG
# Step 7: Deploy to Cloud Run
- name: Deploy to Cloud Run
run: |
IMAGE_TAG="${{ github.event.inputs.branch }}-${{ github.sha }}"
gcloud run deploy ${{env.SERVICE_NAME}}-${{ github.event.inputs.environment }} \
--image gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{env.SERVICE_NAME}}-${{ github.event.inputs.environment }}:$IMAGE_TAG \
--region ${{ env.REGION }} \
--platform managed \
--allow-unauthenticated \
--set-env-vars SPRING_DATA_MONGODB_URI=mongodb+srv://droiddumbledore:[email protected]/?retryWrites=true&w=majority&appName=QuashMagnusStage,SPRING_DATA_MONGODB_DATABASE=QuashMagnusStage,SPRING_PROFILES_ACTIVE=${{ github.event.inputs.environment }}
# Step 8: Verify the deployment
- name: Verify deployment
run: |
echo "Deployment complete. Access your service at:"
gcloud run services describe ${{env.SERVICE_NAME}}-${{ github.event.inputs.environment }} --region=${{ env.REGION }} --format='value(status.url)'
- name: Cleanup older revisions
run: |
SERVICE_NAME=${{env.SERVICE_NAME}}-${{ github.event.inputs.environment }}
REGION=${{ env.REGION }}
# List all revisions, sorted by creation timestamp in descending order
gcloud run revisions list \
--service=$SERVICE_NAME \
--region=$REGION \
--format="value(METADATA.name)" \
--sort-by=~CREATED_AT \
| tail -n +4