Deploying Grails to Google Cloud Run with Cloud Build
Cloud Run gives a Grails application a serverless destination without maintaining a virtual machine. Each request runs in a managed container, while the service scales up or down and can scale to zero. Cloud Build supplies the delivery mechanism: it builds the application into an image, stores that image in Artifact Registry and promotes a new Cloud Run revision when source code changes. The result is a repeatable path from a Git commit to a running service.
A clean deployment has clear handovers. Cloud Build receives the source, installs dependencies and compiles the Grails application, then packages it as a Linux container image and pushes it to Artifact Registry. Cloud Run receives the image only after that succeeds. This separation protects the live service from a broken build: the current revision remains available while a new image is compiled, tested and deployed. It also creates a deployment history for rollbacks.
The project needs a Google Cloud project with billing enabled, the Cloud Build, Cloud Run and Artifact Registry APIs activated, and permission to create images and services. A modern Grails project supplies the Gradle wrapper, so local Java and Git experience is enough for the first build. Choose an Australian region before creating resources. australia-southeast1 is around Sydney, while australia-southeast2 is around Melbourne; use the region nearest the database and most users.
Budgeting deserves attention because many Australian teams approve spending in Australian dollars while Google Cloud can charge in US dollars, with local tax treatment shown on the billing account. Set a spending alert and check the applicable AUD conversion, GST treatment and ABN details. The examples use Sydney, but the commands accept another Australian region when Melbourne or a different data-residency requirement is the better fit.
How Cloud Build fits the deployment path
A Grails service may include a web tier, sessions, uploads, scheduled jobs and database connections. Cloud Run fits the request-driven web tier because the application can remain stateless while the platform manages instances. It is less suitable for long-running workers needing a guaranteed process or local disk. Separate those workloads so a web-traffic scaling policy does not cause unexpected background-job behaviour.
The build artefact should be an immutable container image rather than a loose JAR copied to a server. Artifact Registry stores each image under a regional repository, and Cloud Run deploys a specific image tag or digest. Tags such as latest are convenient, but a build ID makes each revision traceable. Cloud Run can then shift traffic gradually, send requests to both revisions during rollout and move traffic back to a known-good revision if error rates climb.
Separating business logic from infrastructure also keeps the Grails code testable. Where a cloud SDK sits behind a Groovy interface, the application can be tested without contacting the provider. This is a useful architectural cousin of the DispatchProxy pattern review, which demonstrates a similar separation in C#. The frameworks differ, yet both approaches reduce coupling: deployment configuration remains outside controllers, while the application depends on interfaces it controls.
The pipeline gives the team one place to enforce checks. A Cloud Build configuration can compile, test, scan, push and deploy as consecutive steps. A failed step stops the chain before Cloud Run receives the image. That feedback is quick for learning projects and creates a consistent, reproducible release policy for production systems.
Prepare the Grails application and project
First align the application with the Java runtime in the container. Grails versions differ in their supported Java releases, so use the version required by the project; Java 17 is a sensible baseline for many current Grails releases, while older projects may need Java 11. Do not use a floating latest tag in the final image because a base-image update can unexpectedly change behaviour between builds.
Make the HTTP port environment-driven because Cloud Run supplies PORT. A small application.yml fragment keeps the local default and the hosted behaviour aligned:
server:
port: ${PORT:8080}
grails:
profile: web
spring:
main:
banner-mode: off
The Gradle wrapper should remain the only build entry point. Run ./gradlew clean test and then ./gradlew assemble locally, using the same Java major version as the container. If the project has separate development and production configuration, set the intended Grails profile through a Cloud Run environment variable and supply other environment-specific values through environment variables or Secret Manager rather than committing credentials. For project-specific Grails and Groovy guidance, the Grails Example tutorials provide useful preparation material.
Before packaging, remove assumptions about a writable application directory and a single long-lived instance. Store uploaded files in Cloud Storage, keep sessions in a shared store when they are required, and move relational data to Cloud SQL or another managed database. Place the database in the same Australian region as Cloud Run where possible to reduce latency and make data residency easier to document for Australian privacy obligations under the Privacy Act. If regulated data must remain in Australia, verify the location of every dependent service, including backups and logs.
Build a container and store the image
A multi-stage Dockerfile keeps compilers, Gradle caches and source files out of the runtime image. It also lets the container run as an unprivileged user. The example assumes a Grails application whose packaged JAR is named grails-demo-1.0.0.jar; inspect build/libs after a local build and adjust that filename if the project uses a different application name or version.
FROM eclipse-temurin:17-jdk-jammy AS build
WORKDIR /workspace
COPY . .
RUN chmod +x gradlew \
&& ./gradlew clean test assemble --no-daemon \
&& cp build/libs/grails-demo-1.0.0.jar /workspace/app.jar
FROM eclipse-temurin:17-jre-jammy
RUN useradd --system --uid 10001 --create-home appuser
WORKDIR /app
COPY --from=build /workspace/app.jar /app/app.jar
USER appuser
EXPOSE 8080
ENTRYPOINT ["java", "-XX:MaxRAMPercentage=75.0", "-jar", "/app/app.jar"]
Add a .dockerignore file so local Git metadata, Gradle caches and previous build outputs are not sent into the Docker build context. The JAR is copied into the smaller JRE stage, and MaxRAMPercentage lets the JVM respect the memory limit assigned to the Cloud Run instance. Pin the base image to a tested digest when the service becomes business-critical; otherwise, rebuild and test regularly when its Java runtime receives security updates.
Create a regional Artifact Registry repository once, then reuse it for subsequent services. The repository location should match the deployment region to keep image transfer latency and administration straightforward:
gcloud services enable artifactregistry.googleapis.com \
cloudbuild.googleapis.com run.googleapis.com
gcloud artifacts repositories create grails \
--repository-format=docker \
--location=australia-southeast1 \
--description="Grails images for Cloud Run"
The repository name appears in the image path as australia-southeast1-docker.pkg.dev/PROJECT_ID/grails/SERVICE. Cloud Build can authenticate to Artifact Registry without storing a registry password in the repository, provided its service account has the Artifact Registry Writer role. This removes another long-lived credential from the release path.
Automate deployment with Cloud Build
The following cloudbuild.yaml builds the image, pushes it and deploys it. ${BUILD_ID} creates a unique image tag for every build, while ${PROJECT_ID} and the custom substitutions remove project-specific values from the command steps.
steps:
- name: gcr.io/cloud-builders/docker
args:
- build
- -t
- ${_REGION}-docker.pkg.dev/${PROJECT_ID}/${_REPOSITORY}/${_SERVICE}:${BUILD_ID}
- .
- name: gcr.io/cloud-builders/docker
args:
- push
- ${_REGION}-docker.pkg.dev/${PROJECT_ID}/${_REPOSITORY}/${_SERVICE}:${BUILD_ID}
- name: gcr.io/google.com/cloudsdktool/cloud-sdk:slim
entrypoint: gcloud
args:
- run
- deploy
- ${_SERVICE}
- --image=${_REGION}-docker.pkg.dev/${PROJECT_ID}/${_REPOSITORY}/${_SERVICE}:${BUILD_ID}
- --region=${_REGION}
- --platform=managed
- --port=8080
- --service-account=grails-runtime@${PROJECT_ID}.iam.gserviceaccount.com
- --allow-unauthenticated
substitutions:
_REGION: australia-southeast1
_REPOSITORY: grails
_SERVICE: grails-demo
Create grails-runtime before the first build with gcloud iam service-accounts create grails-runtime. Give the Cloud Build service account Artifact Registry Writer and Cloud Run Admin permissions at project level, plus Service Account User on grails-runtime. Give grails-runtime only the permissions the application needs, such as access to a specific Secret Manager secret. The --allow-unauthenticated flag makes this sample endpoint public, so retain application authentication; remove the flag for a private service and grant roles/run.invoker only to authorised identities.
Connect the Git repository in Cloud Build and create a trigger for main. Select cloudbuild.yaml and a dedicated builder service account with the required roles. Each push can then produce a tested image and new Cloud Run revision without a developer using a personal Google account. For a manual run, select the project and submit the same configuration with gcloud builds submit --config cloudbuild.yaml.
Revision details and Cloud Run metrics show whether the new image started cleanly and how it behaves under traffic. If a rollout causes errors, route traffic back to the previous revision with gcloud run services update-traffic, fix the source and trigger another build. Keep the first regional release small: request logs, application metrics and an alert on 5xx responses are more useful than broad dashboards. After a successful build, obtain and exercise the service URL:
SERVICE_URL=$(gcloud run services describe grails-demo \
--region=australia-southeast1 \
--format='value(status.url)')
curl -i "$SERVICE_URL"
A dependable operating checklist for Australian teams
Teams may work across Sydney and Melbourne, where daylight-saving changes make timestamps ambiguous. Australian English and friendly shorthand such as “arvo” and “no worries” are common in chat, but deployment records still need UTC timestamps and explicit AEST or AEDT labels. That precision helps support staff compare Australian customer reports with logs from another region.
Traffic can arrive in bursts during Australian retail events, end-of-financial-year promotions and the Boxing Day sales period. Cloud Run can absorb that variability, but the service still needs tested concurrency, sensible maximum instances and a budget alert. Include these local operating patterns in a game day rather than discovering them during a busy afternoon in December.
- Pin the Java runtime, Grails version and container base image, with scheduled rebuilds for tested security updates.
- Keep the web tier stateless; use Cloud Storage for files and a supported session or database service for shared state.
- Keep credentials in Secret Manager and grant the runtime service account access only to the secrets it uses.
- Place Cloud Run and regional data stores in the required Australian region; verify global-service settings and backup locations.
- Set minimum and maximum instances deliberately, then use load tests and budget alerts to validate the cost envelope.
- Record the image digest, Cloud Run revision and deployment timestamp in the release history for rapid rollback.
A release is complete only when the application, configuration and image share one audit trail. Cloud Build supplies image identity, Cloud Run supplies revision history and service logs show release behaviour. Together they give a small Australian team the traceable delivery process used by much larger engineering organisations.
Deploy the first revision now: Commit the Dockerfile, application.yml change and cloudbuild.yaml, push to the branch watched by the Cloud Build trigger, and run gcloud builds submit --config cloudbuild.yaml when testing manually. Open the resulting Cloud Run revision, exercise its URL from the intended Australian audience and keep the previous revision available until the release is verified.