Skip to content

Commit 593e2f0

Browse files
authored
chore: add a composite action for library generation (#3173)
In this PR: - Add a composite action to execute library generation shell script. - Separate repo specific logic, e.g., installing modules, building images, etc., in the generation shell script. An example workflow in google-cloud-java: googleapis/google-cloud-java#11117
1 parent 7996aab commit 593e2f0

File tree

4 files changed

+105
-28
lines changed

4 files changed

+105
-28
lines changed

.cloudbuild/library_generation/library_generation.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ COPY . .
2222
ENV DOCKER_GAPIC_GENERATOR_VERSION="2.45.1-SNAPSHOT"
2323
# {x-version-update-end}
2424

25-
RUN mvn install -DskipTests -Dclirr.skip -Dcheckstyle.skip
25+
RUN mvn install -B -ntp -DskipTests -Dclirr.skip -Dcheckstyle.skip
2626
RUN cp "/root/.m2/repository/com/google/api/gapic-generator-java/${DOCKER_GAPIC_GENERATOR_VERSION}/gapic-generator-java-${DOCKER_GAPIC_GENERATOR_VERSION}.jar" \
2727
"./gapic-generator-java.jar"
2828

.github/scripts/action.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright 2024 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+
# GitHub action job to test core java library features on
15+
# downstream client libraries before they are released.
16+
17+
# This composite action should be used in google-cloud-java and handwritten
18+
# libraries to generate changed libraries.
19+
# This composite action serves as a source of truth of scripts that run
20+
# library generation and create pull requests.
21+
name: Hermetic library generation
22+
description: Runs hermetic library generation to produce changed libraries
23+
inputs:
24+
base_ref:
25+
description: base branch
26+
required: true
27+
head_ref:
28+
description: head branch
29+
required: true
30+
image_tag:
31+
description: the tag of hermetic build image
32+
required: false
33+
token:
34+
description: Personal Access Token
35+
required: true
36+
37+
runs:
38+
using: "composite"
39+
steps:
40+
- name: Copy shell script
41+
shell: bash
42+
run: |
43+
cd ${{ github.action_path }}
44+
cp hermetic_library_generation.sh $GITHUB_WORKSPACE
45+
- name: Generate changed libraries
46+
shell: bash
47+
run: |
48+
set -x
49+
[ -z "$(git config user.email)" ] && git config --global user.email "[email protected]"
50+
[ -z "$(git config user.name)" ] && git config --global user.name "cloud-java-bot"
51+
cd "${GITHUB_WORKSPACE}"
52+
bash hermetic_library_generation.sh \
53+
--target_branch "${BASE_REF}" \
54+
--current_branch "${HEAD_REF}" \
55+
--image_tag "${IMAGE_TAG}"
56+
env:
57+
BASE_REF: ${{ inputs.base_ref }}
58+
HEAD_REF: ${{ inputs.head_ref }}
59+
IMAGE_TAG: ${{ inputs.image_tag }}
60+
GH_TOKEN: ${{ inputs.token }}

.github/scripts/hermetic_library_generation.sh

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ set -e
2222
# The parameters of this script is:
2323
# 1. target_branch, the branch into which the pull request is merged.
2424
# 2. current_branch, the branch with which the pull request is associated.
25-
# 3. [optional] generation_config, the path to the generation configuration,
25+
# 3. [optional] image_tag, the tag of gcr.io/cloud-devrel-public-resources/java-library-generation.
26+
# 4. [optional] generation_config, the path to the generation configuration,
2627
# the default value is generation_config.yaml in the repository root.
2728
while [[ $# -gt 0 ]]; do
2829
key="$1"
@@ -35,6 +36,10 @@ case "${key}" in
3536
current_branch="$2"
3637
shift
3738
;;
39+
--image_tag)
40+
image_tag="$2"
41+
shift
42+
;;
3843
--generation_config)
3944
generation_config="$2"
4045
shift
@@ -62,7 +67,10 @@ if [ -z "${generation_config}" ]; then
6267
echo "Use default generation config: ${generation_config}"
6368
fi
6469

65-
image_tag=local
70+
if [ -z "${image_tag}" ]; then
71+
image_tag=$(grep "gapic_generator_version" "${generation_config}" | cut -d ':' -f 2 | xargs)
72+
fi
73+
6674
workspace_name="/workspace"
6775
baseline_generation_config="baseline_generation_config.yaml"
6876
message="chore: generate libraries at $(date)"
@@ -73,31 +81,23 @@ git checkout "${current_branch}"
7381
# copy generation configuration from target branch to current branch.
7482
git show "${target_branch}":"${generation_config}" > "${baseline_generation_config}"
7583

76-
generator_version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout -pl gapic-generator-java)
77-
echo "Local generator version: ${generator_version}"
78-
79-
# install generator locally since we're using a SNAPSHOT version.
80-
mvn -V -B -ntp clean install -DskipTests
84+
# get .m2 folder so it's mapped into the docker container
85+
m2_folder=$(dirname "$(mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout)")
8186

