Skip to content

Commit

Permalink
chore: update hermetic build workflow (#11114)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeWang1127 committed Sep 3, 2024
1 parent 0ad33c1 commit 05713a3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
34 changes: 17 additions & 17 deletions .github/scripts/hermetic_library_generation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ set -e
# 1. git
# 2. gh
# 3. docker
# 4. mvn

# The parameters of this script is:
# 1. target_branch, the branch into which the pull request is merged.
# 2. current_branch, the branch with which the pull request is associated.
# 3. [optional] image_tag, the tag of gcr.io/cloud-devrel-public-resources/java-library-generation.
# The value will be parsed from the generation configuration if not specified.
# 4. [optional] generation_config, the path to the generation configuration,
# 3. [optional] generation_config, the path to the generation configuration,
# the default value is generation_config.yaml in the repository root.
while [[ $# -gt 0 ]]; do
key="$1"
Expand All @@ -37,10 +34,6 @@ case "${key}" in
current_branch="$2"
shift
;;
--image_tag)
image_tag="$2"
shift
;;
--generation_config)
generation_config="$2"
shift
Expand All @@ -54,22 +47,18 @@ shift
done

if [ -z "${target_branch}" ]; then
echo "missing required argument --target_branch"
echo "Missing required argument --target_branch"
exit 1
fi

if [ -z "${current_branch}" ]; then
echo "missing required argument --current_branch"
echo "Missing required argument --current_branch"
exit 1
fi

if [ -z "${generation_config}" ]; then
generation_config=generation_config.yaml
echo "Use default generation config: ${generation_config}"
fi

if [ -z "${image_tag}" ]; then
image_tag=$(grep "gapic_generator_version" "${generation_config}" | cut -d ':' -f 2 | xargs)
echo "Using default generation config: ${generation_config}"
fi

workspace_name="/workspace"
Expand All @@ -85,10 +74,14 @@ if [[ ! ("${change_of_last_commit}" == *"${generation_config}"*) ]]; then
echo "The last commit doesn't contain any changes to the generation_config.yaml, skipping the whole generation process." || true
exit 0
fi

# copy generation configuration from target branch to current branch.
git show "${target_branch}":"${generation_config}" > "${baseline_generation_config}"
config_diff=$(diff "${generation_config}" "${baseline_generation_config}" || true)

# parse image tag from the generation configuration.
image_tag=$(grep "gapic_generator_version" "${generation_config}" | cut -d ':' -f 2 | xargs)

# get .m2 folder so it's mapped into the docker container
m2_folder=$(dirname "$(mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout)")

Expand All @@ -101,8 +94,15 @@ docker run \
gcr.io/cloud-devrel-public-resources/java-library-generation:"${image_tag}" \
--baseline-generation-config-path="${workspace_name}/${baseline_generation_config}" \
--current-generation-config-path="${workspace_name}/${generation_config}"

# commit the change to the pull request.
git add java-* pom.xml gapic-libraries-bom/pom.xml versions.txt
if [[ $(basename "$(pwd)") == "google-cloud-java" ]]; then
git add java-* pom.xml gapic-libraries-bom/pom.xml versions.txt
else
# The image leaves intermediate folders and files it works with. Here we remove them
rm -rdf output googleapis "${baseline_generation_config}"
git add --all -- ':!pr_description.txt'
fi
changed_files=$(git diff --cached --name-only)
if [[ "${changed_files}" == "" ]]; then
echo "There is no generated code change with the generation config change ${config_diff}."
Expand All @@ -118,4 +118,4 @@ git push
if [[ -f "pr_description.txt" ]]; then
pr_num=$(gh pr list -s open -H "${current_branch}" -q . --json number | jq ".[] | .number")
gh pr edit "${pr_num}" --body "$(cat pr_description.txt)"
fi
fi
11 changes: 8 additions & 3 deletions .github/workflows/hermetic_library_generation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ name: Hermetic library generation upon generation config change through pull req
on:
pull_request:

env:
REPO_FULL_NAME: ${{ github.event.pull_request.head.repo.full_name }}
GITHUB_REPOSITORY: ${{ github.repository }}
jobs:
library_generation:
# skip pull requests come from a forked repository
if: github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -31,6 +32,10 @@ jobs:
shell: bash
run: |
set -x
if [[ "${GITHUB_REPOSITORY}" != "${REPO_FULL_NAME}" ]]; then
echo "This PR comes from a fork. Skip library generation."
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"
bash .github/scripts/hermetic_library_generation.sh \
Expand All @@ -39,4 +44,4 @@ jobs:
env:
base_ref: ${{ github.base_ref }}
head_ref: ${{ github.head_ref }}
GH_TOKEN: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}
GH_TOKEN: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}

0 comments on commit 05713a3

Please sign in to comment.