Archived
Nobody but me ever used this plugin.
I only used is in OpenTorah's "Collector" to deploy dynamic www.alter-rebbe.org.
With that site going static (using Podval Site Publisher,
there are no users for it ;)
Archiving.
Introduction
Gradle plugin to deploy a service described in a knative
Service
YAML file
to Google Cloud Run
from the local machine or from CI like GitHub Actions -
or to run it in the local Docker.
Plugin is opinionated:
- the only supported method of configuring the service is the YAML file;
- the only supported method of authentication with the Google Cloud Platform
is the Service Account;
- plugin integrates nicely with the
JIB Gradle plugin,
so using JIB is recommended (but not required).
Apply to a project
plugins {
id 'com.google.cloud.tools.jib' version '3.2.1' // optional; see `JIB Plugin` section below
id 'org.podval.tools.cloudrun' version '0.3.9'
}
Service YAML file
Put the following into the service.yaml in your project:
apiVersion: "serving.knative.dev/v1"
kind: "Service"
metadata:
name: "<service-name>"
spec:
template:
spec:
containers:
- image: "gcr.io/<project-id>/<service-name>"
Configurable parameters are listed in the service.yaml section below.
Deploy from local machine
You can use the task cloudRunDeploy created by the plugin to deploy new revision of the
service to Google Cloud Run from your local machine:
$ ./gradlew cloudRunDeploy
Deploy from CI
Put your service account key into a secret gcloudServiceAccountKeySecret
in the GitHub repository or organization, and you can use the following GitHub Actions workflow
step to deploy from the CI environment:
- name: "Build and push the image and deploy Cloud Run service"
env:
gcloudServiceAccountKey: ${{secrets.gcloudServiceAccountKeySecret}}
run: ./gradlew cloudRunDeploy
Run locally
You can use the task cloudRunLocal to run the service with the same parameters,
resource limits etc. in the local Docker:
$ ./gradlew cloudRunLocal
Additional Docker options can be configured using additionalOptions property on
that task:
cloudRunLocal.additionalOptions = [
'--volume', '...:/mnt/store/:Z'
]
For multiple local configurations with different additionalOptions,
additional tasks of type org.podval.tools.cloudrun.CloudRunPlugin.RunLocalTask
can be added to the Gradle project.
JIB plugin
If your Gradle project uses JIB to build and push the image (and why wouldn't it?),
CloudRun plugin configures some values on thejib extension (if you did not configure them):
jib.to.image is set to the image name from the service YAML file;
jib.to.auth.password is set to the service account key;
jib.to.auth.username is set to _json_key, telling JIB to use service account key.
In addition:
cloudRunDeploy task is configured to depend on the jib task;
cloudRunLocal task is configured to depend on the jibDockerBuild task.
Note: any additional task of type org.podval.tools.cloudrun.CloudRunPlugin.RunLocalTask
is configured with the parameters from the Service YAML file,
and automatically depends on thejibDockerBuild task.
Using JIB plugin is recommended but not required; if you build and push your
images by other means, it is your responsibility to arrange the Gradle build file
in such a way that:
- the image is built and pushed to the Google Container Registry
before the
cloudRunDeploy task runs;
- the image is built and pushed to the local Docker before
cloudRunLocal task runs.
YAML retrieval
Plugin creates two help tasks that retrieve the YAML for the service and
its latest revision respectively:
cloudRunDescribe is similar to
gcloud run services describe $serviceName --format export
cloudRunDescribeRevision is similar to
gcloud run revisions describe $revisionName --format export (if you know the name of the
latest revision of your service :))
Configuration
Plugin creates cloudRun extension that can be used to configure it via build.gradle file:
cloudRun {
region = 'us-east1' // required
serviceYamlFilePath = "$getProjectDir/service.yaml" // optional
serviceAccountKeyProperty = 'gcloudServiceAccountKey' // optional
}
If you use default property name for the key (gcloudServiceAccountKey), and the YAML
file for your service is in the file service.yaml in your Gradle project,
the only thing you need to configure in your Gradle build file is the region to deploy in,
for example:
cloudRun.region = 'us-east4'
Region
Parameter region specifies which region the service is to be deployed in;
one of the Google Cloud Run regions must be supplied.
Path to the YAML file
Parameter serviceYamlFilePath is a path to a YAML file that describes the
service; see service.yaml section below for the mapping between
gcloud run deploy options and the fields in the file.
This parameter defaults to "$getProjectDir/service.yaml" and needs to be
explicitly set only if a different path is desired.
Service Account Key Property
Parameter serviceAccountKeyProperty names the environment variable and Gradle
property that contains the JSON key for the service
account to be used for deployment.
When deploying to Google Cloud Run from the local machine,
the key is retrieved from a property
defined in ~/.gradle/gradle.properties.
When running in Continuous Integration environment (e.g., GitHub Actions),
it is retrieved from a secret configured in that environment
and passed to the workflow steps that need it in an environment variable
with the same name. Since the key itself is retrieved only when needed,
steps that do not have the key available will work - unless they involve
tasks that require it.
It is this name that this parameter configures.
It defaults to gcloudServiceAccountKey and needs to be explicitly set
only if a different name is desired.
To help configure the Gradle property,
if this parameter is set to an absolute path to the file with the JSON key,
plugin outputs the (appropriately quoted and encoded) property file snippet
that can be added to ~/.gradle/gradle.properties.
Motivation
Deploy from local machine
Let's say I am developing a Google Cloud Run service that I build and push to
Google Container Registry using JIB's Gradle plugin. To deploy the service,
I use gcloud CLI (which, since I work with Google Cloud, I of course have
installed on my machine) and not the Google Cloud Platform UI, because I want my
deployments to be reproducible. (One-time things like setting the service to be
available without authentication is done using the UI,
since using gcloud --allow-unauthenticated
for this is not
recommended.)
Since I want my deployments to be reproducible, I want the gcloud commands to be
recorded in my sources.
This leads to something like this in the Gradle build file:
final String gcloudService = '...'
final String serviceImage = "gcr.io/<gcloud-project>/$gcloudService"
final String gcloudServiceAccountProperty = 'gcloudServiceAccountKey'
final String gcloudServiceAccountKey = findProperty(gcloudServiceAccountProperty) ?: System.getenv(gcloudServiceAccountProperty)
jib {
to {
auth {
username = '_json_key'
password = gcloudServiceAccountKey
}
image = serviceImage
}
container {
mainClass = '...'
}
}
cloudRunDeploy.dependsOn('jib')
cloudRunDeploy.doLast {
exec {
standardInput new ByteArrayInputStream(gcloudServiceAccountKey.getBytes('UTF-8'))
commandLine 'gcloud', 'auth', 'activate-service-account', '--key-file', '-'
}
exec {
commandLine 'gcloud', 'beta', 'run', 'deploy', gcloudService,
'--image', serviceImage,
'--platform', 'managed',
'--region', 'us-east4',
'--min-instances', '1',
'--max-instances', '2'
}
}
Now I can deploy with:
$ ./gradlew cloudRunDeploy
Note: serviceImage and gcloudServiceAccountKey are shared between jib and gcloud.
Note: gcloud beta must be used while --min-instances is in beta.
Note: I didn't find a way to run gcloud using service account without it becoming
the current default, so to run other gcoloud commands as my main account, I need
to remember to switch to it first. This is inconvenient and annoying, but workable.
Deploy from CI
Of course, I also want to be able to build and push the image and deploy the service
from the CI run, in my case - GitHub Actions workflow.
Thankfully, there is an action
google-github-actions/setup-gcloud
that can be used to install gcloud on the CI machine!
This leads to something like this in the .github/workflows/CI.yaml file:
- name: "Build and push image to Google Container Registry"
env:
gcloudServiceAccountKey: ${{secrets.gcloudServiceAccountKeySecret}}
run: ./gradlew jib
- name: "Set up gcloud CLI"
uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
with:
project_id: "<project-id>"
service_account_key: ${{secrets.gcloudServiceAccountKeySecret}}
- name: "Deploy Cloud Run service"
run: |
gcloud beta run deploy <service-name> \
--quiet \
--image "gcr.io/<project-id>/<service-name>" \
--platform managed \
--region us-east4 \
--min-instances 1 \
--max-instances 2
And here is the real problem: service configuration is now duplicated;
one copy - in the build.gradle file - is used for deployment from the
local machine, while another - in the workflow file - is used from the CI.
Note: I didn't try to simplify the above to:
- name: "Set up gcloud CLI"
uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
with:
project_id: "<project-id>"
service_account_key: ${{secrets.gcloudServiceAccountKeySecret}}
- name: "Build and push image and deploy Cloud Run service"
env:
gcloudServiceAccountKey: ${{secrets.gcloudServiceAccountKeySecret}}
run: ./gradlew cloudRunDeploy
If that actually worked, I might have stopped right there, since
there is no more configuration duplication - and CloudRun plugin
would not have been written ;)
Using YAML
It would be nice if a format existed that:
- describes configuration of the service;
- is understood by
gcloud and
- is understood by a GitHub Action.
Then, configuration duplication could be avoided.
Turns out, such a format does exist: it is knative
Service
YAML format!
This is how it looks like:
apiVersion: "serving.knative.dev/v1"
kind: "Service"
metadata:
name: "<service-name>"
spec:
template:
metadata:
annotations:
autoscaling.knative.dev/minScale: "1"
autoscaling.knative.dev/maxScale: "2"
spec:
containers:
- image: "gcr.io/<project-id>/<service-name>"
resources:
limits:
cpu: "1000m"
memory: "512Mi"
To use it in gcloud, instead of run deploy command one uses run services replace;
to use it in GitHub workflow, one switches to a sibling
action: google-github-actions/deploy-cloudrun.
Assuming the YAML describing the service is in the service.yaml file,
configuration in build.gradle looks like this:
final String gcloudService = '...'
final String serviceImage = "gcr.io/<gcloud-project>/$gcloudService"
final String gcloudServiceAccountProperty = 'gcloudServiceAccountKey'
final String gcloudServiceAccountKey = findProperty(gcloudServiceAccountProperty) ?: System.getenv(gcloudServiceAccountProperty)
jib {
to {
auth {
username = '_json_key'
password = gcloudServiceAccountKey
}
image = serviceImage
}
container {
mainClass = '...'
}
}
cloudRunDeploy.dependsOn('jib')
cloudRunDeploy.doLast {
exec {
standardInput new ByteArrayInputStream(gcloudServiceAccountKey.getBytes('UTF-8'))
commandLine 'gcloud', 'auth', 'activate-service-account', '--key-file', '-'
}
exec {
commandLine 'gcloud', 'beta', 'run', 'services', 'replace', "$projectDir/service.yaml"
}
}
And GitHub workflow simplifies to:
- name: "Build and push image to Google Container Registry"
env:
gcloudServiceAccountKey: ${{secrets.gcloudServiceAccountKeySecret}}
run: ./gradlew --no-daemon jib
- name: "Deploy Cloud Run service"
uses: google-github-actions/deploy-cloudrun@main
with:
credentials: ${{secrets.gcloudServiceAccountKeySecret}}
metadata: "./service.yaml"
region: "us-east4"