Skip to content

Commit 2c304eb

Browse files
committed
chore: bootstrap project
Initialize the project with Fastify server, CircleCI and Helm CI/CD pipeline
1 parent bdc21d8 commit 2c304eb

34 files changed

+9009
-0
lines changed

.circleci/config.yml

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
version: 2.1
2+
orbs:
3+
gcp-gcr: circleci/[email protected]
4+
helm: circleci/[email protected]
5+
gcp-cli: circleci/[email protected]
6+
7+
jobs:
8+
build:
9+
docker:
10+
- image: circleci/node:14.3.0
11+
steps:
12+
- checkout
13+
- restore_cache:
14+
key: dependency-cache-{{ checksum "package-lock.json" }}
15+
- run:
16+
name: Install dependencies
17+
command: npm install
18+
- save_cache:
19+
key: dependency-cache-{{ checksum "package-lock.json" }}
20+
paths:
21+
- ./node_modules
22+
- run:
23+
name: Test
24+
command: npm run test -- --ci --reporters=default --reporters=jest-junit
25+
environment:
26+
- JEST_JUNIT_OUTPUT_DIR: ./test-results
27+
- store_test_results:
28+
path: ./test-results
29+
- run:
30+
name: Build
31+
command: npm run build
32+
- persist_to_workspace:
33+
root: .
34+
paths:
35+
- build
36+
build_and_push_docker:
37+
machine: true
38+
steps:
39+
- checkout
40+
- attach_workspace:
41+
at: .
42+
- gcp-gcr/build-image:
43+
image: $CIRCLE_PROJECT_REPONAME
44+
tag: $CIRCLE_SHA1
45+
- gcp-gcr/gcr-auth
46+
- gcp-gcr/push-image:
47+
image: $CIRCLE_PROJECT_REPONAME
48+
tag: $CIRCLE_SHA1
49+
secrets:
50+
docker:
51+
- image: ubuntu:20.04
52+
steps:
53+
- checkout
54+
- run:
55+
name: Install git-crypt
56+
command: apt-get update -y && apt-get install -y git-crypt
57+
- run:
58+
name: Import GPG key
59+
command: echo $GPG_KEY | base64 -d > key.gpg && gpg --pinentry-mode loopback --passphrase $GPG_PASSPHRASE --allow-secret-key-import --import key.gpg && rm key.gpg
60+
- run:
61+
name: Trust ultimately key
62+
command: echo -e "5\ny\n" | gpg --command-fd 0 --expert --edit-key $GPG_FINGERPRINT trust;
63+
- run:
64+
name: Decrypt manually git-crypt key
65+
command: gpg --pinentry-mode loopback --passphrase $GPG_PASSPHRASE --decrypt ./.git-crypt/keys/default/0/$GPG_FINGERPRINT.gpg > ./.git-crypt/keys/default/0/decrypted.gpg
66+
- run:
67+
name: Unlock the secrets
68+
command: git-crypt unlock .git-crypt/keys/default/0/decrypted.gpg
69+
- persist_to_workspace:
70+
root: .
71+
paths:
72+
- helm/values
73+
deploy_helm:
74+
docker:
75+
- image: google/cloud-sdk
76+
steps:
77+
- checkout
78+
- attach_workspace:
79+
at: .
80+
- gcp-cli/initialize
81+
- run:
82+
name: Get cluster credentials
83+
command: gcloud container clusters get-credentials prod
84+
- helm/install-helm-client:
85+
version: v2.9.1
86+
- run:
87+
name: Update Helm release
88+
command: helm upgrade $CIRCLE_PROJECT_REPONAME ./helm/$CIRCLE_PROJECT_REPONAME --namespace daily -f ./helm/values/prod.yaml --set-string image.tag=$CIRCLE_SHA1 -i
89+
workflows:
90+
build:
91+
jobs:
92+
- build
93+
- build_and_push_docker:
94+
requires:
95+
- build
96+
context: GCR
97+
filters:
98+
branches:
99+
ignore: /pull\/[0-9]+/
100+
- secrets:
101+
context: GPG
102+
filters:
103+
branches:
104+
only:
105+
- master
106+
- deploy_helm:
107+
requires:
108+
- secrets
109+
- build_and_push_docker
110+
context: PROD
111+
filters:
112+
branches:
113+
only:
114+
- master
115+
- gcp-gcr/add-image-tag:
116+
requires:
117+
- build_and_push_docker
118+
context: GCR
119+
image: $CIRCLE_PROJECT_REPONAME
120+
source-tag: $CIRCLE_SHA1
121+
target-tag: latest
122+
filters:
123+
branches:
124+
only:
125+
- master

.editorconfig

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# EditorConfig helps developers define and maintain consistent
2+
# coding styles between different editors and IDEs
3+
# http://editorconfig.org
4+
5+
root = true
6+
7+
[*]
8+
9+
# Change these settings to your own preference
10+
indent_style = space
11+
indent_size = 2
12+
13+
# We recommend you to keep these unchanged
14+
end_of_line = lf
15+
charset = utf-8
16+
trim_trailing_whitespace = true
17+
insert_final_newline = true
18+
max_line_length = 80
19+
20+
[*.md]
21+
trim_trailing_whitespace = false

