<commit_before># First Deployment

*In this section, we'll create our first deployment.*

**We're nearly there!**

We're up to deploying our project for the first time!

This is going to be a bit complicated at first, but the work we do here will make every other deployment much simplier. 

You'll remember way back when we were [testing locally](00-test-local.md) we built two images: a `web` image and a `db` image. 

`db` will be replaced with the proper postgres database we've [already setup](20-setup-sql.md). 

So we just need to build the other image!

For that, we'll be using Cloud Build. This is a similar process to `docker build .`, but this will build the image on Google Cloud itself, and publish the image to the Google Cloud image repository, `gcr.io`. 

If you haven't already done so, you'll need to clone the code into a local copy: 

```shell
git clone https://github.com/GoogleCloudPlatform/django-demo-app-unicodex
cd django-demo-app-unicodex
```

Then, build the image using the `gcloud` command:

```shell
gcloud builds submit --tag gcr.io/$PROJECT_ID/unicodex .
```

This will build the image much like Docker might locally, but within Google Cloud (no need to install Docker locally!)

Then, we can (finally!) create our Cloud Run service using this image. We'll also tell it all about the database we setup earlier, and all those secrets: 

```shell
gcloud beta run deploy unicodex \
    --allow-unauthenticated \
    --region $REGION \
    --image gcr.io/$PROJECT_ID/unicodex \
    --update-env-vars DATABASE_URL=berglas://${BERGLAS_BUCKET}/database_url,SECRET_KEY=berglas://${BERGLAS_BUCKET}/secret_key,GS_BUCKET_NAME=berglas://${BERGLAS_BUCKET}/media_bucket \
    --add-cloudsql-instances $DATABASE_INSTANCE
```

*Note:* Cloud Run is still in beta at the time of writing. There may be additional options you have to set here. Best bet is to choose the default that is offered. 

Sadly, we have a few more steps. Even though we have deployed our service, **Django won't work yet**. Because: 

* We need to tell Django where to expect our service to run, and 
* we need to initalise our database.

#### Service URL, and `ALLOWED_HOSTS` 

We can either copy the URL from the output we got from the last step, or we can get it from `gcloud`

```shell,exclude
gcloud beta run services list
```<commit_msg>Add note about fully qualified database instance<commit_after># First Deployment

*In this section, we'll create our first deployment.*

**We're nearly there!**

We're up to deploying our project for the first time!

This is going to be a bit complicated at first, but the work we do here will make every other deployment much simplier. 

You'll remember way back when we were [testing locally](00-test-local.md) we built two images: a `web` image and a `db` image. 

`db` will be replaced with the proper postgres database we've [already setup](20-setup-sql.md). 

So we just need to build the other image!

For that, we'll be using Cloud Build. This is a similar process to `docker build .`, but this will build the image on Google Cloud itself, and publish the image to the Google Cloud image repository, `gcr.io`. 

If you haven't already done so, you'll need to clone the code into a local copy: 

```shell
git clone https://github.com/GoogleCloudPlatform/django-demo-app-unicodex
cd django-demo-app-unicodex
```

Then, build the image using the `gcloud` command:

```shell
gcloud builds submit --tag gcr.io/$PROJECT_ID/unicodex .
```

This will build the image much like Docker might locally, but within Google Cloud (no need to install Docker locally!)

Then, we can (finally!) create our Cloud Run service using this image. We'll also tell it all about the database we setup earlier, and all those secrets: 

```shell
gcloud beta run deploy unicodex \
    --allow-unauthenticated \
    --region $REGION \
    --image gcr.io/$PROJECT_ID/unicodex \
    --update-env-vars DATABASE_URL=berglas://${BERGLAS_BUCKET}/database_url,SECRET_KEY=berglas://${BERGLAS_BUCKET}/secret_key,GS_BUCKET_NAME=berglas://${BERGLAS_BUCKET}/media_bucket \
    --add-cloudsql-instances $DATABASE_INSTANCE
```

*Note:* Cloud Run is still in beta at the time of writing. There may be additional options you have to set here. Best bet is to choose the default that is offered. 

*Note:* We are using the fully-qualified database instance name here. Although not strictly required, as our database is in the same project and region, it helps with clarity. 

Sadly, we have a few more steps. Even though we have deployed our service, **Django won't work yet**. Because: 

* We need to tell Django where to expect our service to run, and 
* we need to initalise our database.

#### Service URL, and `ALLOWED_HOSTS` 

We can either copy the URL from the output we got from the last step, or we can get it from `gcloud`

```shell,exclude
gcloud beta run services list
```
{'fraction_non_alphanumeric': 0.08529833914291572, 'fraction_numerical': 0.0016403526758253025, 'mean_word_length': 4.102510460251046, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6482322', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1386, 'n_words': 682}