|
| 1 | +# CI/CD Setup for GDG Docs |
| 2 | + |
| 3 | +## Overview |
| 4 | +This repository uses GitHub Actions for automatic deployment to Google Cloud Run. Every push to the `main` branch triggers a build, test, and deployment pipeline. |
| 5 | + |
| 6 | +## Pipeline Workflow |
| 7 | + |
| 8 | +### 1. Build and Test Job |
| 9 | +- ✅ Builds Docker image |
| 10 | +- ✅ Tests the container locally |
| 11 | +- ✅ Uploads image artifact for deployment |
| 12 | + |
| 13 | +### 2. Deploy Job (only on main branch) |
| 14 | +- ✅ Authenticates with Google Cloud |
| 15 | +- ✅ Pushes image to Google Container Registry (GCR) |
| 16 | +- ✅ Deploys to Cloud Run |
| 17 | +- ✅ Provides deployment summary with URLs |
| 18 | + |
| 19 | +## Setup Instructions |
| 20 | + |
| 21 | +### Prerequisites |
| 22 | +1. Google Cloud Project with Cloud Run API enabled |
| 23 | +2. Service account with necessary permissions |
| 24 | +3. GitHub repository with Actions enabled |
| 25 | + |
| 26 | +### Step 1: Create Google Cloud Service Account |
| 27 | + |
| 28 | +```bash |
| 29 | +# Set your project ID |
| 30 | +export PROJECT_ID="erudite-marker-465011-c6" |
| 31 | + |
| 32 | +# Create service account |
| 33 | +gcloud iam service-accounts create github-actions \ |
| 34 | + --display-name="GitHub Actions CI/CD" \ |
| 35 | + --project=$PROJECT_ID |
| 36 | + |
| 37 | +# Grant necessary permissions |
| 38 | +gcloud projects add-iam-policy-binding $PROJECT_ID \ |
| 39 | + --member="serviceAccount:github-actions@${PROJECT_ID}.iam.gserviceaccount.com" \ |
| 40 | + --role="roles/run.admin" |
| 41 | + |
| 42 | +gcloud projects add-iam-policy-binding $PROJECT_ID \ |
| 43 | + --member="serviceAccount:github-actions@${PROJECT_ID}.iam.gserviceaccount.com" \ |
| 44 | + --role="roles/storage.admin" |
| 45 | + |
| 46 | +gcloud projects add-iam-policy-binding $PROJECT_ID \ |
| 47 | + --member="serviceAccount:github-actions@${PROJECT_ID}.iam.gserviceaccount.com" \ |
| 48 | + --role="roles/iam.serviceAccountUser" |
| 49 | + |
| 50 | +# Create and download service account key |
| 51 | +gcloud iam service-accounts keys create github-actions-key.json \ |
| 52 | + --iam-account=github-actions@${PROJECT_ID}.iam.gserviceaccount.com |
| 53 | +``` |
| 54 | + |
| 55 | +### Step 2: Add GitHub Secret |
| 56 | + |
| 57 | +1. Go to your GitHub repository settings |
| 58 | +2. Navigate to **Settings → Secrets and variables → Actions** |
| 59 | +3. Click **New repository secret** |
| 60 | +4. Name: `GCP_SA_KEY` |
| 61 | +5. Value: Paste the entire contents of `github-actions-key.json` |
| 62 | +6. Click **Add secret** |
| 63 | + |
| 64 | +**⚠️ Important:** Delete the `github-actions-key.json` file after adding it to GitHub: |
| 65 | +```bash |
| 66 | +rm github-actions-key.json |
| 67 | +``` |
| 68 | + |
| 69 | +### Step 3: Verify Workflow File |
| 70 | + |
| 71 | +The workflow is already configured in `.github/workflows/ci.yml`. It will: |
| 72 | +- Trigger on every push to `main` branch |
| 73 | +- Build and test the Docker image |
| 74 | +- Deploy to Cloud Run (only on main branch pushes) |
| 75 | + |
| 76 | +### Step 4: Test the Pipeline |
| 77 | + |
| 78 | +1. Make a change to your repository |
| 79 | +2. Commit and push to `main` branch: |
| 80 | + ```bash |
| 81 | + git add . |
| 82 | + git commit -m "Test CI/CD pipeline" |
| 83 | + git push origin main |
| 84 | + ``` |
| 85 | +3. Go to **Actions** tab in GitHub to see the workflow running |
| 86 | +4. Once complete, check the deployment summary for the service URL |
| 87 | + |
| 88 | +## Environment Variables |
| 89 | + |
| 90 | +The following environment variables are set automatically: |
| 91 | +- `PROJECT_ID`: Your Google Cloud project ID |
| 92 | +- `SERVICE_NAME`: Cloud Run service name (gdg-docs) |
| 93 | +- `REGION`: Deployment region (us-central1) |
| 94 | +- `IMAGE_NAME`: Full GCR image path |
| 95 | + |
| 96 | +## Manual Deployment (Fallback) |
| 97 | + |
| 98 | +If you need to deploy manually, you can still use: |
| 99 | + |
| 100 | +```bash |
| 101 | +# Using gcloud CLI |
| 102 | +gcloud builds submit --config cloudbuild.yaml . |
| 103 | + |
| 104 | +# Or using the deploy script |
| 105 | +bash deploy.sh |
| 106 | +``` |
| 107 | + |
| 108 | +## Monitoring Deployments |
| 109 | + |
| 110 | +### View Deployment History |
| 111 | +```bash |
| 112 | +gcloud run revisions list --service=gdg-docs --region=us-central1 |
| 113 | +``` |
| 114 | + |
| 115 | +### View Service Logs |
| 116 | +```bash |
| 117 | +gcloud run services logs read gdg-docs --region=us-central1 |
| 118 | +``` |
| 119 | + |
| 120 | +### Check Service Status |
| 121 | +```bash |
| 122 | +gcloud run services describe gdg-docs --region=us-central1 |
| 123 | +``` |
| 124 | + |
| 125 | +## Troubleshooting |
| 126 | + |
| 127 | +### Build Fails |
| 128 | +- Check Docker build locally: `docker build -t gdg-docs .` |
| 129 | +- Review Dockerfile for errors |
| 130 | +- Check GitHub Actions logs for detailed error messages |
| 131 | + |
| 132 | +### Authentication Fails |
| 133 | +- Verify `GCP_SA_KEY` secret is correctly set |
| 134 | +- Ensure service account has proper permissions |
| 135 | +- Check if APIs are enabled in Google Cloud |
| 136 | + |
| 137 | +### Deployment Fails |
| 138 | +- Verify Cloud Run API is enabled |
| 139 | +- Check service account permissions |
| 140 | +- Review Cloud Run logs: `gcloud run services logs read gdg-docs` |
| 141 | + |
| 142 | +### Container Doesn't Start |
| 143 | +- Test container locally: `docker run -p 8080:8080 gdg-docs` |
| 144 | +- Check port configuration (must be 8080) |
| 145 | +- Review container logs in Cloud Run console |
| 146 | + |
| 147 | +## URLs |
| 148 | + |
| 149 | +- **Cloud Run URL**: https://gdg-docs-426637717447.us-central1.run.app |
| 150 | +- **Custom Domain**: https://docs.gdgalgiers.dev |
| 151 | +- **GitHub Actions**: https://github.com/GDGAlgiers/gdg-docs/actions |
| 152 | + |
| 153 | +## Security Notes |
| 154 | + |
| 155 | +- ✅ Service account key is stored securely in GitHub Secrets |
| 156 | +- ✅ Only `main` branch deployments are automated |
| 157 | +- ✅ Pull requests are built and tested but not deployed |
| 158 | +- ✅ Service account has minimal required permissions |
| 159 | + |
| 160 | +## Additional Resources |
| 161 | + |
| 162 | +- [GitHub Actions Documentation](https://docs.github.com/en/actions) |
| 163 | +- [Google Cloud Run Documentation](https://cloud.google.com/run/docs) |
| 164 | +- [Docker Best Practices](https://docs.docker.com/develop/dev-best-practices/) |
0 commit comments