82-
# build image locally since we want to include latest change.
83-
docker build \
84-
-f .cloudbuild/library_generation/library_generation.Dockerfile \
85-
-t gcr.io/cloud-devrel-public-resources/java-library-generation:"${image_tag}" \
86-
.
8787
# run hermetic code generation docker image.
8888
docker run \
8989
--rm \
9090
-u "$(id -u):$(id -g)" \
9191
-v "$(pwd):${workspace_name}" \
92-
-v "$HOME"/.m2:/home/.m2 \
93-
-e GENERATOR_VERSION="${generator_version}" \
92+
-v "${m2_folder}":/home/.m2 \
93+
-e GENERATOR_VERSION="${image_tag}" \
9494
gcr.io/cloud-devrel-public-resources/java-library-generation:"${image_tag}" \
9595
--baseline-generation-config-path="${workspace_name}/${baseline_generation_config}" \
9696
--current-generation-config-path="${workspace_name}/${generation_config}"
9797

9898
# commit the change to the pull request.
9999
rm -rdf output googleapis "${baseline_generation_config}"
100-
git add --all -- ':!pr_description.txt'
100+
git add --all -- ':!pr_description.txt' ':!hermetic_library_generation.sh'
101101
changed_files=$(git diff --cached --name-only)
102102
if [[ "${changed_files}" == "" ]]; then
103103
echo "There is no generated code change."

.github/workflows/hermetic_library_generation.yaml

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,41 @@ jobs:
2424
library_generation:
2525
runs-on: ubuntu-latest
2626
steps:
27+
- name: Determine whether the pull request comes from a fork
28+
run: |
29+
if [[ "${GITHUB_REPOSITORY}" != "${REPO_FULL_NAME}" ]]; then
30+
echo "This PR comes from a fork. Skip library generation."
31+
echo "SHOULD_RUN=false" >> $GITHUB_ENV
32+
else
33+
echo "SHOULD_RUN=true" >> $GITHUB_ENV
34+
fi
2735
- uses: actions/checkout@v4
36+
if: env.SHOULD_RUN == 'true'
2837
with:
2938
fetch-depth: 0
3039
token: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}
31-
- name: Generate changed libraries
40+
- name: Install Maven modules
41+
if: env.SHOULD_RUN == 'true'
3242
shell: bash
3343
run: |
34-
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
39-
[ -z "$(git config user.email)" ] && git config --global user.email "[email protected]"
40-
[ -z "$(git config user.name)" ] && git config --global user.name "cloud-java-bot"
41-
bash .github/scripts/hermetic_library_generation.sh \
42-
--target_branch "${base_ref}" \
43-
--current_branch "${head_ref}"
44+
git checkout "${HEAD_REF}"
45+
mvn install -B -ntp -DskipTests -Dclirr.skip -Dcheckstyle.skip
4446
env:
47+
HEAD_REF: ${{ github.head_ref }}
48+
- name: Build image
49+
if: env.SHOULD_RUN == 'true'
50+
shell: bash
51+
run: |
52+
GENERATOR_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout -pl gapic-generator-java)
53+
echo "GENERATOR_VERSION=${GENERATOR_VERSION}" >> "$GITHUB_ENV"
54+
docker build \
55+
-f .cloudbuild/library_generation/library_generation.Dockerfile \
56+
-t gcr.io/cloud-devrel-public-resources/java-library-generation:"${GENERATOR_VERSION}" \
57+
.
58+
- uses: ./.github/scripts
59+
if: env.SHOULD_RUN == 'true'
60+
with:
4561
base_ref: ${{ github.base_ref }}
4662
head_ref: ${{ github.head_ref }}
47-
GH_TOKEN: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}
63+
image_tag: ${{ env.GENERATOR_VERSION }}
64+
token: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}

0 commit comments

Comments
 (0)