如何使用云构建设置源 java api?
How to setup source with cloud build java api?
当我 运行:
时,看起来 gcloud
命令行默认做了很多事情
gcloud builds submit --tag gcr.io/$PROJECT_ID/$IMAGE_ID
例如,它将 repo 上传到 GCS,然后在构建开始时将其下载到工作目录。
我想使用 java api 而不是从另一个进程调用 gcloud
。
我可以使用 java api 执行同样的操作,而无需手动实施吗?我找不到任何关于如何使用云构建的示例 java api.
我目前拥有的 Kotlin 代码:
val buildDockerStep = BuildStep.newBuilder()
.setName("gcr.io/cloud-builders/docker")
.addAllArgs(listOf("build", "-t", imageRepository, "."))
val build: Build = Build.newBuilder()
// .setSource() ? does this need to happen manually?
.addSteps(buildDockerStep)
.build()
val request = CreateBuildRequest.newBuilder()
.setBuild(build)
.setProjectId(PROJECT_ID)
.build()
val result = cloudBuildClient.createBuildAsync(request)
回复您的评论,您会 indeed need to zip your source files and upload them to GCS if you’d like to build a StorageSource
. While there isn’t any sample code or examples for the Java API client out there, I pieced together this code from the API reference。它使用我的存储桶和压缩源存档构建了一个 StorageSource
,并构建了一个新图像以上传到我的 Artifact Registry。
StorageSource.Builder gcsSource = StorageSource.newBuilder()
.setBucket(bucketName)
.setObject(objectName); //Name of the zipped archive in GCS
Source.Builder buildSource = Source.newBuilder().setStorageSource(gcsSource);
Builder buildStep = BuildStep.newBuilder()
.setName("gcr.io/cloud-builders/docker")
.addAllArgs(Arrays.asList(new String[] {"build", "-t", imageDir, "."}));
Build testBuild = Build.newBuilder()
.setSource(buildSource)
.addSteps(buildStep)
.addImages(imageDir) //Adds the image to Artifact Registry
.build();
CreateBuildRequest req = CreateBuildRequest.newBuilder()
.setBuild(testBuild)
.setProjectId(projectId)
.build();
Build res = CloudBuildClient.create().createBuildAsync(req).get();
如果这对您有用,请告诉我,因为我找不到 Java 客户端的任何其他示例。我对其进行了测试,它在我这边运行良好。
当我 运行:
时,看起来gcloud
命令行默认做了很多事情
gcloud builds submit --tag gcr.io/$PROJECT_ID/$IMAGE_ID
例如,它将 repo 上传到 GCS,然后在构建开始时将其下载到工作目录。
我想使用 java api 而不是从另一个进程调用 gcloud
。
我可以使用 java api 执行同样的操作,而无需手动实施吗?我找不到任何关于如何使用云构建的示例 java api.
我目前拥有的 Kotlin 代码:
val buildDockerStep = BuildStep.newBuilder()
.setName("gcr.io/cloud-builders/docker")
.addAllArgs(listOf("build", "-t", imageRepository, "."))
val build: Build = Build.newBuilder()
// .setSource() ? does this need to happen manually?
.addSteps(buildDockerStep)
.build()
val request = CreateBuildRequest.newBuilder()
.setBuild(build)
.setProjectId(PROJECT_ID)
.build()
val result = cloudBuildClient.createBuildAsync(request)
回复您的评论,您会 indeed need to zip your source files and upload them to GCS if you’d like to build a StorageSource
. While there isn’t any sample code or examples for the Java API client out there, I pieced together this code from the API reference。它使用我的存储桶和压缩源存档构建了一个 StorageSource
,并构建了一个新图像以上传到我的 Artifact Registry。
StorageSource.Builder gcsSource = StorageSource.newBuilder()
.setBucket(bucketName)
.setObject(objectName); //Name of the zipped archive in GCS
Source.Builder buildSource = Source.newBuilder().setStorageSource(gcsSource);
Builder buildStep = BuildStep.newBuilder()
.setName("gcr.io/cloud-builders/docker")
.addAllArgs(Arrays.asList(new String[] {"build", "-t", imageDir, "."}));
Build testBuild = Build.newBuilder()
.setSource(buildSource)
.addSteps(buildStep)
.addImages(imageDir) //Adds the image to Artifact Registry
.build();
CreateBuildRequest req = CreateBuildRequest.newBuilder()
.setBuild(testBuild)
.setProjectId(projectId)
.build();
Build res = CloudBuildClient.create().createBuildAsync(req).get();
如果这对您有用,请告诉我,因为我找不到 Java 客户端的任何其他示例。我对其进行了测试,它在我这边运行良好。