Search code examples
javasdkjobsgoogle-cloud-run

Google Cloud SDK to create a Cloud Run Job


We are trying to create a Cloud Run job programmatically using the Java SDK. So far we have been able to use classes in the com.google.cloud.run.v2 to create a service, doing something like this:

Container container = Container.newBuilder().setImage(image).build();

try (ServicesClient servicesClient = ServicesClient.create()) {

    log.debug("Creating service");

    CreateServiceRequest request =
        CreateServiceRequest.newBuilder()
            .setParent(LocationName.of(projectId, location).toString())
            .setService(Service.newBuilder().
                setTemplate(RevisionTemplate.newBuilder().addContainers(container)).
                                build())
            .setServiceId("hello")
            .build();
    Service response = servicesClient.createServiceAsync(request).get();
}

We would like to know if there is a similar Java API that allows us to create Cloud Run jobs instead of services.


Solution

  • Google provides 2 parallel sets of libraries. See Google Client libraries explained.

    The original API Client Libraries that are machine-generated and available for all Google services. The API client libraries are generally (!) perfect replicas of the underlying service.

    The Cloud Client Libraries that are (still?) hand-written and only available for Google Cloud services. The Cloud Client Libraries can lag in providing functionality that matches the underlying service.

    I think the Java Cloud Client Library for Cloud Run doesn't yet support Jobs (see Overview).

    The Java API Client Library for Cloud Run includes GoogleCloudRunV2Job.