Skip to content

Commit cefdd50

Browse files
authored
feat(generator): automate compute discovery document updates (#16368)
1 parent 6339afd commit cefdd50

2 files changed

Lines changed: 89 additions & 2 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: Update Compute Discovery Document
2+
3+
on:
4+
schedule:
5+
# Runs every Monday at 09:00 UTC
6+
- cron: '0 9 * * 1'
7+
workflow_dispatch: # Allows manual trigger from the GitHub Actions UI
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
update-compute-discovery:
14+
runs-on: ubuntu-latest
15+
permissions:
16+
contents: write
17+
pull-requests: write
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
21+
with:
22+
fetch-depth: 0
23+
persist-credentials: false
24+
25+
- name: Setup Git user identity
26+
run: |
27+
git config user.name "cloud-cpp-automation[bot]"
28+
git config user.email "cloud-cpp-automation[bot]@users.noreply.github.com"
29+
30+
- name: Run update_discovery_doc.sh
31+
run: |
32+
./generator/discovery/update_discovery_doc.sh --no-create-branch
33+
34+
- name: Run checkers
35+
run: |
36+
ci/cloudbuild/build.sh -t checkers-pr
37+
38+
- name: Push branch and create Pull Request if changes exist
39+
env:
40+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
41+
run: |
42+
# Check if a new commit was made (HEAD differs from origin/main)
43+
if [[ "$(git rev-parse HEAD)" != "$(git rev-parse origin/main)" ]]; then
44+
echo "Changes detected. Pushing branch automation/update-compute-discovery..."
45+
git push origin HEAD:automation/update-compute-discovery --force
46+
47+
echo "Checking if a PR is already open..."
48+
if ! gh pr list --head "automation/update-compute-discovery" --json number -q '.[0].number' | grep -q '^[0-9]'; then
49+
echo "Opening new PR..."
50+
BODY="Automated update of Compute Engine Discovery Document and generated C++ client code.\n\nTriggered automatically by the \`update-compute-discovery\` workflow."
51+
gh pr create \
52+
--title "feat(compute): update discovery doc and generate new services" \
53+
--body "$(printf '%b' "$BODY")" \
54+
--base main \
55+
--head automation/update-compute-discovery \
56+
--label "autorelease: pending"
57+
else
58+
echo "Pull request already exists for automation/update-compute-discovery. Branch has been updated."
59+
fi
60+
else
61+
echo "No changes committed. Nothing to push or create."
62+
fi

generator/discovery/update_discovery_doc.sh

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,36 @@ readonly GENERATOR_CONFIG_RELATIVE_PATH="generator/generator_config.textproto"
8686
readonly COMPUTE_SERVICE_DIRS_CMAKE_RELATIVE_PATH="google/cloud/compute/service_dirs.cmake"
8787
readonly COMPUTE_SERVICE_DIRS_BZL_RELATIVE_PATH="google/cloud/compute/service_dirs.bzl"
8888

89+
CURRENT_REVISION=$(sed -En 's/ \"revision\": \"([[:digit:]]+)\",/\1/p' "${PROJECT_ROOT}/${COMPUTE_DISCOVERY_JSON_RELATIVE_PATH}" 2>/dev/null || echo "")
90+
8991
io::log_h2 "Fetching discovery document from ${COMPUTE_DISCOVERY_DOCUMENT_URL}"
90-
curl "${COMPUTE_DISCOVERY_DOCUMENT_URL}" >"${PROJECT_ROOT}/${COMPUTE_DISCOVERY_JSON_RELATIVE_PATH}"
92+
TEMP_JSON=$(mktemp)
93+
if [[ ! -f "${TEMP_JSON}" ]]; then
94+
io::log_red "Failed to create temporary file."
95+
exit 1
96+
fi
97+
trap 'rm -f "${TEMP_JSON}"' EXIT
9198

92-
REVISION=$(sed -En 's/ \"revision\": \"([[:digit:]]+)\",/\1/p' "${PROJECT_ROOT}/${COMPUTE_DISCOVERY_JSON_RELATIVE_PATH}")
99+
if ! curl -fsSL "${COMPUTE_DISCOVERY_DOCUMENT_URL}" >"${TEMP_JSON}"; then
100+
io::log_red "Failed to fetch discovery document from ${COMPUTE_DISCOVERY_DOCUMENT_URL}"
101+
exit 1
102+
fi
103+
104+
REVISION=$(sed -En 's/ \"revision\": \"([[:digit:]]+)\",/\1/p' "${TEMP_JSON}")
105+
if [[ -z "${REVISION}" ]]; then
106+
io::log_red "Could not parse revision from fetched discovery document."
107+
exit 1
108+
fi
93109
readonly REVISION
110+
111+
if [[ -n "${CURRENT_REVISION}" && "${REVISION}" == "${CURRENT_REVISION}" ]]; then
112+
io::log_green "Compute discovery document is already up to date (revision ${CURRENT_REVISION}). Nothing to do."
113+
exit 0
114+
fi
115+
116+
io::log_h2 "Updating Discovery Document: ${CURRENT_REVISION:-none} -> ${REVISION}"
117+
mv "${TEMP_JSON}" "${PROJECT_ROOT}/${COMPUTE_DISCOVERY_JSON_RELATIVE_PATH}"
118+
94119
if [[ "${CREATE_BRANCH}" == "true" ]]; then
95120
io::run git checkout -B update_compute_discovery_circa_"${REVISION}"
96121
fi

0 commit comments

Comments
 (0)