A GitHub Action to build and push container images to Google Cloud using Cloud Build.
- 🚀 Build container images using Google Cloud Build
- 📦 Uses Cloud Native Buildpacks for automatic image creation or custom Dockerfiles
- 🔧 Configurable project, region, and build settings
- ⚡ Simple and lightweight composite action
Before using this action, ensure you have:
- Google Cloud authentication set up in your workflow (using
google-github-actions/auth
or similar) - Cloud Build API enabled in your Google Cloud project
- Artifact Registry (or Container Registry) repository created for storing images
- Proper IAM permissions for the service account:
Cloud Build Editor
orCloud Build Service Account
Artifact Registry Writer
(if using Artifact Registry)
name: Build and Deploy
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.GCP_SA_KEY }}
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v1
- name: Build image
uses: discue/gcp-build-image-action@v1
with:
image: 'europe-west3-docker.pkg.dev/my-project/my-repo/my-app'
project: 'my-project'
region: 'europe-west3'
- name: Build image with custom settings
uses: discue/gcp-build-image-action@v1
with:
image: 'europe-west3-docker.pkg.dev/my-project/my-repo/my-app:${{ github.sha }}'
project: 'my-project'
region: 'europe-west3'
source: './src'
quiet: 'false'
Input | Description | Required | Default |
---|---|---|---|
image |
The full image URL to build (e.g., europe-west3-docker.pkg.dev/project/repository/image ) |
Yes | - |
project |
Google Cloud project ID | No | Uses gcloud default |
region |
Google Cloud region for builds | No | Uses gcloud default |
source |
Source directory to build from | No | . |
quiet |
Run in quiet mode | No | true |
builder |
Build method: buildpack (uses Cloud Native Buildpacks) or dockerfile (uses your Dockerfile). If not set, the action will auto-detect: uses Dockerfile if present in the source directory, otherwise uses buildpack. |
No | Auto-detect |
Output | Description |
---|---|
image-url |
The built image URL |
This action requires Google Cloud authentication. The recommended approach is to use Workload Identity Federation:
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v1
with:
workload_identity_provider: 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider'
service_account: '[email protected]'
Alternatively, you can use a service account key (less secure):
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.GCP_SA_KEY }}
The service account used must have the following permissions:
cloudbuild.builds.create
cloudbuild.builds.get
artifactregistry.repositories.uploadArtifacts
(if using Artifact Registry)storage.objects.create
(for build logs and cache)
These are typically provided by the following predefined roles:
roles/cloudbuild.builds.builder
roles/artifactregistry.writer
name: Build and Deploy to Cloud Run
on:
push:
branches: [main]
env:
PROJECT_ID: my-project
REGION: europe-west3
REPOSITORY: my-repo
SERVICE: my-service
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v1
with:
workload_identity_provider: ${{ secrets.WIF_PROVIDER }}
service_account: ${{ secrets.WIF_SERVICE_ACCOUNT }}
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v1
- name: Build image
id: build
uses: discue/gcp-build-image-action@v1
with:
image: '${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }}'
project: ${{ env.PROJECT_ID }}
region: ${{ env.REGION }}
- name: Deploy to Cloud Run
run: |
gcloud run deploy ${{ env.SERVICE }} \
--image ${{ steps.build.outputs.image-url }} \
--region ${{ env.REGION }} \
--platform managed \
--allow-unauthenticated
The action will fail if:
- Google Cloud authentication is not set up
- The specified project or region is invalid
- Cloud Build API is not enabled
- Insufficient IAM permissions
- Invalid image URL format
- Source directory doesn't exist
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.