Skip to content

Commit b8787c5

Browse files
committed
add gh action
1 parent 7b19853 commit b8787c5

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
name: Generate new GAPIC client library (Hermetic Build)
2+
on:
3+
workflow_dispatch:
4+
# some inputs are ommited due to limit of 10 input arguments
5+
inputs:
6+
api_shortname:
7+
required: true
8+
type: string
9+
description: "`api_shortname`: Name for the new directory name and (default) artifact name"
10+
name_pretty:
11+
required: true
12+
type: string
13+
description: "`name_pretty`: The human-friendly name that appears in README.md"
14+
proto_path:
15+
required: true
16+
type: string
17+
description: |
18+
`proto_path`: Path to proto file from the root of the googleapis repository to the
19+
directory that contains the proto files (without the version).
20+
For example, to generate the library for 'google/maps/routing/v2',
21+
then you specify this value as 'google/maps/routing'
22+
product_docs:
23+
required: true
24+
type: string
25+
description: "`product_docs`: Documentation URL that appears in README.md"
26+
rest_docs:
27+
required: false
28+
type: string
29+
description: |
30+
`rest_docs`: If it exists, link to the REST Documentation for a service
31+
rpc_docs:
32+
required: false
33+
type: string
34+
description: |
35+
`rpc_docs`: If it exists, link to the RPC Documentation for a service
36+
api_description:
37+
required: true
38+
description: "`api_description`: Description that appears in README.md"
39+
transport:
40+
required: false
41+
type: choice
42+
default: grpc
43+
options:
44+
- grpc
45+
- http
46+
- both
47+
description: "`transport`: A label that appears in repo-metadata.json"
48+
destination_name:
49+
required: false
50+
type: string
51+
description: |
52+
`destination_name`: The directory name of the new library. By default it's
53+
java-<api_shortname>
54+
distribution_name:
55+
required: false
56+
type: string
57+
description: |
58+
`distribution_name`: Maven coordinates of the generated library. By default it's
59+
com.google.cloud:google-cloud-<api_shortname>
60+
61+
jobs:
62+
generate:
63+
runs-on: ubuntu-22.04
64+
steps:
65+
- uses: actions/checkout@v3
66+
- uses: actions/setup-python@v4
67+
with:
68+
python-version: '3.9'
69+
cache: 'pip' # caching pip dependencies
70+
- name: Install new-client.py dependencies
71+
run: pip install --require-hashes -r generation/new_client_hermetic_build/requirements.in
72+
- name: Add entry to generation_config.yaml
73+
id: config_generation
74+
run: |
75+
set -x
76+
arguments="--api_shortname=\"${API_SHORTNAME}\" \
77+
--proto-path=\"${PROTO_PATH}\" \
78+
--name-pretty=\"${NAME_PRETTY}\" \
79+
--product-docs=\"${PRODUCT_DOCS}\" \
80+
--api-description=\"${API_DESCRIPTION}\""
81+
82+
# helper function that appends a python argument only if specified in the GH action inputs
83+
append_argument() {
84+
py_arg=$1
85+
# env vars look exactly like new-client arguments but uppercase + underscores
86+
env_name=$(echo "${py_arg}" | sed 's/-/_/g' | sed -e 's/\([a-z]\)/\U\1/g')
87+
if [[ -n "${!env_name}" ]]; then
88+
# $(echo) is redundant but it works around a syntax highlighting problem in vim
89+
arguments=$(echo "${arguments} --${py_arg}=\"${!env_name}\"")
90+
fi
91+
}
92+
93+
declare -a optional_args=('transport' 'destination-name' 'distribution-name' 'group-id' 'rest-docs' 'rpc-docs')
94+
95+
for python_argument in "${optional_args[@]}"; do
96+
append_argument "${python_argument}"
97+
done
98+
echo "::set-output name=new_library_args::${arguments}"
99+
echo "${arguments} --googleapis-gen-url=\"${GOOGLEAPIS_GEN_URL}\"" \
100+
| xargs python generation/new_client_hermetic_build/new-client.py generate
101+
env:
102+
GOOGLEAPIS_GEN_URL: https://cloud-java-bot:${{ secrets.CLOUD_JAVA_BOT_TOKEN }}@github.com/googleapis/googleapis-gen.git
103+
API_SHORTNAME: ${{ github.event.inputs.api_shortname }}
104+
NAME_PRETTY: ${{ github.event.inputs.name_pretty }}
105+
PROTO_PATH: ${{ github.event.inputs.proto_path }}
106+
PRODUCT_DOCS: ${{ github.event.inputs.product_docs }}
107+
REST_DOCS: ${{ github.event.inputs.rest_docs }}
108+
RPC_DOCS: ${{ github.event.inputs.rpc_docs }}
109+
API_DESCRIPTION: ${{ github.event.inputs.api_description }}
110+
TRANSPORT: ${{ github.event.inputs.transport }}
111+
DESTINATION_NAME: ${{ github.event.inputs.destination_name }}
112+
DISTRIBUTION_NAME: ${{ github.event.inputs.distribution_name }}
113+
- name: setup docker environment
114+
shell: bash
115+
run: |
116+
# we create a volume pointing to `pwd` (google-cloud-java) that will
117+
# be referenced by the container and its children
118+
if [[ $(docker volume inspect repo-google-cloud-java) != '[]' ]]; then
119+
docker volume rm repo-google-cloud-java
120+
fi
121+
docker volume create --name "repo-google-cloud-java" --opt "type=none" --opt "device=$(pwd)" --opt "o=bind"
122+
- name: generate from configuration
123+
shell: bash
124+
run: |
125+
repo_volumes="-v repo-google-cloud-java:/workspace/google-cloud-java"
126+
docker run --rm \
127+
${repo_volumes} \
128+
-v /tmp:/tmp \
129+
-v /var/run/docker.sock:/var/run/docker.sock \
130+
-e "RUNNING_IN_DOCKER=true" \
131+
-e "REPO_BINDING_VOLUMES=${repo_volumes}" \
132+
gcr.io/cloud-devrel-public-resources/java-library-generation:latest \
133+
python /src/generate_repo.py generate \
134+
--generation-config-yaml=/workspace/google-cloud-java/generation_config.yaml \
135+
--repository-path=/workspace/google-cloud-java \
136+
--target-library-api-shortname=${API_SHORTNAME}
137+
env:
138+
API_SHORTNAME: ${{ github.event.inputs.api_shortname }}
139+
- name: Push to branch and create PR
140+
run: |
141+
set -x
142+
[ -z "`git config user.email`" ] && git config --global user.email "cloud-java-bot@google.com"
143+
[ -z "`git config user.name`" ] && git config --global user.name "cloud-java-bot"
144+
145+
# create and push to branch in origin
146+
# random_id allows multiple runs of this workflow
147+
random_id=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 5; echo)
148+
branch_name="new-library/${{ github.event.inputs.api_shortname }}-${random_id}"
149+
git checkout -b "${branch_name}"
150+
git add --all
151+
commit_message="feat: [${API_SHORTNAME}] new module for ${API_SHORTNAME}"
152+
git commit -m "${commit_message}"
153+
git remote add monorepo https://cloud-java-bot:${GH_TOKEN}@github.com/${{ github.repository }}.git
154+
git fetch -q --unshallow monorepo
155+
git push -f monorepo "${branch_name}"
156+
157+
# create PR
158+
pr_body="Generated by @${USERNAME} via [generate_new_client_hermetic_build.yaml](https://github.com/googleapis/google-cloud-java/actions/workflows/generate_new_client_hermetic_build.yaml)
159+
160+
Command used:
161+
162+
\`\`\`
163+
python generation/new_client_hermetic_build/new-client.py generate ${GENERATION_ARGUMENTS}
164+
\`\`\`"
165+
gh pr create --title "${commit_message}" --label "owlbot:run" --head "${branch_name}" --body "${pr_body}"
166+
env:
167+
USERNAME: ${{ github.actor }}
168+
API_SHORTNAME: ${{ github.event.inputs.api_shortname }}
169+
GENERATION_ARGUMENTS: ${{ steps.config_generation.outputs.new_library_args }}
170+
GH_TOKEN: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}
171+

0 commit comments

Comments
 (0)