Skip to content

Deploy to Cloud Run #26

Deploy to Cloud Run

Deploy to Cloud Run #26

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:
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: Set SERVICE_NAME based on environment
- name: Set SERVICE_NAME
run: |
if [ "${{ github.event.inputs.environment }}" = "production" ]; then
echo "SERVICE_NAME=${{ secrets.CLOUD_RUN_PRODUCTION_SERVICE }}" >> $GITHUB_ENV
elif [ "${{ github.event.inputs.environment }}" = "staging" ]; then
echo "SERVICE_NAME=${{ secrets.CLOUD_RUN_STAGING_SERVICE }}" >> $GITHUB_ENV
else
echo "Invalid environment: ${{ github.event.inputs.environment }}. Valid values are 'production' or 'staging'." >&2
exit 1
fi
# Step 3: 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 4: Configure gcloud CLI
- name: Set up gcloud CLI
run: |
gcloud config set project ${{ secrets.GCP_PROJECT_ID }}
gcloud auth configure-docker
# Step 5: 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 6: 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 7: 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 8: Deploy to Cloud Run
- name: Deploy to Cloud Run
run: |
IMAGE_TAG="${{ github.event.inputs.branch }}-${{ github.sha }}"
gcloud run deploy ${{env.SERVICE_NAME}} \
--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 9: 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)'
# Step 10: Clean Older Revisions
- 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 \
| xargs -I {} gcloud run revisions delete {} \
--region=$REGION --quiet
# Notify Success (Send Slack Message)
- name: Notify Slack - Success
if: success()
run: |
curl -X POST -H 'Content-type: application/json' \
--data '{
"text": "✅ *Deployment Successful* \nService: `${{env.SERVICE_NAME}}` \nBranch: `${{ github.event.inputs.branch }}` \nEnvironment: `${{ github.event.inputs.environment }}` \n<https://console.cloud.google.com/run|View Cloud Run>"
}' \
${{ secrets.SLACK_WEBHOOK_URL }}
# Notify Failure (Send Slack Message)
- name: Notify Slack - Failure
if: failure()
run: |
curl -X POST -H 'Content-type: application/json' \
--data '{
"text": "❌ *Deployment Failed* \nService: `${{env.SERVICE_NAME}}` \nBranch: `${{ github.event.inputs.branch }}` \nEnvironment: `${{ github.event.inputs.environment }}` \nCheck the <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|GitHub Actions logs> for details."
}' \
${{ secrets.SLACK_WEBHOOK_URL }}