SOP-002: Deploy from Source Code
DOCUMENT CONTROL
| Field | Value |
|---|---|
| SOP ID | SOP-002 |
| Version | 1.0 |
| Status | Active |
Purpose
Deploy applications directly from source code without building Docker images locally. Cloud Run uses Google Cloud Buildpacks to automatically containerize your application.
Supported Languages
| Language | Supported Versions |
|---|---|
| Go | 1.11+ |
| Node.js | 10+ |
| Python | 3.7+ |
| Java | 8, 11, 17, 21 |
| .NET Core | 3.1, 6.0, 7.0, 8.0 |
| Ruby | 2.5+ |
Prerequisites
- Source code with proper entry point
gcloudCLI authenticated- Cloud Build API enabled
Flowchart
┌─────────────────┐
│ Prepare Source │
│ Code │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Enable APIs │
│ (Run + Build) │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Run gcloud │
│ deploy --source│
└────────┬────────┘
│
▼
┌─────────────────┐
│ Cloud Build │
│ Builds Image │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Service Live │
└─────────────────┘Procedure
Step 1: Prepare Your Application
python
import os
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello from Cloud Run!'
if __name__ == '__main__':
port = int(os.environ.get('PORT', 8080))
app.run(host='0.0.0.0', port=port)javascript
const express = require('express');
const app = express();
const PORT = process.env.PORT || 8080;
app.get('/', (req, res) => {
res.send('Hello from Cloud Run!');
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});go
package main
import (
"fmt"
"net/http"
"os"
)
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello from Cloud Run!")
})
http.ListenAndServe(":"+port, nil)
}PORT REQUIREMENT
Your application MUST listen on the port specified by the PORT environment variable (default: 8080).
Step 2: Enable Required APIs
bash
gcloud services enable run.googleapis.com cloudbuild.googleapis.comStep 3: Deploy from Source
bash
# Navigate to your source directory
cd /path/to/your/app
# Deploy with source
gcloud run deploy my-service \
--source . \
--region us-central1 \
--allow-unauthenticatedCloud Build will:
- Upload your source code
- Detect the language
- Build a container image
- Push to Artifact Registry
- Deploy to Cloud Run
Step 4: Monitor Build Progress
bash
# View build logs
gcloud builds list --limit 5
# Stream logs from latest build
gcloud builds log $(gcloud builds list --limit 1 --format 'value(id)')Language-Specific Requirements
Python
txt
# requirements.txt
flask==3.0.0
gunicorn==21.0.0Create Procfile:
web: gunicorn --bind :$PORT app:appNode.js
json
{
"name": "my-app",
"scripts": {
"start": "node index.js"
},
"engines": {
"node": ">=18"
}
}Go
// go.mod
module my-app
go 1.21Verification Checklist
- [ ] Build completed successfully in Cloud Build
- [ ] Container image pushed to Artifact Registry
- [ ] Service responds at generated URL
- [ ] No errors in Cloud Logging
- [ ]
PORTenvironment variable handled correctly
Troubleshooting
| Issue | Solution |
|---|---|
| Build fails | Check language requirements and dependencies |
| Port binding error | Ensure app listens on $PORT |
| Permission denied | Grant Cloud Build service account permissions |
| Timeout | Increase --timeout flag value |