Librarian - Update googleapis commitish and generate all #102
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Copyright 2026 Google LLC | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| permissions: | |
| contents: read | |
| name: Librarian - Update googleapis commitish and generate all | |
| on: | |
| schedule: | |
| - cron: '0 3 * * *' # Run once a day at 3:00 AM UTC | |
| workflow_dispatch: # Allow manual trigger | |
| pull_request: # Run in dry-run mode to test changes to this workflow itself | |
| paths: | |
| - '.github/workflows/update_librarian_googleapis.yaml' | |
| jobs: | |
| update-librarian-googleapis: | |
| runs-on: ubuntu-24.04 | |
| defaults: | |
| run: | |
| working-directory: google-cloud-java | |
| steps: | |
| - name: Checkout google-cloud-java | |
| uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6 | |
| with: | |
| repository: googleapis/google-cloud-java | |
| path: google-cloud-java | |
| fetch-depth: 0 | |
| persist-credentials: false | |
| - name: Close Existing PR and Branch | |
| env: | |
| GH_TOKEN: ${{ secrets.CLOUD_JAVA_BOT_GITHUB_TOKEN }} | |
| run: | | |
| set -x | |
| current_branch="update-librarian-googleapis-main" | |
| if [ "${{ github.event_name }}" = "pull_request" ]; then | |
| echo "PR Test: Skipping cleanup." | |
| else | |
| # Try to find an open pull request associated with the branch | |
| pr_num=$(gh pr list -s open -H "${current_branch}" -q . --json number | jq ".[] | .number") | |
| if [ -n "${pr_num}" ]; then | |
| echo "Closing existing PR #${pr_num}..." | |
| gh pr close "${pr_num}" --delete-branch || gh pr close "${pr_num}" | |
| fi | |
| # Delete the remote branch if it still exists (e.g. if no PR was open) | |
| echo "Checking if remote branch ${current_branch} exists..." | |
| if gh api "repos/${{ github.repository }}/git/refs/heads/${current_branch}" --silent 2>/dev/null; then | |
| echo "Deleting remote branch ${current_branch}..." | |
| gh api -X DELETE "repos/${{ github.repository }}/git/refs/heads/${current_branch}" || { | |
| echo "Error: Failed to delete remote branch ${current_branch}." | |
| exit 1 | |
| } | |
| fi | |
| fi | |
| - name: Set up Go | |
| uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5 | |
| with: | |
| go-version: '>=1.20.2' | |
| - name: Get librarian version | |
| id: librarian | |
| run: | | |
| version=$(go run github.com/googleapis/librarian/cmd/librarian@latest config get version) | |
| echo "version=${version}" >> $GITHUB_OUTPUT | |
| - name: Run librarian update & tidy | |
| run: | | |
| go run "github.com/googleapis/librarian/cmd/librarian@${STEPS_LIBRARIAN_OUTPUTS_VERSION}" update sources.googleapis | |
| go run "github.com/googleapis/librarian/cmd/librarian@${{ steps.librarian.outputs.version }}" tidy | |
| env: | |
| STEPS_LIBRARIAN_OUTPUTS_VERSION: ${{ steps.librarian.outputs.version }} | |
| - name: Get latest commit | |
| id: commit | |
| run: | | |
| new_commit=$(go run "github.com/googleapis/librarian/cmd/librarian@${STEPS_LIBRARIAN_OUTPUTS_VERSION}" config get sources.googleapis.commit) | |
| echo "new_commit=${new_commit}" >> $GITHUB_OUTPUT | |
| echo "short_commit=${new_commit:0:7}" >> $GITHUB_OUTPUT | |
| env: | |
| STEPS_LIBRARIAN_OUTPUTS_VERSION: ${{ steps.librarian.outputs.version }} | |
| - name: Detect Changes To Librarian.yaml | |
| id: detect_librarian | |
| run: | | |
| git add librarian.yaml | |
| changed_files=$(git diff --cached --name-only) | |
| if [[ "${changed_files}" == "" ]]; then | |
| echo "has_changes=false" >> $GITHUB_OUTPUT | |
| echo "No changes in librarian.yaml" | |
| else | |
| echo "has_changes=true" >> $GITHUB_OUTPUT | |
| fi | |
| - uses: actions/setup-java@c1e323688fd81a25caa38c78aa6df2d33d3e20d9 # v4 | |
| if: steps.detect_librarian.outputs.has_changes == 'true' | |
| with: | |
| java-version: "17" | |
| distribution: "temurin" | |
| cache: "maven" | |
| - name: Verify Java and Maven installation | |
| if: steps.detect_librarian.outputs.has_changes == 'true' | |
| run: | | |
| java -version | |
| if ! command -v mvn &> /dev/null; then | |
| sudo apt-get update && sudo apt-get install -y maven | |
| fi | |
| mvn -version | |
| - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5 | |
| if: steps.detect_librarian.outputs.has_changes == 'true' | |
| with: | |
| python-version: "3.12" | |
| cache: 'pip' | |
| - name: Run librarian install | |
| if: steps.detect_librarian.outputs.has_changes == 'true' | |
| run: | | |
| go run "github.com/googleapis/librarian/cmd/librarian@${STEPS_LIBRARIAN_OUTPUTS_VERSION}" install | |
| echo "$HOME/.cache/librarian/bin/java_tools/bin" >> $GITHUB_PATH | |
| env: | |
| PYTHONPATH: ${{ github.workspace }}/sdk-platform-java/hermetic_build/library_generation/owlbot | |
| STEPS_LIBRARIAN_OUTPUTS_VERSION: ${{ steps.librarian.outputs.version }} | |
| - name: Generate Libraries | |
| if: steps.detect_librarian.outputs.has_changes == 'true' | |
| run: | | |
| go run "github.com/googleapis/librarian/cmd/librarian@${STEPS_LIBRARIAN_OUTPUTS_VERSION}" generate --all | |
| env: | |
| STEPS_LIBRARIAN_OUTPUTS_VERSION: ${{ steps.librarian.outputs.version }} | |
| - name: Commit and Create PR | |
| if: steps.detect_librarian.outputs.has_changes == 'true' | |
| env: | |
| GH_TOKEN: ${{ secrets.CLOUD_JAVA_BOT_GITHUB_TOKEN }} | |
| PR_TITLE: "chore: update googleapis commitish to ${{ steps.commit.outputs.short_commit }}" | |
| PR_BODY: | | |
| Updated googleapis commitish in librarian.yaml to https://github.com/googleapis/googleapis/commit/${{ steps.commit.outputs.new_commit }} | |
| 💡 **Note:** If this PR is still open when the daily update workflow runs next, it will be closed and replaced with a new PR containing the latest updates. | |
| run: | | |
| set -x | |
| if [ "${{ github.event_name }}" = "pull_request" ]; then | |
| echo "=== PR Test: DRY RUN MODE ACTIVE ===" | |
| echo "Would have checked out branch: update-librarian-googleapis-main" | |
| echo "Would have committed configs with title: $PR_TITLE" | |
| echo "Would have pushed branch and created PR." | |
| exit 0 | |
| fi | |
| [ -z "$(git config user.email)" ] && git config --global user.email "cloud-java-bot@google.com" | |
| [ -z "$(git config user.name)" ] && git config --global user.name "cloud-java-bot" | |
| base_branch="main" | |
| current_branch="update-librarian-googleapis-${base_branch}" | |
| # Create and switch to the branch (force checkout -B to discard any local state on this branch name if it existed) | |
| git checkout -B "${current_branch}" | |
| # 1. Commit Config Changes | |
| # Ensure they are staged | |
| git add librarian.yaml | |
| if ! git diff --cached --quiet; then | |
| git commit -m "${PR_TITLE}" | |
| else | |
| echo "No config changes to commit" | |
| fi | |
| # 2. Commit Generated Code | |
| git add . | |
| if ! git diff --cached --quiet; then | |
| git commit -m "chore: generate libraries" | |
| else | |
| echo "No generated code changes to commit" | |
| fi | |
| # Push to remote (force push to overwrite any stale branch on remote) | |
| git remote add remote_repo https://cloud-java-bot:"${GH_TOKEN}@github.com/${{ github.repository }}.git" || git remote set-url remote_repo https://cloud-java-bot:"${GH_TOKEN}@github.com/${{ github.repository }}.git" | |
| git fetch -q remote_repo | |
| git push -f remote_repo "${current_branch}" | |
| # Create the PR | |
| gh pr create --title "${PR_TITLE}" --head "${current_branch}" --body "${PR_BODY}" --base "${base_branch}" |