From 05713a3607459c0764474700b792d3b1c6730b1c Mon Sep 17 00:00:00 2001 From: Joe Wang <106995533+JoeWang1127@users.noreply.github.com> Date: Tue, 3 Sep 2024 18:20:20 +0000 Subject: [PATCH] chore: update hermetic build workflow (#11114) --- .../scripts/hermetic_library_generation.sh | 34 +++++++++---------- .../hermetic_library_generation.yaml | 11 ++++-- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/.github/scripts/hermetic_library_generation.sh b/.github/scripts/hermetic_library_generation.sh index 84d69a971fda..77d5f33675fa 100755 --- a/.github/scripts/hermetic_library_generation.sh +++ b/.github/scripts/hermetic_library_generation.sh @@ -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" @@ -37,10 +34,6 @@ case "${key}" in current_branch="$2" shift ;; - --image_tag) - image_tag="$2" - shift - ;; --generation_config) generation_config="$2" shift @@ -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" @@ -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)") @@ -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}." @@ -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 \ No newline at end of file diff --git a/.github/workflows/hermetic_library_generation.yaml b/.github/workflows/hermetic_library_generation.yaml index ebc9ab770ccd..164743610495 100644 --- a/.github/workflows/hermetic_library_generation.yaml +++ b/.github/workflows/hermetic_library_generation.yaml @@ -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 @@ -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 \ @@ -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 }} \ No newline at end of file