Skip to content

chore: update hermetic library generation workflow #10693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 0 additions & 113 deletions .github/workflows/generate-from-configuration.yaml

This file was deleted.

48 changes: 2 additions & 46 deletions .github/workflows/generate_new_client_hermetic_build.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Generate new GAPIC client library (Hermetic Build)
on:
workflow_dispatch:
# some inputs are ommited due to limit of 10 input arguments
# some inputs are emitted due to limit of 10 input arguments
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: ommited should be omitted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

inputs:
api_shortname:
required: true
Expand Down Expand Up @@ -53,7 +53,7 @@ jobs:
generate:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: '3.9'
Expand All @@ -78,36 +78,6 @@ jobs:
API_DESCRIPTION: ${{ github.event.inputs.api_description }}
LIBRARY_NAME: ${{ github.event.inputs.library_name }}
DISTRIBUTION_NAME: ${{ github.event.inputs.distribution_name }}
- name: setup docker environment
shell: bash
run: |
set -x
# we create a volume pointing to `pwd` (google-cloud-java) that will
# be referenced by the container and its children
if [[ $(docker volume inspect repo-google-cloud-java) != '[]' ]]; then
docker volume rm repo-google-cloud-java
fi
docker volume create --name "repo-google-cloud-java" --opt "type=none" --opt "device=$(pwd)" --opt "o=bind"
- name: generate from configuration
id: generation
shell: bash
run: |
set -x
repo_volumes="-v repo-google-cloud-java:/workspace/google-cloud-java"
echo "::set-output name=repo_volumes::${repo_volumes}"
docker run --rm \
${repo_volumes} \
-v /tmp:/tmp \
-v /var/run/docker.sock:/var/run/docker.sock \
-e "RUNNING_IN_DOCKER=true" \
-e "REPO_BINDING_VOLUMES=${repo_volumes}" \
gcr.io/cloud-devrel-public-resources/java-library-generation:latest \
python /src/generate_repo.py generate \
--generation-config-yaml=/workspace/google-cloud-java/generation_config.yaml \
--repository-path=/workspace/google-cloud-java \
--target-library-names=${API_SHORTNAME}
env:
API_SHORTNAME: ${{ github.event.inputs.api_shortname }}
- name: Push to branch and create PR
run: |
set -x
Expand All @@ -133,19 +103,6 @@ jobs:

