Skip to content

Commit e11ddc9

Browse files
committed
Outline the GCP infra setup and authentication from github actions in the readme
1 parent 28f9929 commit e11ddc9

File tree

1 file changed

+85
-8
lines changed

1 file changed

+85
-8
lines changed

README.md

Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,25 +155,102 @@ The service now runs on port 3000 and listens to requests
155155

156156
<p align="right">(<a href="#readme-top">back to top</a>)</p>
157157

158+
## Google Cloud Platform (GCP) Infrastructure
159+
160+
We use [Cloud Run](https://cloud.google.com/run). It is a serverless platform for deploying containerized applications.
161+
Besides Cloud Run, there are a few other supporting GCP services that we use, such as Cloud Build and Artifact Registry.
162+
163+
This section briefly describes how to setup the infrastructure resources using the `gcloud` CLI. For a production-ready setup,
164+
we strongly recommend not using the CLI, but rather adopting an Infrastructure-as-Code approach, e.g. using [Terraform](https://www.terraform.io/). For the scope of this demo application, the CLI is sufficient.
165+
166+
### Infrastructure Setup Prerequisites
167+
168+
1. Open an account on GCP.
169+
2. Create a new project on GCP.
170+
3. Install the [gcloud CLI](https://cloud.google.com/sdk/docs/install).
171+
4. Authenticate to GCP using: `gcloud auth login`.
172+
173+
### Setup Steps
174+
175+
Start by setting the project ID:
176+
177+
`gcloud config set project <GCP_PROJECT_ID>`
178+
179+
Next, we'll enable a few services:
180+
181+
`gcloud services enable cloudbuild.googleapis.com artifactregistry.googleapis.com run.googleapis.com`
182+
183+
* `cloudbuild.googleapis.com` -> [Cloud Build](https://cloud.google.com/build), used to build the container images
184+
* `artifactregistry.googleapis.com` -> [Artifact Registry](https://cloud.google.com/artifact-registry), used to store the container images
185+
* `run.googleapis.com` -> [Cloud Run](https://cloud.google.com/run), used to deploy the container images
186+
187+
Once that is done, we'll create an artifact repository to store the container images:
188+
189+
`gcloud artifacts repositories create --location <GCP_REGION> cloud-run-builds --repository-format docker`
190+
191+
Don't forget to replace the `<GCP_REGION>` placeholder with a region of your choice, e.g. europe-west1.
192+
193+
Now, we create a service account for GitHub Actions to use:
194+
195+
`gcloud iam service-accounts create gh-actions`
196+
197+
We'll use this service account in the next section to authenticate to GCP from GitHub Actions. Let's now grant a few
198+
permissions to the service account:
199+
200+
```shell
201+
gcloud projects add-iam-policy-binding <GCP_PROJECT_ID> \
202+
--member=serviceAccount:gh-actions@<GCP_PROJECT_ID>.iam.gserviceaccount.com \
203+
--role=roles/cloudbuild.builds.editor
204+
205+
gcloud projects add-iam-policy-binding <GCP_PROJECT_ID> \
206+
--member=serviceAccount:gh-actions@<GCP_PROJECT_ID>.iam.gserviceaccount.com \
207+
--role=roles/cloudbuild.builds.viewer
208+
209+
gcloud projects add-iam-policy-binding <GCP_PROJECT_ID> \
210+
--member=serviceAccount:gh-actions@<GCP_PROJECT_ID>.iam.gserviceaccount.com \
211+
--role=roles/storage.admin
212+
213+
gcloud iam service-accounts add-iam-policy-binding \
214+
<GCP_PROJECT_NUMBER>[email protected] \
215+
--member="serviceAccount:gh-actions@<GCP_PROJECT_ID>.iam.gserviceaccount.com" \
216+
--role="roles/iam.serviceAccountUser"
217+
```
218+
219+
As a rule of thumb, try to always keep the permissions as granular as possible and follow the least-privilege principle.
220+
221+
> Note: `<GCP_PROJECT_NUMBER>[email protected]` refers to the Compute Engine default service account.
222+
> You can find the project number in the GCP console, or by running `gcloud projects describe <GCP_PROJECT_ID> --format='value(projectNumber)'`.
223+
158224
## CI/CD Pipelines Using GitHub Actions
159225

160-
## Authenticating to GCP using a Service Account Key
226+
[GitHub Actions](https://github.com/features/actions) is a reasonably good CI/CD platform. We use it to build and deploy
227+
this demo application to GCP. The [.github/workflows](.github/workflows) directory contains the definitions for the CI and CD pipelines.
228+
229+
### Authenticating to GCP using a Service Account Key
161230

162-
TODO: add details
231+
We use the [auth](https://github.com/google-github-actions/auth/tree/v1/) action to authenticate to GCP using a service account key.
232+
The service account key is a long-lived credential, thus it's not ideal from a security perspective.
233+
For a production-ready setup, we strongly recommend using [Workload Identity Federation](https://cloud.google.com/iam/docs/workload-identity-federation) instead.
163234

164-
### Populating Secrets
235+
Generate the service account key using the following command:
165236

166-
The GCP service account key need to be stored as a secret in the GitHub repo. Alongside, we store a few other GCP-related
237+
`gcloud iam service-accounts keys create gh-actions-key.json --iam-account gh-actions@<GCP_PROJECT_ID>.iam.gserviceaccount.com`
238+
239+
### Populating Secrets in GitHub
240+
241+
The GCP service account key needs to be stored as a secret in the GitHub repo. Alongside, we store a few other GCP-related
167242
configuration values, such as project ID and region. Secrets can be accessed in the GitHub Actions workflows.
168243

169-
We advise using `gh` to create the secrets:
244+
We advise using [gh](https://cli.github.com/) to create the secrets:
170245

171246
```shell
172-
gh secret set GCP_PROJECT_ID --body '<gcp_project_id>'
173-
gh secret set GCP_REGION --body '<gcp_region>'
174-
gh secret set GCP_SA_KEY --body $(cat <gpc_service_account_key.json> | base64)
247+
gh secret set GCP_PROJECT_ID --body '<GCP_PROJECT_ID>'
248+
gh secret set GCP_REGION --body '<GCP_REGION>' # e.g. europe-west1
249+
gh secret set GCP_SA_KEY --body $(cat <GPC_SERVICE_ACCOUNT_KEY.json> | base64)
175250
```
176251

252+
The CI and CD workflows reference these secrets and will now be able to authenticate to GCP.
253+
177254
<!-- MARKDOWN LINKS & IMAGES -->
178255
<!-- https://www.markdownguide.org/basic-syntax/#reference-style-links -->
179256
[contributors-shield]: https://img.shields.io/github/contributors/netlight/my-finance-pal-backend.svg?style=for-the-badge

0 commit comments

Comments
 (0)