Skip to content

SOP-002: Deploy from Source Code

DOCUMENT CONTROL

FieldValue
SOP IDSOP-002
Version1.0
StatusActive

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

LanguageSupported Versions
Go1.11+
Node.js10+
Python3.7+
Java8, 11, 17, 21
.NET Core3.1, 6.0, 7.0, 8.0
Ruby2.5+

Prerequisites

  • Source code with proper entry point
  • gcloud CLI 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.com

Step 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-unauthenticated

Cloud Build will:

  1. Upload your source code
  2. Detect the language
  3. Build a container image
  4. Push to Artifact Registry
  5. 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.0

Create Procfile:

web: gunicorn --bind :$PORT app:app

Node.js

json
{
  "name": "my-app",
  "scripts": {
    "start": "node index.js"
  },
  "engines": {
    "node": ">=18"
  }
}

Go

// go.mod
module my-app

go 1.21

Verification Checklist

  • [ ] Build completed successfully in Cloud Build
  • [ ] Container image pushed to Artifact Registry
  • [ ] Service responds at generated URL
  • [ ] No errors in Cloud Logging
  • [ ] PORT environment variable handled correctly

Troubleshooting

IssueSolution
Build failsCheck language requirements and dependencies
Port binding errorEnsure app listens on $PORT
Permission deniedGrant Cloud Build service account permissions
TimeoutIncrease --timeout flag value