An example of how to deploy an R Shiny app on Google Cloud Run.
Work from Cloud Shell by clicking the button below:
Or you can clone this repository to your development environment of choice
git clone https://github.com/justinjm/google-cloud-run-r-shiny
run the following in Cloud Shell Terminal to trigger authentication
gcloud config list
- Install
gcloud
CLI - Set ADC on your local machine so you can test the app locally https://cloud.google.com/sdk/gcloud/reference/auth/application-default/login
gcloud auth application-default login
As global environment variables here for re-use throughout the rest of the steps
PROJECT_ID=$(gcloud config get-value project)
REGION="us-central1"
SVC_ACCOUNT_NAME="shiny-run"
DOCKER_REPO="shiny-run"
IMAGE_NAME="shiny-run"
IMAGE_TAG="latest"
IMAGE_URI="$REGION-docker.pkg.dev/$PROJECT_ID/$DOCKER_REPO/$IMAGE_NAME:$IMAGE_TAG"
SERVICE_NAME="shiny"
Create an .Renviron
file in the root of this project directory and then copy it to the build/app/
directory by running the following:
cat << EOF > ./.Renviron
# .Renviron
PROJECT_ID=$PROJECT_ID
REGION=$REGION
EOF
cp ./.Renviron ./build/app/.Renviron
While it's non-ideal we need the .Renviron
file in 2 places:
- the project root directory for development of the shiny app in RStudio, this is where the app looks
- the
build/app
directory for deployment of the shiny app to Google Cloud Run
This can be avoided with a Terraform workflow and I plan to add it here, see issue) for status.
Please note that for now, you must ensure these 2 files are in sync to avoid any deployment issues.
gcloud services enable \
bigquery.googleapis.com \
cloudbuild.googleapis.com \
artifactregistry.googleapis.com \
run.googleapis.com
Create an Artifact Registry (AR) repository (repo) to serve as our docker repository,
Running the code below will check if the repo exists first and create only if it does not exist.
## Create artifact registry only if it does not already exist
# Check if the repository already exists
if gcloud artifacts repositories describe $DOCKER_REPO --location=$REGION &> /dev/null; then
echo "Repository $DOCKER_REPO already exists:"
gcloud artifacts repositories describe $DOCKER_REPO --location=$REGION
else
# Create the repository if it doesn't exist
echo "Respository does not exist. Creating...."
gcloud artifacts repositories create $DOCKER_REPO \
--repository-format=docker \
--location=$REGION \
--description="Docker repository for Shiny on Cloud Run demo"
echo "Repository created."
gcloud artifacts repositories describe $DOCKER_REPO --location=$REGION
fi
gcloud auth configure-docker $REGION-docker.pkg.dev --quiet
gcloud builds submit --region=$REGION --tag=$IMAGE_URI --timeout=1h ./build
To follow Google Cloud recommended best practices for service accounts.
- https://cloud.google.com/run/docs/deploying#permissions_required_to_deploy
- https://cloud.google.com/run/docs/reference/iam/roles#additional-configuration
- https://cloud.google.com/run/docs/securing/service-identity#gcloud
- https://cloud.google.com/iam/docs/service-accounts-create#iam-service-accounts-create-gcloud
- https://cloud.google.com/iam/docs/understanding-roles
gcloud iam service-accounts create $SVC_ACCOUNT_NAME \
--description="For deploying R Shiny apps on Cloud Run" \
--display-name="R Shiny Cloud Run service account"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:$SVC_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/aiplatform.user"
save as global variable for use in next step
export SVC_ACCOUNT_EMAIL=$SVC_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com
echo $SVC_ACCOUNT_EMAIL
Deploy app from container image we built in previous step to be publicly accessible
https://cloud.google.com/sdk/gcloud/reference/run/deploy https://cloud.google.com/run/docs/configuring/session-affinity
source args
gcloud run deploy $SERVICE_NAME \
--image $IMAGE_URI \
--region=$REGION \
--platform="managed" \
--port="5000" \
--allow-unauthenticated \
--session-affinity \
--service-account=$SVC_ACCOUNT_EMAIL \
--min-instances=1 \
--max-instances=10
See docs/debugging.md for instructions
First, deploy allowing only authenticated users:
source args
gcloud run deploy $SERVICE_NAME \
--image $IMAGE_URI \
--region=$REGION \
--platform="managed" \
--port="5000" \
--no-allow-unauthenticated \
--session-affinity \
--service-account=$SVC_ACCOUNT_EMAIL \
--min-instances=1 \
--max-instances=10
Then access via a proxy
gcloud beta run services proxy $SERVICE_NAME --project=$PROJECT_ID --region=$REGION
Delete all created services
## gcloud run services delete $SERVICE_NAME --region=$REGION
## gcloud artifacts repositories delete $DOCKER_REPO ##
## gcloud iam service-accounts delete $SVC_ACCOUNT_EMAIL ##
Source code forked from tolgakurtuluss/shinychatgpt, thank you to tolgakurtuluss for open sourcing and sharing your project.