.env

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
PORT=6000
2+
TZ=UTC
3+
4+
ACCESS_SECRET='topsecret'

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/*

.eslintrc.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
parser: '@typescript-eslint/parser',
3+
extends: ['plugin:@typescript-eslint/recommended', 'prettier'],
4+
plugins: ['prettier'],
5+
parserOptions: {
6+
ecmaVersion: 2018,
7+
sourceType: 'module',
8+
},
9+
rules: {
10+
'prettier/prettier': 'error',
11+
},
12+
env: {
13+
node: true,
14+
},
15+
};

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
helm/values/** filter=git-crypt diff=git-crypt

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.idea/
2+
build/
3+
node_modules/
4+
.env.development
5+
.env.production

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
14.3.0

.prettierrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all"
4+
}

CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @idoshamun

Dockerfile

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM node:14.3.0-alpine
2+
3+
RUN mkdir -p /opt/app
4+
WORKDIR /opt/app
5+
6+
COPY package.json .
7+
COPY package-lock.json .
8+
9+
RUN \
10+
apk --no-cache add \
11+
libc6-compat
12+
13+
RUN npm i --only=prod
14+
15+
COPY build .
16+
17+
CMD ["npm", "run", "start"]
18+

__tests__/health.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as request from 'supertest';
2+
3+
import appFunc from '../src';
4+
import { FastifyInstance } from 'fastify';
5+
6+
describe('health check', () => {
7+
let app: FastifyInstance;
8+
9+
beforeAll(() => {
10+
app = appFunc();
11+
return app.ready();
12+
});
13+
14+
afterAll(() => app.close());
15+
16+
it('should return status code 200', () =>
17+
request(app.server)
18+
.get('/health')
19+
.expect('content-type', 'application/health+json')
20+
.expect(200, { status: 'ok' }));
21+
});

helm/daily-scraper/.helmignore

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Patterns to ignore when building packages.
2+
# This supports shell glob matching, relative path matching, and
3+
# negation (prefixed with !). Only one pattern per line.
4+
.DS_Store
5+
# Common VCS dirs
6+
.git/
7+
.gitignore
8+
.bzr/
9+
.bzrignore
10+
.hg/
11+
.hgignore
12+
.svn/
13+
# Common backup files
14+
*.swp
15+
*.bak
16+
*.tmp
17+
*~
18+
# Various IDEs
19+
.project
20+
.idea/
21+
*.tmproj

helm/daily-scraper/Chart.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: v1
2+
appVersion: "1.0"
3+
description: Daily Scraper API Helm chart
4+
name: daily-scraper
5+
version: 0.1.0
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
1. Get the application URL by running these commands:
2+
{{- if .Values.ingress.enabled }}
3+
{{- range .Values.ingress.hosts }}
4+
http{{ if $.Values.ingress.tls }}s{{ end }}://{{ . }}{{ $.Values.ingress.path }}
5+
{{- end }}
6+
{{- else if contains "NodePort" .Values.service.type }}
7+
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ template "daily-scraper.fullname" . }})
8+
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
9+
echo http://$NODE_IP:$NODE_PORT
10+
{{- else if contains "LoadBalancer" .Values.service.type }}
11+
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
12+
You can watch the status of by running 'kubectl get svc -w {{ template "daily-scraper.fullname" . }}'
13+
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "daily-scraper.fullname" . }} -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
14+
echo http://$SERVICE_IP:{{ .Values.service.port }}
15+
{{- else if contains "ClusterIP" .Values.service.type }}
16+
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app={{ template "daily-scraper.name" . }},release={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
17+
echo "Visit http://127.0.0.1:3000 to use your application"
18+
kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 3000:3000
19+
{{- end }}
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{{/* vim: set filetype=mustache: */}}
2+
{{/*
3+
Expand the name of the chart.
4+
*/}}
5+
{{- define "daily-scraper.name" -}}
6+
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
7+
{{- end -}}
8+
9+
{{/*
10+
Create a default fully qualified app name.
11+
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
12+
If release name contains chart name it will be used as a full name.
13+
*/}}
14+
{{- define "daily-scraper.fullname" -}}
15+
{{- if .Values.fullnameOverride -}}
16+
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
17+
{{- else -}}
18+
{{- $name := default .Chart.Name .Values.nameOverride -}}
19+
{{- if contains $name .Release.Name -}}
20+
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
21+
{{- else -}}
22+
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
23+
{{- end -}}
24+
{{- end -}}
25+
{{- end -}}
26+
27+
{{/*
28+
Create chart name and version as used by the chart label.
29+
*/}}
30+
{{- define "daily-scraper.chart" -}}
31+
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
32+
{{- end -}}
33+
34+
{{- define "daily-scraper.checksum" }}
35+
checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
36+
checksum/files: {{ include (print $.Template.BasePath "/secret-files.yaml") . | sha256sum }}
37+
checksum/configmap: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
38+
{{- end }}
39+
40+
{{- define "daily-scraper.config" }}
41+
{{- $fullName := include "daily-scraper.fullname" . -}}
42+
- name: NODE_ENV
43+
value: production
44+
- name: ACCESS_SECRET
45+
valueFrom:
46+
secretKeyRef:
47+
name: {{ $fullName }}
48+
key: ACCESS_SECRET
49+
- name: API_SECRET
50+
valueFrom:
51+
secretKeyRef:
52+
name: {{ $fullName }}
53+
key: API_SECRET
54+
- name: API_URL
55+
valueFrom:
56+
configMapKeyRef:
57+
name: {{ $fullName }}
58+
key: API_URL
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: {{ template "daily-scraper.fullname" . }}
5+
labels:
6+
app: {{ template "daily-scraper.name" . }}
7+
chart: {{ template "daily-scraper.chart" . }}
8+
release: {{ .Release.Name }}
9+
heritage: {{ .Release.Service }}
10+
data:
11+
API_URL: {{ .Values.appConfig.apiUrl }}
12+

0 commit comments

Comments
 (0)