Cloud Run Example
This example demonstrates how to deploy a Go Fiber application to Google Cloud Run.
Description
This project provides a starting point for deploying a Go Fiber application to Google Cloud Run. It includes necessary configuration files and scripts to build and deploy the application using Docker and Google Cloud Build.
Requirements
- Go 1.18 or higher
- Docker
- Google Cloud SDK
- Git
Setup
-
Clone the repository:
git clone https://github.com/gofiber/recipes.git
cd recipes/cloud-run -
Install the dependencies:
go mod download
-
Build the Docker image:
docker build -t cloud-run-example .
-
Run the Docker container:
docker run -p 3000:3000 cloud-run-example
The application should now be running on http://localhost:3000
.
Deploy to Google Cloud Run
-
Set up Google Cloud SDK and authenticate:
gcloud auth login
gcloud config set project [YOUR_PROJECT_ID] -
Build and push the Docker image using Google Cloud Build:
gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/cloud-run-example
-
Deploy the image to Cloud Run:
gcloud run deploy cloud-run-example --image gcr.io/[YOUR_PROJECT_ID]/cloud-run-example --platform managed --region [YOUR_REGION] --allow-unauthenticated
Replace [YOUR_PROJECT_ID]
and [YOUR_REGION]
with your Google Cloud project ID and desired region.
Cloud Build Configuration
The cloudbuild.yaml
file defines the steps to build and deploy the application using Google Cloud Build:
steps:
- name: 'gcr.io/kaniko-project/executor:latest'
id: 'build-and-push'
args:
- '--destination=asia.gcr.io/$PROJECT_ID/$_SERVICE_NAME:$SHORT_SHA'
- '--destination=asia.gcr.io/$PROJECT_ID/$_SERVICE_NAME:latest'
- '--dockerfile=Dockerfile'
- '--context=.'
- '--cache=true'
- '--cache-ttl=120h'
- id: 'Deploy to Cloud Run'
name: 'gcr.io/cloud-builders/gcloud'
entrypoint: 'bash'
args:
- '-c'
- |
gcloud run deploy $_SERVICE_NAME \
--image=asia.gcr.io/$PROJECT_ID/$_SERVICE_NAME:$SHORT_SHA \
--region=$_REGION --platform managed --allow-unauthenticated \
--port=3000
options:
substitutionOption: ALLOW_LOOSE
substitutions:
_SERVICE_NAME: cloud-run-example
_REGION: asia-southeast1
Example Usage
-
Open your browser and navigate to the Cloud Run service URL provided after deployment.
-
You should see the message:
Hello, World!
.
Conclusion
This example provides a basic setup for deploying a Go Fiber application to Google Cloud Run. It can be extended and customized further to fit the needs of more complex applications.