Skip to content

Commit 0412fa6

Browse files
authored
feat: update cloud build config to run test (#14300)
Fixes: googleapis/librarian#982
1 parent f7638a6 commit 0412fa6

File tree

3 files changed

+85
-9
lines changed

3 files changed

+85
-9
lines changed

.generator/cli.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import shutil
2222
import subprocess
2323
import sys
24+
from pathlib import Path
2425
from typing import Dict, List
2526

2627
try:
@@ -282,31 +283,40 @@ def _copy_files_needed_for_post_processing(output: str, input: str, library_id:
282283

283284
def _clean_up_files_after_post_processing(output: str, library_id: str):
284285
"""
285-
Clean up files which should not be included in the generated client
286+
Clean up files which should not be included in the generated client.
287+
This function is idempotent and will not fail if files are already removed.
286288
287289
Args:
288290
output(str): Path to the directory in the container where code
289291
should be generated.
290292
library_id(str): The library id to be used for post processing.
291293
"""
292294
path_to_library = f"packages/{library_id}"
293-
shutil.rmtree(f"{output}/{path_to_library}/.nox")
294-
os.remove(f"{output}/{path_to_library}/CHANGELOG.md")
295-
os.remove(f"{output}/{path_to_library}/docs/CHANGELOG.md")
296-
os.remove(f"{output}/{path_to_library}/docs/README.rst")
295+
296+
# Safely remove directories, ignoring errors if they don't exist.
297+
shutil.rmtree(f"{output}/{path_to_library}/.nox", ignore_errors=True)
298+
shutil.rmtree(f"{output}/owl-bot-staging", ignore_errors=True)
299+
300+
# Safely remove specific files if they exist using pathlib.
301+
Path(f"{output}/{path_to_library}/CHANGELOG.md").unlink(missing_ok=True)
302+
Path(f"{output}/{path_to_library}/docs/CHANGELOG.md").unlink(missing_ok=True)
303+
Path(f"{output}/{path_to_library}/docs/README.rst").unlink(missing_ok=True)
304+
305+
# The glob loops are already safe, as they do nothing if no files match.
297306
for post_processing_file in glob.glob(
298307
f"{output}/{path_to_library}/scripts/client-post-processing/*.yaml"
299308
): # pragma: NO COVER
300309
os.remove(post_processing_file)
310+
301311
for gapic_version_file in glob.glob(
302312
f"{output}/{path_to_library}/**/gapic_version.py", recursive=True
303313
): # pragma: NO COVER
304314
os.remove(gapic_version_file)
315+
305316
for snippet_metadata_file in glob.glob(
306317
f"{output}/{path_to_library}/samples/generated_samples/snippet_metadata*.json"
307318
): # pragma: NO COVER
308319
os.remove(snippet_metadata_file)
309-
shutil.rmtree(f"{output}/owl-bot-staging")
310320

311321

312322
def handle_generate(
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"id": "google-cloud-language",
3+
"version": "2.17.2",
4+
"last_generated_commit": "97a83d76a09a7f6dcab43675c87bdfeb5bcf1cb5",
5+
"apis": [
6+
{
7+
"path": "google/cloud/language/v1beta2",
8+
"service_config": "language_v1beta2.yaml",
9+
"status": ""
10+
},
11+
{
12+
"path": "google/cloud/language/v2",
13+
"service_config": "language_v2.yaml",
14+
"status": ""
15+
},
16+
{
17+
"path": "google/cloud/language/v1",
18+
"service_config": "language_v1.yaml",
19+
"status": ""
20+
}
21+
],
22+
"source_roots": [
23+
"packages/google-cloud-language"
24+
],
25+
"preserve_regex": [
26+
".OwlBot.yaml",
27+
"packages/google-cloud-language/CHANGELOG.md",
28+
"docs/CHANGELOG.md",
29+
"docs/README.rst",
30+
"samples/README.txt",
31+
"tar.gz",
32+
"gapic_version.py",
33+
"samples/generated_samples/snippet_metadata_",
34+
"scripts/client-post-processing"
35+
],
36+
"remove_regex": [
37+
"packages/google-cloud-language"
38+
]
39+
}

cloudbuild-test.yaml

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
# Reduce this timeout by moving the installation of Python runtimes to a separate base image
1717
timeout: 7200s # 2 hours for the first uncached run, can be lowered later.
1818
steps:
19-
# A single step using the Kaniko executor to build and cache
19+
# Step 1: Build the generator image using Kaniko and push it to the registry.
2020
- name: 'gcr.io/kaniko-project/executor:latest'
21+
id: 'build-generator'
2122
args:
2223
# Specifies the Dockerfile path
2324
- '--dockerfile=.generator/Dockerfile'
@@ -27,10 +28,36 @@ steps:
2728
- '--destination=gcr.io/$PROJECT_ID/python-librarian-generator:latest'
2829
# Enables Kaniko's remote registry caching
2930
- '--cache=true'
30-
# (Optional but recommended) Sets a time-to-live for cache layers
31+
# Sets a time-to-live for cache layers
3132
- '--cache-ttl=24h'
3233

33-
# The 'images' section is no longer needed because Kaniko pushes the image itself.
34+
# Step 2: Clone the googleapis repository into the workspace.
35+
# This runs in parallel with the image build.
36+
- name: 'gcr.io/cloud-builders/git'
37+
id: 'clone-googleapis'
38+
args: ['clone', '--depth', '1', 'https://github.com/googleapis/googleapis.git', '/workspace/googleapis']
39+
waitFor: ['-']
40+
41+
# Step 3: Run the generator to generate the library code.
42+
- name: 'gcr.io/cloud-builders/docker'
43+
id: 'generate-library'
44+
args:
45+
- 'run'
46+
- '--rm'
47+
# Mount the cloned googleapis repo from the workspace.
48+
- '-v'
49+
- '/workspace/googleapis:/app/source'
50+
# Mount the generator-input from this repo's workspace.
51+
- '-v'
52+
- '/workspace/.librarian/generator-input:/app/input'
53+
# Mount the test-resources/librarian from this repo's workspace as the librarian dir.
54+
- '-v'
55+
- '/workspace/.generator/test-resources/librarian:/app/librarian'
56+
# The image that was built in the first step.
57+
- 'gcr.io/$PROJECT_ID/python-librarian-generator:latest'
58+
# The command to execute inside the container.
59+
- 'generate'
60+
waitFor: ['build-generator', 'clone-googleapis']
3461

3562
options:
3663
default_logs_bucket_behavior: REGIONAL_USER_OWNED_BUCKET

0 commit comments

Comments
 (0)