It is minimal viable REST application project deployable on Google Cloud Platform (GCP).
- Deployable on GCP, and using only GCP's Free Tier services.
- Terraform used for managing infrastructure.
- Simplified CI/CD workflows on GitHub Actions.
- Only one environment is created (no staging, prod etc.), but can be easily extended.
- Application is implemented in nest.js framework.
Cloud Run service is used as a host of application, Firestore service is used as a datastore.
There are two workflows in solution, one is responsible for building and other for deployment.
Building process is triggered by new commits pushed into repository, it:
- Executes tests and linting.
- Builds production docker image.
- Pushes image to Artifact Registry, images are tagged with SHA of commit.
Deployment process is triggered by pushing tag in form v*-dev
.
Process deploys to Cloud Run image with give commit's SHA.
Follow next steps to deploy solution on own GCP's project or run it locally.
- Create project on https://console.cloud.google.com/projectcreate it need to be unique across GCP
- Create github fork of this repository to be able use GitHub Actions on your account.
- Copy
.env.template
and rename it to.env
- Fill
TF_VAR_project_id
with id created project. - Fill
TF_VAR_github_owner
with name of your user/org on github. - Fill
TF_VAR_github_repo
with name of repository - Follow instructions on https://cloud.google.com/billing/docs/how-to/modify-project#enable_billing_for_a_project to add billing account to project, it is required to create new resources.
- Generate certificate used by terraform and during local development.
- Go to Service Accounts settings: https://console.cloud.google.com/iam-admin/serviceaccounts
- Click
Create service account
button - Fill
Name
,Account ID
and addDescription
i usedterraform
. - Click
Create and continue
Button. - Click on
Select a Role
dropdown and selectOwner
- Click
Done
- Click newly created account on list.
- Switch to "Keys" Tab.
- Click on "Add key" and select "Create new Key", pick
JSON
type on modal. - Certificate should be downloaded.
- Rename certificate to
cert.json
and move it to root directory of project.
- Use
docker-compose run --rm terraform
to enter terraform environment. - Type
terraform init
to initialize state files. - Type
terraform apply
to see plan, then typeyes
and confirm with enter to create infrastructure.
Use bellow snipet to get service URL and values used in github integration
terraform output app_url
terraform output wif_provider_id
terraform output wif_service_account_email
To configure authentication of GitHub Actions in GCP two secrets needs to be set in Actions Secrets: https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository
Below secrets are needed:
WIF_PROVIDER
- value of terraform outputwif_provider_id
from previous sectionWIF_SERVICE_ACCOUNT
value of terraform outputwif_service_account_email
from previous section
Before running app localy you need to Setup Project.
Use command:
docker-compose up app
Application will be available on localhost:8080
Before running app localy you need to Setup Project.
Navigate to app/
directory.
cd app/
npm install
npm run start:dev
It will start application on port 3000
.
To run tests use:
npm run test
To execute linting:
npm run lint