diff --git a/.github/workflows/update-compute-discovery.yml b/.github/workflows/update-compute-discovery.yml new file mode 100644 index 0000000000000..f2cfa5241324a --- /dev/null +++ b/.github/workflows/update-compute-discovery.yml @@ -0,0 +1,62 @@ +name: Update Compute Discovery Document + +on: + schedule: + # Runs every Monday at 09:00 UTC + - cron: '0 9 * * 1' + workflow_dispatch: # Allows manual trigger from the GitHub Actions UI + +permissions: + contents: read + +jobs: + update-compute-discovery: + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + steps: + - name: Checkout repository + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + persist-credentials: false + + - name: Setup Git user identity + run: | + git config user.name "cloud-cpp-automation[bot]" + git config user.email "cloud-cpp-automation[bot]@users.noreply.github.com" + + - name: Run update_discovery_doc.sh + run: | + ./generator/discovery/update_discovery_doc.sh --no-create-branch + + - name: Run checkers + run: | + ci/cloudbuild/build.sh -t checkers-pr + + - name: Push branch and create Pull Request if changes exist + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + # Check if a new commit was made (HEAD differs from origin/main) + if [[ "$(git rev-parse HEAD)" != "$(git rev-parse origin/main)" ]]; then + echo "Changes detected. Pushing branch automation/update-compute-discovery..." + git push origin HEAD:automation/update-compute-discovery --force + + echo "Checking if a PR is already open..." + if ! gh pr list --head "automation/update-compute-discovery" --json number -q '.[0].number' | grep -q '^[0-9]'; then + echo "Opening new PR..." + BODY="Automated update of Compute Engine Discovery Document and generated C++ client code.\n\nTriggered automatically by the \`update-compute-discovery\` workflow." + gh pr create \ + --title "feat(compute): update discovery doc and generate new services" \ + --body "$(printf '%b' "$BODY")" \ + --base main \ + --head automation/update-compute-discovery \ + --label "autorelease: pending" + else + echo "Pull request already exists for automation/update-compute-discovery. Branch has been updated." + fi + else + echo "No changes committed. Nothing to push or create." + fi diff --git a/generator/discovery/update_discovery_doc.sh b/generator/discovery/update_discovery_doc.sh index 31794d8e7b567..b6f19e847dcd5 100755 --- a/generator/discovery/update_discovery_doc.sh +++ b/generator/discovery/update_discovery_doc.sh @@ -86,11 +86,36 @@ readonly GENERATOR_CONFIG_RELATIVE_PATH="generator/generator_config.textproto" readonly COMPUTE_SERVICE_DIRS_CMAKE_RELATIVE_PATH="google/cloud/compute/service_dirs.cmake" readonly COMPUTE_SERVICE_DIRS_BZL_RELATIVE_PATH="google/cloud/compute/service_dirs.bzl" +CURRENT_REVISION=$(sed -En 's/ \"revision\": \"([[:digit:]]+)\",/\1/p' "${PROJECT_ROOT}/${COMPUTE_DISCOVERY_JSON_RELATIVE_PATH}" 2>/dev/null || echo "") + io::log_h2 "Fetching discovery document from ${COMPUTE_DISCOVERY_DOCUMENT_URL}" -curl "${COMPUTE_DISCOVERY_DOCUMENT_URL}" >"${PROJECT_ROOT}/${COMPUTE_DISCOVERY_JSON_RELATIVE_PATH}" +TEMP_JSON=$(mktemp) +if [[ ! -f "${TEMP_JSON}" ]]; then + io::log_red "Failed to create temporary file." + exit 1 +fi +trap 'rm -f "${TEMP_JSON}"' EXIT -REVISION=$(sed -En 's/ \"revision\": \"([[:digit:]]+)\",/\1/p' "${PROJECT_ROOT}/${COMPUTE_DISCOVERY_JSON_RELATIVE_PATH}") +if ! curl -fsSL "${COMPUTE_DISCOVERY_DOCUMENT_URL}" >"${TEMP_JSON}"; then + io::log_red "Failed to fetch discovery document from ${COMPUTE_DISCOVERY_DOCUMENT_URL}" + exit 1 +fi + +REVISION=$(sed -En 's/ \"revision\": \"([[:digit:]]+)\",/\1/p' "${TEMP_JSON}") +if [[ -z "${REVISION}" ]]; then + io::log_red "Could not parse revision from fetched discovery document." + exit 1 +fi readonly REVISION + +if [[ -n "${CURRENT_REVISION}" && "${REVISION}" == "${CURRENT_REVISION}" ]]; then + io::log_green "Compute discovery document is already up to date (revision ${CURRENT_REVISION}). Nothing to do." + exit 0 +fi + +io::log_h2 "Updating Discovery Document: ${CURRENT_REVISION:-none} -> ${REVISION}" +mv "${TEMP_JSON}" "${PROJECT_ROOT}/${COMPUTE_DISCOVERY_JSON_RELATIVE_PATH}" + if [[ "${CREATE_BRANCH}" == "true" ]]; then io::run git checkout -B update_compute_discovery_circa_"${REVISION}" fi