Skip to content

Commit 05713a3

Browse files
authored
chore: update hermetic build workflow (googleapis#11114)
1 parent 0ad33c1 commit 05713a3

2 files changed

Lines changed: 25 additions & 20 deletions

File tree

.github/scripts/hermetic_library_generation.sh

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@ set -e
1717
# 1. git
1818
# 2. gh
1919
# 3. docker
20-
# 4. mvn
2120

2221
# The parameters of this script is:
2322
# 1. target_branch, the branch into which the pull request is merged.
2423
# 2. current_branch, the branch with which the pull request is associated.
25-
# 3. [optional] image_tag, the tag of gcr.io/cloud-devrel-public-resources/java-library-generation.
26-
# The value will be parsed from the generation configuration if not specified.
27-
# 4. [optional] generation_config, the path to the generation configuration,
24+
# 3. [optional] generation_config, the path to the generation configuration,
2825
# the default value is generation_config.yaml in the repository root.
2926
while [[ $# -gt 0 ]]; do
3027
key="$1"
@@ -37,10 +34,6 @@ case "${key}" in
3734
current_branch="$2"
3835
shift
3936
;;
40-
--image_tag)
41-
image_tag="$2"
42-
shift
43-
;;
4437
--generation_config)
4538
generation_config="$2"
4639
shift
@@ -54,22 +47,18 @@ shift
5447
done
5548

5649
if [ -z "${target_branch}" ]; then
57-
echo "missing required argument --target_branch"
50+
echo "Missing required argument --target_branch"
5851
exit 1
5952
fi
6053

6154
if [ -z "${current_branch}" ]; then
62-
echo "missing required argument --current_branch"
55+
echo "Missing required argument --current_branch"
6356
exit 1
6457
fi
6558

6659
if [ -z "${generation_config}" ]; then
6760
generation_config=generation_config.yaml
68-
echo "Use default generation config: ${generation_config}"
69-
fi
70-
71-
if [ -z "${image_tag}" ]; then
72-
image_tag=$(grep "gapic_generator_version" "${generation_config}" | cut -d ':' -f 2 | xargs)
61+
echo "Using default generation config: ${generation_config}"
7362
fi
7463

7564
workspace_name="/workspace"
@@ -85,10 +74,14 @@ if [[ ! ("${change_of_last_commit}" == *"${generation_config}"*) ]]; then
8574
echo "The last commit doesn't contain any changes to the generation_config.yaml, skipping the whole generation process." || true
8675
exit 0
8776
fi
77+
8878
# copy generation configuration from target branch to current branch.
8979
git show "${target_branch}":"${generation_config}" > "${baseline_generation_config}"
9080
config_diff=$(diff "${generation_config}" "${baseline_generation_config}" || true)
9181

82+
# parse image tag from the generation configuration.
83+
image_tag=$(grep "gapic_generator_version" "${generation_config}" | cut -d ':' -f 2 | xargs)
84+
9285
# get .m2 folder so it's mapped into the docker container
9386
m2_folder=$(dirname "$(mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout)")
9487

@@ -101,8 +94,15 @@ docker run \
10194
gcr.io/cloud-devrel-public-resources/java-library-generation:"${image_tag}" \
10295
--baseline-generation-config-path="${workspace_name}/${baseline_generation_config}" \
10396
--current-generation-config-path="${workspace_name}/${generation_config}"
97+
10498
# commit the change to the pull request.
105-
git add java-* pom.xml gapic-libraries-bom/pom.xml versions.txt
99+
if [[ $(basename "$(pwd)") == "google-cloud-java" ]]; then
100+
git add java-* pom.xml gapic-libraries-bom/pom.xml versions.txt
101+
else
102+
# The image leaves intermediate folders and files it works with. Here we remove them
103+
rm -rdf output googleapis "${baseline_generation_config}"
104+
git add --all -- ':!pr_description.txt'
105+
fi
106106
changed_files=$(git diff --cached --name-only)
107107
if [[ "${changed_files}" == "" ]]; then
108108
echo "There is no generated code change with the generation config change ${config_diff}."
@@ -118,4 +118,4 @@ git push
118118
if [[ -f "pr_description.txt" ]]; then
119119
pr_num=$(gh pr list -s open -H "${current_branch}" -q . --json number | jq ".[] | .number")
120120
gh pr edit "${pr_num}" --body "$(cat pr_description.txt)"
121-
fi
121+
fi

.github/workflows/hermetic_library_generation.yaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ name: Hermetic library generation upon generation config change through pull req
1717
on:
1818
pull_request:
1919

20+
env:
21+
REPO_FULL_NAME: ${{ github.event.pull_request.head.repo.full_name }}
22+
GITHUB_REPOSITORY: ${{ github.repository }}
2023
jobs:
2124
library_generation:
22-
# skip pull requests come from a forked repository
23-
if: github.event.pull_request.head.repo.full_name == github.repository
2425
runs-on: ubuntu-latest
2526
steps:
2627
- uses: actions/checkout@v4
@@ -31,6 +32,10 @@ jobs:
3132
shell: bash
3233
run: |
3334
set -x
35+
if [[ "${GITHUB_REPOSITORY}" != "${REPO_FULL_NAME}" ]]; then
36+
echo "This PR comes from a fork. Skip library generation."
37+
exit 0
38+
fi
3439
[ -z "$(git config user.email)" ] && git config --global user.email "cloud-java-bot@google.com"
3540
[ -z "$(git config user.name)" ] && git config --global user.name "cloud-java-bot"
3641
bash .github/scripts/hermetic_library_generation.sh \
@@ -39,4 +44,4 @@ jobs:
3944
env:
4045
base_ref: ${{ github.base_ref }}
4146
head_ref: ${{ github.head_ref }}
42-
GH_TOKEN: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}
47+
GH_TOKEN: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}

0 commit comments

Comments
 (0)