Skip to content

Commit

Permalink
add cloud run commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Samet committed Jan 19, 2025
1 parent 832fde4 commit c7e3acc
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,101 @@ To run web server from command line:
cargo run -- --minchar 200 --maxchar 500 --input-files nah --dir /data/inputs -o /data -w
```

To build Docker image:
```
docker build . -t <image-name> -f Dockerfile
```

To run web server in Docker:

```
docker run -v /home/user777/code/rust/text-splitter/tests:/data <image-name> /target/release/text-splitter --minchar 200 --maxchar 500 --input-files nah --dir /data/inputs -o /data -w
```

Follow these steps to deploy this service to GCP cloud run:

- Create project for our Cloud Run deployment, or reuse an existing one.
- Create Cloud Storage bucket named text-splitter-data.
- Install gcloud tool, run ```gcloud init```.

Run this in command line:
```
PROJECT=$(gcloud config get-value project)
```
This will set PROJECT environment variable for our GCP project.
We can also set LOCATION environment variable for our GCP project:
```
LOCATION=<type your location here>
```

Run this to list available locations sorted by price:
```
gcloud compute regions list --format="table(name,location,description,available)" --sort-by price
```

Then we need to enable Artifact Registry in our project:
```
gcloud services enable artifactregistry.googleapis.com
```
Now, create repository for your company:
```
gcloud artifacts repositories create --location $LOCATION
--repository-format docker <type-your-company-name>
```
Here is an example:
```
gcloud artifacts repositories create --location us --repository-format docker example-company
```

We can now construct the image URL in another environment variable:
```
IMAGE=$LOCATION-docker.pkg.dev/$PROJECT/cloud-run-book/hello
```
Finally, you need to build and tag the container image. Make sure you are in the
directory with the source and run docker build:
```
docker build . -t $IMAGE -f Dockerfile
```
You now have the container image with the correct URL, but it’s still stored on your local machine (run ```docker images``` to list all local images).

To let Docker push the local image to Artifact Registry, you need to set up credentials:
```
gcloud auth configure-docker <location>-docker.pkg.dev
```
We can now push the image to Artifact Registry:
```docker push $IMAGE```

And then we deploy it:
```
gcloud run deploy --image $IMAGE --execution-environment gen2 --add-volume=name=data,type=cloud-storage,bucket=text-splitter-data --add-volume-mount=volume=data,mount-path=/data --command /text-splitter --args="-w"
```
Then we navigate to GCP console to get the url of our Cloud Run service and save it in the environment variable:
```
ENDPOINT = <our-service-url>
```

By default, Cloud Run is deployed in private mode by default, this means that unauthenticated user, or authenticated user with the role roles/run.invoker, can’t invoke the service (or an another role with the required permissions).
Therefore, we need to use a project owner account. Follow steps below:
```
gcloud auth list
```
This will list available accounts for our gcloud command, including project owner account. Then run this command to set active account to project owner:
```
gcloud config set account <project_owner_account>
```

Finally, print the token for project owner account:
```
gcloud auth print-identity-token
```
Now we can use that token for api calls.
Before we call our service, we need to upload text files to our bucket.
Our service will process files in this bucket and save it in output folder.

```
curl -X POST \
$ENDPOINT/api/v1/run \
-H 'Content-Type: application/json' \
-d '{"list_of_files": ["example--vesting.mdx", "example--gift-card.mdx"]}'
```

0 comments on commit c7e3acc

Please sign in to comment.