\`\`\`
python generation/new_client_hermetic_build/add-new-client-config.py add-new-client ${GENERATION_ARGUMENTS}

docker run --rm \\
${DOCKER_VOLUMES} \\
-v /tmp:/tmp \\
-v /var/run/docker.sock:/var/run/docker.sock \\
-e \"RUNNING_IN_DOCKER=true\" \\
-e \"REPO_BINDING_VOLUMES=${DOCKER_VOLUMES}\" \\
gcr.io/cloud-devrel-public-resources/java-library-generation:latest \\
python /src/generate_repo.py generate \\
--generation-config-yaml=/workspace/google-cloud-java/generation_config.yaml \\
--repository-path=/workspace/google-cloud-java \\
--target-library-names=${API_SHORTNAME}

\`\`\`"
gh pr create --title "${commit_message}" --label "owlbot:run" --head "${branch_name}" --body "${pr_body}"
env:
Expand All @@ -154,4 +111,3 @@ jobs:
GENERATION_ARGUMENTS: ${{ steps.config_generation.outputs.new_library_args }}
DOCKER_VOLUMES: ${{ steps.generation.outputs.repo_volumes }}
GH_TOKEN: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}

65 changes: 54 additions & 11 deletions .github/workflows/hermetic_library_generation.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
name: Hermetic library generation upon generation config change through pull requests
on:
pull_request:
types:
- synchronize
paths:
- generation_config.yaml
- "generation_config.yaml"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the trigger only for main branch? If yes, can this be configured to be triggered for other branches, like a future LTS branch?

Copy link
Contributor Author

@JoeWang1127 JoeWang1127 Apr 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be triggered when a pull request changes generation_config.yaml.

Therefore, it can be used in any branch since the pull request is not necessarily based on main.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any configuration for a target branch, does it mean if I create a PR with generation_config.yaml changes against a release/2.x branch, this workflow would be triggered too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

jobs:
library_generation:
runs-on: ubuntu-latest
Expand All @@ -14,14 +12,59 @@ jobs:
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
fetch-depth: 0
- name: checkout the target branch
shell: bash
run: |
git checkout ${{ github.base_ref }}
- name: checkout the current branch
shell: bash
run: |
git checkout ${{ github.head_ref }}
- name: get baseline generation config
uses: actions/checkout@v4
with:
ref: ${{ github.base_ref }}
path: baseline
sparse-checkout: generation_config.yaml
- name: diff
shell: bash
run: |
diff generation_config.yaml baseline/generation_config.yaml
git show ${{ github.base_ref }}:generation_config.yaml > baseline_generation_config.yaml
- name: show configuration diff
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we extract most of the logics here to a shell script so that we can call the script directly here? That way we can easily call it in local and possibly in a non-Github environment in the future.
Ideally, we should try to put this logic into Docker container as well, so that this logic could be reused by handwritten libraries, otherwise we would need to duplicate this in every handwritten repos. Bake it into Docker can be a future enhancement, but I think we should at least extract it to a separate script.

Copy link
Contributor Author

@JoeWang1127 JoeWang1127 Apr 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved command lines to a script.

shell: bash
run: |
diff generation_config.yaml baseline_generation_config.yaml || echo "config diff"
- name: create a docker volume
shell: bash
run: |
set -x
# we create a volume pointing to `pwd` (google-cloud-java) that will
# be referenced by the container and its children
if [[ $(docker volume inspect repo-google-cloud-java) != '[]' ]]; then
docker volume rm repo-google-cloud-java
fi
docker volume create --name "repo-google-cloud-java" --opt "type=none" --opt "device=$(pwd)" --opt "o=bind"
- name: generate changed libraries
shell: bash
run: |
docker run --rm \
${repo_volumes} \
-v /tmp:/tmp \
-v /var/run/docker.sock:/var/run/docker.sock \
-e "RUNNING_IN_DOCKER=true" \
-e "REPO_BINDING_VOLUMES=${repo_volumes}" \
gcr.io/cloud-devrel-public-resources/java-library-generation:"${library_generation_image_tag}" \
python /src/cli/entry_point.py generate \
--baseline-generation-config-path=/workspace/google-cloud-java/baseline_generation_config.yaml \
--current-generation-config-path=/workspace/google-cloud-java/generation_config.yaml \
--repository-path=/workspace/google-cloud-java
- name: push commit to the pull request
shell: bash
run: |
[ -z "`git config user.email`" ] && git config --global user.email "[email protected]"
[ -z "`git config user.name`" ] && git config --global user.name "cloud-java-bot"
git add java-* pom.xml gapic-libraries-bom/pom.xml versions.txt generation_config.yaml
message="chore: generate libraries at $(date)"
git commit --allow-empty -m "${message}"
git push -f
if [[ -f "pr_description.txt" ]]; then
pr_num=$(gh pr list -s open -H ${{ github.head_ref }} -q . --json number | jq ".[] | .number")
gh pr edit "${pr_num}" --body "$(cat pr_description.txt)"
fi
env:
GH_TOKEN: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}
62 changes: 62 additions & 0 deletions .github/workflows/update-googleapis-commit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Update googleapis commit
on:
schedule:
- cron: '* 2 * * *'
workflow_dispatch:


jobs:
update-googleapis-commit:
runs-on: ubuntu-22.04
env:
# the branch into which the pull request is merged
base_branch: main
steps:
- uses: actions/checkout@v4
- name: Setup branch for pull request
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above. Let's extract it to a script for now, and think about ways to make it more reusable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved command lines to a script.

shell: bash
run: |
[ -z "`git config user.email`" ] && git config --global user.email "[email protected]"
[ -z "`git config user.name`" ] && git config --global user.name "cloud-java-bot"
# try to find a open pull request associated with the branch
pr_num=$(gh pr list -s open -H "generate-libraries-${base_branch}" -q . --json number | jq ".[] | .number")
# create a branch if there's no open pull request associated with the
# branch; otherwise checkout the pull request.
if [ -z "${pr_num}" ]; then
git checkout -b "generate-libraries-${base_branch}"
else
gh pr checkout "${pr_num}"
fi
echo "pr_num=${pr_num}" >> "$GITHUB_ENV"
env:
GH_TOKEN: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}
- name: Update googleapis commit to latest
shell: bash
run: |
mkdir tmp-googleapis
# use partial clone because only commit history is needed.
git clone --filter=blob:none https://github.com/googleapis/googleapis.git tmp-googleapis
pushd tmp-googleapis
git pull
latest_commit=$(git rev-parse HEAD)
popd
rm -rf tmp-googleapis
sed -i -e "s/^googleapis_commitish.*$/googleapis_commitish: ${latest_commit}/" generation_config.yaml
- name: create or update the pull request
shell: bash
run: |
title="chore: update googleapis commit at $(date)"
git add generation_config.yaml
# use --allow-empty because (rarely) there's no change.
git commit --allow-empty -m "${title}"
if [ -z "${pr_num}" ]; then
git remote add monorepo https://cloud-java-bot:${GH_TOKEN}@github.com/${{ github.repository }}.git
git fetch -q --unshallow monorepo
git push -f monorepo "generate-libraries-${base_branch}"
gh pr create --title "${title}" --head "generate-libraries-${base_branch}" --body "${title}" --base "${base_branch}"
else
git push
fi
env:
GH_TOKEN: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}
pr_num: ${{ env.pr_num }}