|
| 1 | +# 🚀 GitHub Repository Deployment for Cloud Functions |
| 2 | + |
| 3 | +## 🎯 **Implementation Complete** |
| 4 | + |
| 5 | +Your Cloud Function has been successfully configured to deploy directly from your GitHub repository instead of using local zip files. This eliminates the constant rebuilding issue and creates a more efficient CI/CD workflow. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 🏗️ **What Changed** |
| 10 | + |
| 11 | +### **Before (Local Zip Approach):** |
| 12 | +```terraform |
| 13 | +# Old configuration - caused constant rebuilds |
| 14 | +source { |
| 15 | + storage_source { |
| 16 | + bucket = google_storage_bucket.function_source.name |
| 17 | + object = google_storage_bucket_object.function_source_zip.name |
| 18 | + } |
| 19 | +} |
| 20 | +
|
| 21 | +# Required local zip creation |
| 22 | +data "archive_file" "function_zip" { |
| 23 | + type = "zip" |
| 24 | + output_path = "${path.module}/function-source.zip" |
| 25 | + source_dir = "${path.module}/function" |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +### **After (GitHub Repository Approach):** |
| 30 | +```terraform |
| 31 | +# New configuration - deploys directly from GitHub |
| 32 | +source { |
| 33 | + repo_source { |
| 34 | + project_id = var.project_id |
| 35 | + repo_name = "github_${var.github_owner}_${var.github_repo_name}" |
| 36 | + branch_name = var.deployment_branch |
| 37 | + dir = "terraform/function" |
| 38 | + } |
| 39 | +} |
| 40 | +
|
| 41 | +# Cloud Source Repository mirrors your GitHub repo |
| 42 | +resource "google_sourcerepo_repository" "github_mirror" { |
| 43 | + name = "github_${var.github_owner}_${var.github_repo_name}" |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +## 🔧 **Configuration Added** |
| 50 | + |
| 51 | +### **New Variables:** |
| 52 | +- `github_owner` = "JeanFraga" |
| 53 | +- `github_repo_name` = "agentic-data-science" |
| 54 | +- `deployment_branch` = "main" |
| 55 | + |
| 56 | +### **New APIs Enabled:** |
| 57 | +- `sourcerepo.googleapis.com` - Cloud Source Repositories |
| 58 | + |
| 59 | +### **New IAM Permissions:** |
| 60 | +- `roles/source.admin` - For GitHub Actions to manage repositories |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +## 🚀 **Deployment Process** |
| 65 | + |
| 66 | +### **1. Initial Setup (One-time)** |
| 67 | + |
| 68 | +Run the setup script to prepare GitHub integration: |
| 69 | + |
| 70 | +```powershell |
| 71 | +.\scripts\setup-github-deployment.ps1 -ProjectId "agentic-data-science-460701" |
| 72 | +``` |
| 73 | + |
| 74 | +### **2. Deploy Infrastructure** |
| 75 | + |
| 76 | +```powershell |
| 77 | +cd terraform |
| 78 | +terraform init -backend-config="bucket=agentic-data-science-460701-terraform-state" |
| 79 | +terraform plan |
| 80 | +terraform apply |
| 81 | +``` |
| 82 | + |
| 83 | +### **3. Connect GitHub Repository** |
| 84 | + |
| 85 | +After Terraform creates the Cloud Source Repository, you need to connect it to your GitHub repo: |
| 86 | + |
| 87 | +**Option A: Google Cloud Console** |
| 88 | +1. Go to [Cloud Source Repositories](https://console.cloud.google.com/source/repos) |
| 89 | +2. Find repository: `github_JeanFraga_agentic-data-science` |
| 90 | +3. Click "Connect to GitHub" |
| 91 | +4. Follow the setup wizard to connect your GitHub repository |
| 92 | + |
| 93 | +**Option B: Command Line** |
| 94 | +```bash |
| 95 | +# This will be handled automatically by the Cloud Source Repository |
| 96 | +gcloud source repos create github_JeanFraga_agentic-data-science |
| 97 | +``` |
| 98 | + |
| 99 | +### **4. Automatic Deployment** |
| 100 | + |
| 101 | +Once connected, your Cloud Function will automatically redeploy when you: |
| 102 | +- Push changes to the `main` branch |
| 103 | +- Modify files in the `terraform/function/` directory |
| 104 | + |
| 105 | +--- |
| 106 | + |
| 107 | +## ✅ **Benefits Achieved** |
| 108 | + |
| 109 | +| Aspect | Before | After | |
| 110 | +|--------|--------|-------| |
| 111 | +| **Deployment Triggers** | Every `terraform plan` | Only when code changes | |
| 112 | +| **Source of Truth** | Local files | GitHub repository | |
| 113 | +| **CI/CD Integration** | Manual zip uploads | Automatic on git push | |
| 114 | +| **Version Control** | Hash-based | Git commit-based | |
| 115 | +| **Rollbacks** | Difficult | Easy (git revert) | |
| 116 | +| **Team Collaboration** | File sharing issues | Git-based workflow | |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +## 🔍 **Verification** |
| 121 | + |
| 122 | +### **Check Repository Creation:** |
| 123 | +```powershell |
| 124 | +gcloud source repos list --project=agentic-data-science-460701 |
| 125 | +``` |
| 126 | + |
| 127 | +### **Verify Function Configuration:** |
| 128 | +```powershell |
| 129 | +gcloud functions describe titanic-data-loader --region=us-east1 --gen2 --project=agentic-data-science-460701 |
| 130 | +``` |
| 131 | + |
| 132 | +### **Test Deployment:** |
| 133 | +1. Make a small change to `terraform/function/main.py` |
| 134 | +2. Commit and push to main branch |
| 135 | +3. Watch Cloud Function automatically redeploy |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | +## 🛠️ **Troubleshooting** |
| 140 | + |
| 141 | +### **Repository Not Connected:** |
| 142 | +- Ensure you've completed the GitHub connection step |
| 143 | +- Check IAM permissions for Cloud Source Repositories |
| 144 | +- Verify the repository name matches: `github_JeanFraga_agentic-data-science` |
| 145 | + |
| 146 | +### **Function Build Failures:** |
| 147 | +- Check Cloud Build logs in the Google Cloud Console |
| 148 | +- Ensure `terraform/function/requirements.txt` is valid |
| 149 | +- Verify Python syntax in `terraform/function/main.py` |
| 150 | + |
| 151 | +### **Permission Issues:** |
| 152 | +- Ensure Cloud Build service account has proper permissions |
| 153 | +- Check that GitHub repository is public or properly connected |
| 154 | + |
| 155 | +--- |
| 156 | + |
| 157 | +## 🎉 **Success Indicators** |
| 158 | + |
| 159 | +- ✅ `terraform plan` no longer shows constant function updates |
| 160 | +- ✅ Cloud Source Repository created and connected |
| 161 | +- ✅ Function deploys automatically on git push |
| 162 | +- ✅ No more local zip file generation |
| 163 | +- ✅ Cleaner Terraform state management |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +## 📚 **Related Documentation** |
| 168 | + |
| 169 | +- [Cloud Functions Source Repositories](https://cloud.google.com/functions/docs/deploying/repo) |
| 170 | +- [Cloud Source Repositories](https://cloud.google.com/source-repositories/docs) |
| 171 | +- [GitHub Integration Guide](https://cloud.google.com/source-repositories/docs/connecting-hosted-repositories) |
| 172 | + |
| 173 | +--- |
| 174 | + |
| 175 | +*🎯 GitHub deployment implementation completed successfully!* |
| 176 | +*📅 Updated: May 24, 2025* |
0 commit comments