-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Add CI/CD scripts for Google Cloud deployment #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
taiphanvan2k3
merged 1 commit into
main
from
feat/add-ci-cd-scripts-for-google-cloud-deployment
Nov 14, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| name: Deploy to Google Cloud Artifact Registry | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| - develop | ||
| paths: | ||
| - 'src/**' | ||
| - 'appsettings*.json' | ||
| - 'Dockerfile' | ||
| - 'docker-compose.yml' | ||
| - '.github/workflows/deploy-gcloud.yml' | ||
| pull_request: | ||
| branches: | ||
| - main | ||
| paths: | ||
| - 'src/**' | ||
| - 'appsettings*.json' | ||
| - 'Dockerfile' | ||
| - 'docker-compose.yml' | ||
| workflow_dispatch: | ||
| inputs: | ||
| version_tag: | ||
| description: 'Custom version tag (optional, will use SHA if empty)' | ||
| required: false | ||
| type: string | ||
|
|
||
| env: | ||
| PROJECT_ID: lucky-union-472503-c7 | ||
| REGION: asia-southeast1 | ||
| REPOSITORY: backendnetcore | ||
| IMAGE_NAME: webapi | ||
| SERVICE_NAME: legal-assistant-api | ||
|
|
||
| jobs: | ||
| build-and-deploy: | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: read | ||
| id-token: write | ||
|
|
||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Generate version tag | ||
| id: version | ||
| run: | | ||
| if [ "${{ github.event_name }}" == "workflow_dispatch" ] && [ -n "${{ inputs.version_tag }}" ]; then | ||
| VERSION="${{ inputs.version_tag }}" | ||
| elif [ "${{ github.ref_type }}" == "tag" ]; then | ||
| VERSION="${{ github.ref_name }}" | ||
| else | ||
| # Use date + short SHA for immutable versioning | ||
| VERSION="$(date +%Y%m%d)-${GITHUB_SHA::7}" | ||
| fi | ||
| echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT | ||
| echo "Generated version: ${VERSION}" | ||
|
|
||
| - name: Authenticate to Google Cloud | ||
| uses: google-github-actions/auth@v2 | ||
| with: | ||
| credentials_json: ${{ secrets.GCP_SA_KEY }} | ||
|
|
||
| - name: Set up Cloud SDK | ||
| uses: google-github-actions/setup-gcloud@v2 | ||
|
|
||
| - name: Configure Docker for Artifact Registry | ||
| run: | | ||
| gcloud auth configure-docker ${{ env.REGION }}-docker.pkg.dev --quiet | ||
|
|
||
| - name: Build Docker image | ||
| run: | | ||
| docker build -t ${{ env.IMAGE_NAME }}:latest -f src/Web.Api/Dockerfile . | ||
|
|
||
| - name: Tag Docker image | ||
| run: | | ||
| REMOTE_IMAGE="${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.VERSION }}" | ||
| REMOTE_IMAGE_LATEST="${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.IMAGE_NAME }}:latest" | ||
|
|
||
| docker tag ${{ env.IMAGE_NAME }}:latest ${REMOTE_IMAGE} | ||
| docker tag ${{ env.IMAGE_NAME }}:latest ${REMOTE_IMAGE_LATEST} | ||
|
|
||
| echo "REMOTE_IMAGE=${REMOTE_IMAGE}" >> $GITHUB_ENV | ||
| echo "REMOTE_IMAGE_LATEST=${REMOTE_IMAGE_LATEST}" >> $GITHUB_ENV | ||
|
|
||
| - name: Push Docker image to Artifact Registry | ||
| run: | | ||
| docker push ${{ env.REMOTE_IMAGE }} | ||
| docker push ${{ env.REMOTE_IMAGE_LATEST }} | ||
|
|
||
| - name: Deploy to Cloud Run (Production) | ||
| if: github.ref == 'refs/heads/main' && github.event_name == 'push' | ||
| run: | | ||
| gcloud run deploy ${{ env.SERVICE_NAME }} \ | ||
| --image ${{ env.REMOTE_IMAGE }} \ | ||
| --platform managed \ | ||
| --region ${{ env.REGION }} \ | ||
| --allow-unauthenticated \ | ||
| --memory 512Mi \ | ||
| --cpu 1 \ | ||
| --max-instances 10 \ | ||
| --min-instances 0 \ | ||
| --port 8080 \ | ||
| --set-env-vars "ASPNETCORE_ENVIRONMENT=Production" \ | ||
| --quiet | ||
|
|
||
| - name: Deploy to Cloud Run (Staging) | ||
| if: github.ref == 'refs/heads/develop' && github.event_name == 'push' | ||
| run: | | ||
| gcloud run deploy ${{ env.SERVICE_NAME }}-staging \ | ||
| --image ${{ env.REMOTE_IMAGE }} \ | ||
| --platform managed \ | ||
| --region ${{ env.REGION }} \ | ||
| --allow-unauthenticated \ | ||
| --memory 512Mi \ | ||
| --cpu 1 \ | ||
| --max-instances 5 \ | ||
| --min-instances 0 \ | ||
| --port 8080 \ | ||
| --set-env-vars "ASPNETCORE_ENVIRONMENT=Staging" \ | ||
| --quiet | ||
|
|
||
| - name: Output deployment info | ||
| run: | | ||
| echo "=== Deployment Complete ===" | ||
| echo "Version: ${{ steps.version.outputs.VERSION }}" | ||
| echo "Image: ${{ env.REMOTE_IMAGE }}" | ||
| echo "" | ||
| if [ "${{ github.ref }}" == "refs/heads/main" ]; then | ||
| SERVICE_URL=$(gcloud run services describe ${{ env.SERVICE_NAME }} --region ${{ env.REGION }} --format 'value(status.url)') | ||
| echo "Production URL: ${SERVICE_URL}" | ||
| elif [ "${{ github.ref }}" == "refs/heads/develop" ]; then | ||
| SERVICE_URL=$(gcloud run services describe ${{ env.SERVICE_NAME }}-staging --region ${{ env.REGION }} --format 'value(status.url)') | ||
| echo "Staging URL: ${SERVICE_URL}" | ||
| fi | ||
|
|
||
| - name: Create deployment summary | ||
| run: | | ||
| echo "## Deployment Summary 🚀" >> $GITHUB_STEP_SUMMARY | ||
| echo "" >> $GITHUB_STEP_SUMMARY | ||
| echo "- **Version**: \`${{ steps.version.outputs.VERSION }}\`" >> $GITHUB_STEP_SUMMARY | ||
| echo "- **Image**: \`${{ env.REMOTE_IMAGE }}\`" >> $GITHUB_STEP_SUMMARY | ||
| echo "- **Branch**: \`${{ github.ref_name }}\`" >> $GITHUB_STEP_SUMMARY | ||
| echo "- **Commit**: \`${GITHUB_SHA::7}\`" >> $GITHUB_STEP_SUMMARY | ||
| echo "" >> $GITHUB_STEP_SUMMARY | ||
| if [ "${{ github.ref }}" == "refs/heads/main" ]; then | ||
| echo "- **Environment**: Production" >> $GITHUB_STEP_SUMMARY | ||
| elif [ "${{ github.ref }}" == "refs/heads/develop" ]; then | ||
| echo "- **Environment**: Staging" >> $GITHUB_STEP_SUMMARY | ||
| else | ||
| echo "- **Environment**: Image built only (no deployment)" >> $GITHUB_STEP_SUMMARY | ||
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #!/bin/bash | ||
| gcloud projects add-iam-policy-binding lucky-union-472503-c7 --member="serviceAccount:ci-cd-fucntions-hosting@lucky-union-472503-c7.iam.gserviceaccount.com" --role="roles/run.admin" | ||
|
|
||
| # Tạo key cho service account | ||
| gcloud iam service-accounts keys create github-key.json --iam-account=ci-cd-fucntions-hosting@lucky-union-472503-c7.iam.gserviceaccount.com | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Critical: Service account key will be exposed if not properly excluded from version control.
Creating
github-key.jsondirectly in the repository root is a severe security risk. If this file is not strictly excluded from version control, it will expose GCP credentials in the repository history.github-key.jsonis listed in.gitignoreand add it if missing.secrets.GCP_SA_KEY), never written to disk.🏁 Script executed:
Length of output: 6924
🏁 Script executed:
Length of output: 910
🏁 Script executed:
Length of output: 368
🏁 Script executed:
Length of output: 548
🏁 Script executed:
Length of output: 92
🏁 Script executed:
Length of output: 7577
🏁 Script executed:
Length of output: 389
🏁 Script executed:
Length of output: 486
🏁 Script executed:
Length of output: 343
🏁 Script executed:
Length of output: 280
Add
github-key.jsonto.gitignoreto prevent credential exposure.The critical security issue is that
github-key.jsonis not listed in.gitignore. If this script creates a credentials file in the repository root, it could be accidentally committed, exposing GCP service account credentials in version control history.Add the following line to
.gitignore:Note: The service account name
ci-cd-fucntions-hostingis the actual account in use (not a typo). However, the script also lacks error handling—addset -euo pipefailafter the shebang to ensure the script fails gracefully if any command encounters an error, rather than silently continuing.🤖 Prompt for AI Agents