This guide helps you set up automatic deployments to Google Cloud Run whenever you push to the main branch.
- Your code in a GitHub repository
- Google Cloud project already set up
- MongoDB Atlas database configured
# Set your project ID
export PROJECT_ID="your-project-id"
gcloud config set project $PROJECT_ID
# Create service account
gcloud iam service-accounts create github-actions \
--display-name="GitHub Actions Deployer"
# Grant necessary permissions
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:github-actions@${PROJECT_ID}.iam.gserviceaccount.com" \
--role="roles/run.admin"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:github-actions@${PROJECT_ID}.iam.gserviceaccount.com" \
--role="roles/storage.admin"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:github-actions@${PROJECT_ID}.iam.gserviceaccount.com" \
--role="roles/iam.serviceAccountUser"
# Create and download key
gcloud iam service-accounts keys create key.json \
--iam-account=github-actions@${PROJECT_ID}.iam.gserviceaccount.comGo to your GitHub repository → Settings → Secrets and variables → Actions → New repository secret
Add these secrets:
-
GCP_PROJECT_ID
- Value: Your Google Cloud project ID
-
GCP_SA_KEY
- Value: Contents of the
key.jsonfile you just created - Copy the entire JSON content
- Value: Contents of the
-
MONGODB_URI
- Value: Your MongoDB Atlas connection string
- Example:
mongodb+srv://username:password@cluster0.xxxxx.mongodb.net/enclope
rm key.jsonThe key is now safely stored in GitHub Secrets.
The workflow file is already in .github/workflows/deploy.yml
git add .
git commit -m "Add Google Cloud deployment configuration"
git push origin main- On every push to
mainbranch (or manual trigger) - GitHub Actions checks out your code
- Authenticates with Google Cloud using the service account
- Deploys backend to Cloud Run
- Deploys frontend to Cloud Run
- Prints the frontend URL
- Go to your GitHub repository
- Click "Actions" tab
- See deployment progress in real-time
You can also manually trigger deployment:
- Go to Actions tab
- Select "Deploy to Google Cloud Run"
- Click "Run workflow"
- Choose branch and run
Edit .github/workflows/deploy.yml to:
- Change deployment region
- Adjust memory/CPU limits
- Add environment variables
- Deploy on different branches
- Add tests before deployment
- name: Run tests
run: |
cd server
npm install
npm teston:
push:
branches:
- main # Production
- staging # Staging environment- Verify
GCP_SA_KEYsecret contains valid JSON - Check service account has required permissions
- Check GitHub Actions logs
- Verify secrets are set correctly
- Ensure APIs are enabled in Google Cloud
- Verify
MONGODB_URIsecret is correct - Check MongoDB Atlas IP whitelist
✅ DO:
- Use GitHub Secrets for sensitive data
- Use service accounts with minimal permissions
- Rotate service account keys periodically
- Review deployment logs regularly
❌ DON'T:
- Commit credentials to repository
- Share service account keys
- Use overly permissive IAM roles
- Store secrets in code or config files
GitHub Actions is free for public repositories with limits:
- 2,000 minutes/month for private repos
- Unlimited for public repos
Google Cloud Run stays free within usage limits.
To deploy only when you create a release tag:
on:
release:
types: [published]Then create releases:
git tag v1.0.0
git push origin v1.0.0For manual deployment, see QUICK_START.md