Skip to content
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

Sync: Files missing to be in sync with source repo #78

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: Upload glossaries CSV to GC bucket and update glossaries in Google Translate API

on:
workflow_dispatch:
push:
branches:
- main
paths:
- 'glossaries-for-google-translate-api/**'

jobs:
upload_csv_glossaries_to_bucket_and_update_glossaries:
runs-on: ubuntu-latest
steps:

- name: Load secrets from 1Password
id: onepw_secrets
uses: 1password/load-secrets-action@v2.0.0
with:
export-env: true # Export loaded secrets as environment variables
env:
OP_SERVICE_ACCOUNT_TOKEN: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
GCLOUD_SERVICE_ACCOUNT: "op://rbiv7rvkkrsdlpcrz3bmv7nmcu/nyg5diiky4yheorujjdm5h3g6a/ctjnao73yan4bktrxmcafidfdy"
GCLOUD_SERVICE_ACCOUNT_JSON: "op://rbiv7rvkkrsdlpcrz3bmv7nmcu/nyg5diiky4yheorujjdm5h3g6a/Minifyied JSON"
GCLOUD_PROJECT_ID: "op://rbiv7rvkkrsdlpcrz3bmv7nmcu/nyg5diiky4yheorujjdm5h3g6a/Project ID"

- name: Checkout code
uses: actions/checkout@v4.1.4

# Authorize GCP, although this step might be superfluous because of next step
- name: Authorize GCP
id: authorizegcp
uses: 'google-github-actions/auth@v2'
with:
credentials_json: ${{ env.GCLOUD_SERVICE_ACCOUNT_JSON }}

# Authenticate with GCP
- name: Set up Google Cloud SDK
id: authenticategcp
uses: google-github-actions/setup-gcloud@v2.1.0
with:
version: '>= 470.0.0'
project_id: ${{ env.GCLOUD_PROJECT_ID }}
install_components: 'gcloud,gsutil'

# Upload the file to GCP Bucket. They are uploaded at the root of the bucket, existing files will be replaced without confirmation.
- name: Upload files
id: uploadfiles
run: |
for file in $GITHUB_WORKSPACE/glossaries-for-google-translate-api/*; do
filename=$(basename "$file")
gsutil cp "$file" gs://glossaries.mobilitydata.org/${filename}
done

- name: Udpate glossaries
if: ${{ success() }}
id: updateglossaries
run: |
access_token=$(gcloud auth application-default print-access-token | sed '/^$/q')

for file in $GITHUB_WORKSPACE/glossaries-for-google-translate-api/*; do

# Extract the necessary data from the filename
filename=$(basename "$file")
file_sans_ext="${filename%.*}"
language_code="${file_sans_ext##*-}"

# Construct the arguments and JSON payload
patch_url="https://translation.googleapis.com/v3/projects/810644617646/locations/us-central1/glossaries/${file_sans_ext}?update_mask=input_config,display_name"
gloss_name="projects/810644617646/locations/us-central1/glossaries/${file_sans_ext}"
gcs_source_uri="gs://glossaries.mobilitydata.org/$filename"
json_data="{ \"name\": \"$gloss_name\", \"inputConfig\": { \"gcsSource\": { \"inputUri\": \"$gcs_source_uri\" } } }"

# Execute the curl command
curl -X PATCH "$patch_url" \
-H "Authorization: Bearer $access_token" \
-H "Content-Type: application/json" \
-d "$json_data"

done
Loading