Skip to content

CI/CD with GitHub and Cloud Build

This project shows how to set up a CI/CD pipeline using Google Cloud Build, automatically triggered on every push to a GitHub repository.

1. Connect GitHub to Cloud Build

  1. Go to Cloud Build triggers
  2. Click "Create Trigger"
  3. Select "Connect Repository"
  4. Choose GitHub (via Cloud Build GitHub App) and follow the instructions to authorize GCP to access your repository

2. Create a Trigger

  • Name: Name it as the repo + trigger
  • Event: Push to a branch
  • Branch: main for production, dev for developpement
  • Configuration file: cloudbuild.yaml (at the root of your repo)

3. Example cloudbuild.yaml File

steps:
  - name: 'gcr.io/cloud-builders/docker'
    args: ['build', '-t', '$_IMAGE_NAME', '.']
    id: 'Build Docker Image'

  - name: 'gcr.io/cloud-builders/docker'
    args: ['push', '$_IMAGE_NAME']
    id: 'Push Docker Image'

  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: gcloud
    args:
      - 'run'
      - 'deploy'
      - '$_SERVICE_NAME'
      - '--image'
      - '$_IMAGE_NAME'
      - '--region'
      - '$_REGION'
      - '--platform'
      - 'managed'
      - '--project'
      - '${PROJECT_ID}'
      - '--allow-unauthenticated'
      - '--set-secrets'
      - '(Insert your secrets)'
      - '--set-env-vars'
      - "(Insert your env variables)"
      - "--memory"
      - "4Gi"
      - "--service-account"
      - "your_service_account@${PROJECT_ID}.iam.gserviceaccount.com"
    id: 'Deploy to Cloud Run'


images:
  - $_IMAGE_NAME

options:
  logging: CLOUD_LOGGING_ONLY
  substitution_option: 'ALLOW_LOOSE'

substitutions:
  _IMAGE_NAME: 'europe-west1-docker.pkg.dev/${PROJECT_ID}/cloudrun/(Insert your service name)'
  _SERVICE_NAME: (Insert your service name)
  _REGION: 'europe-west1'

Result

Every push to the specified branch will automatically trigger Cloud Build and execute the pipeline defined in cloudbuild.yaml.