Skip to content

Commit 2e0b752

Browse files
authored
chore(ci): add daily librarian workflow to update googleapis commitish (#13292)
Added workflow to update sources.googleapis in librarian.yaml, scheduled to run daily, can be triggered manually. In case of there is already an open PR for it, simply let the workflow fail. The workflow also triggers a dry-run mode on PR for this workflow. Fixes #5435 Test run succeeded: https://github.com/googleapis/google-cloud-java/actions/runs/26598366465/job/78375192026?pr=13292
1 parent d1091cc commit 2e0b752

1 file changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
name: Update librarian googleapis commitish
16+
on:
17+
schedule:
18+
- cron: '0 3 * * *' # Run once a day at 3:00 AM UTC
19+
workflow_dispatch: # Allow manual trigger
20+
pull_request: # Run in dry-run mode to test changes to this workflow itself
21+
paths:
22+
- '.github/workflows/update_librarian_googleapis.yaml'
23+
jobs:
24+
update-librarian-googleapis:
25+
runs-on: ubuntu-24.04
26+
defaults:
27+
run:
28+
working-directory: google-cloud-java
29+
steps:
30+
- name: Checkout google-cloud-java
31+
uses: actions/checkout@v6
32+
with:
33+
repository: googleapis/google-cloud-java
34+
path: google-cloud-java
35+
fetch-depth: 0
36+
- name: Check for Open PR
37+
env:
38+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39+
run: |
40+
set -x
41+
current_branch="update-librarian-googleapis-main"
42+
43+
if [ "${{ github.event_name }}" = "pull_request" ]; then
44+
echo "PR Test: Skipping open PR check."
45+
else
46+
# Try to find an open pull request associated with the branch
47+
pr_num=$(gh pr list -s open -H "${current_branch}" -q . --json number | jq ".[] | .number")
48+
49+
if [ -n "${pr_num}" ]; then
50+
echo "Error: An open Pull Request already exists for this update: PR #${pr_num}."
51+
echo "Please merge or close the existing PR before running this workflow again."
52+
exit 1 # Fails this step and the workflow
53+
fi
54+
fi
55+
- name: Set up Go
56+
uses: actions/setup-go@v5
57+
with:
58+
go-version: '>=1.20.2'
59+
- name: Run librarian update
60+
run: |
61+
version=$(go run github.com/googleapis/librarian/cmd/librarian@latest config get version)
62+
go run "github.com/googleapis/librarian/cmd/librarian@${version}" update sources.googleapis
63+
- name: Get latest commit
64+
id: commit
65+
run: |
66+
version=$(go run github.com/googleapis/librarian/cmd/librarian@latest config get version)
67+
new_commit=$(go run "github.com/googleapis/librarian/cmd/librarian@${version}" config get sources.googleapis.commit)
68+
echo "new_commit=${new_commit}" >> $GITHUB_OUTPUT
69+
echo "short_commit=${new_commit:0:7}" >> $GITHUB_OUTPUT
70+
- name: Detect Changes
71+
id: detect
72+
run: |
73+
git add librarian.yaml
74+
changed_files=$(git diff --cached --name-only)
75+
if [[ "${changed_files}" == "" ]]; then
76+
echo "has_changes=false" >> $GITHUB_OUTPUT
77+
echo "No changes in librarian.yaml"
78+
else
79+
echo "has_changes=true" >> $GITHUB_OUTPUT
80+
fi
81+
- name: Commit and Create PR
82+
if: steps.detect.outputs.has_changes == 'true'
83+
env:
84+
GH_TOKEN: ${{ secrets.CLOUD_JAVA_BOT_GITHUB_TOKEN }}
85+
PR_TITLE: "chore: update googleapis commitish to ${{ steps.commit.outputs.short_commit }}"
86+
PR_BODY: "Updated googleapis commitish in librarian.yaml to https://github.com/googleapis/googleapis/commit/${{ steps.commit.outputs.new_commit }}"
87+
run: |
88+
set -x
89+
90+
if [ "${{ github.event_name }}" = "pull_request" ]; then
91+
echo "=== PR Test: DRY RUN MODE ACTIVE ==="
92+
echo "Would have checked out branch: update-librarian-googleapis-main"
93+
echo "Would have committed with title: $PR_TITLE"
94+
echo "Would have pushed branch and created PR."
95+
exit 0
96+
fi
97+
98+
[ -z "$(git config user.email)" ] && git config --global user.email "cloud-java-bot@google.com"
99+
[ -z "$(git config user.name)" ] && git config --global user.name "cloud-java-bot"
100+
101+
base_branch="main"
102+
current_branch="update-librarian-googleapis-${base_branch}"
103+
104+
# Create and switch to the branch (force checkout -B to discard any local state on this branch name if it existed)
105+
git checkout -B "${current_branch}"
106+
107+
# Commit the changes (they are already staged by the Detect Changes step!)
108+
git commit -m "${PR_TITLE}"
109+
110+
# Push to remote (force push to overwrite any stale branch on remote)
111+
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"
112+
git fetch -q remote_repo
113+
git push -f remote_repo "${current_branch}"
114+
115+
# Create the PR
116+
gh pr create --title "${PR_TITLE}" --head "${current_branch}" --body "${PR_BODY}" --base "${base_branch}"

0 commit comments

Comments
 (0)