Example application and CI/CD pipeline showing how to run a Node.js project on Semaphore 2.0.
The application is based on Nest.js. The code is written in TypeScript.
The application is deployed to Google Cloud Kubernetes.
-
Fork this repository and use it to create a project.
-
Create a project on Google Cloud:
semaphore-demo-nodejs-k8s
-
Create Kubernetes cluster on Google Cloud:
semaphore-demo-nodejs-k8s-server
-
Create PostgreSQL db on Google Cloud.
-
Create database
demo
and userdemouser
. -
Copy environment files and edit db hostname and db password:
$ cp ormconfig.sample.json /tmp/ormconfig.production.json $ cp sample.env /tmp/production.env
-
Upload environment files as a secret:
$ sem create secret production-env \ -f /tmp/ormconfig.production.json:/home/semaphore/ormconfig.production.json \ -f /tmp/production.env:/home/semaphore/production.env
-
Create Service Account in IAM:
- Role: project owner
- Create and download access key JSON file.
-
Upload Access Key to Semaphore as a secret:
$ sem create secret gcr-secret \ -e GCP_PROJECT_ID=semaphore-demo-nodejs-k8s \ -e GCP_PROJECT_DEFAULT_ZONE=YOUR_REGION_AND_ZONE \ -f PATH_TO_YOUR_ACCESS_KEY_FILE.json:/home/semaphore/.secrets.gcp.json
-
Push changes to get workflow started.
The CI pipeline will look like this:
The example pipeline contains 2 blocks:
- Build
- Install Dependencies: installs and caches all npm dependencies
- Test
- Lint: Runs tslint to check project files codestyle
- Unit tests: Runs Unit Tests
- E2E tests: Runs E2E tests through jest on server.
Then, if all checks are ok, we move to build pipeline. It consists of one block
- Build: build container and push it into google repository
Then, after we've built our apps we move to deploy pipeline. It also consists of one block for client and of two for server. As you can see deploy pipelines of client and server depend only on their own build step and therefore could be run in parallel.
- Deploy
- Deploy server to k8s, pdate k8s deployment using deployment config
- Tag container if all went well
This project requires a PostgreSQL database. If you don't have one you can launch a Docker container to have one.
Copy sample files:
$ cp .sample.env .env
Launch db:
$ docker-compose up
Install dependencies:
$ yarn install
Copy app and db config:
$ cp sample.env .env
$ cp ormconfig.sample.json ormconfig.json
Run migrations:
# apply migrations forward
$ yarn run migrate:up
# to revert last migration
$ yarn run migrate:revert
Running the app:
# development mode
$ yarn run start
# watch mode
$ yarn run start:dev
# production mode
$ yarn run start:prod
Run static code analysis:
$ yarn run lint
Run unit and end-to-end tests:
# unit tests
$ yarn run test
# e2e tests
$ yarn run test:e2e
# test coverage
$ yarn run test:cov
Check out .semaphore/
folder - steps described there have helpful comments to help you figure out what commands are doing.
Also check out .semaphore/secrets
folder. To configure deploy you need to create and populate all those secrets.
Copy each secret file into file without .sample
in filename and populate it. All of them have useful description comments to help you out.
Copyright (c) 2022 Rendered Text
Distributed under the MIT License. See the file LICENSE.md.