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.templateand rename it to.env - Fill
TF_VAR_project_idwith id created project. - Fill
TF_VAR_github_ownerwith name of your user/org on github. - Fill
TF_VAR_github_repowith 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 accountbutton - Fill
Name,Account IDand addDescriptioni usedterraform. - Click
Create and continueButton. - Click on
Select a Roledropdown and selectOwner - Click
Done - Click newly created account on list.
- Switch to "Keys" Tab.
- Click on "Add key" and select "Create new Key", pick
JSONtype on modal. - Certificate should be downloaded.
- Rename certificate to
cert.jsonand move it to root directory of project.
- Use
docker-compose run --rm terraformto enter terraform environment. - Type
terraform initto initialize state files. - Type
terraform applyto see plan, then typeyesand 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_idfrom previous sectionWIF_SERVICE_ACCOUNTvalue of terraform outputwif_service_account_emailfrom 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