Skip to content

Commit bd5f506

Browse files
authored
Feature/ci enhancement (#15)
* Separate postgres and redis volume for dev and test. Run container as node user. Add cors support Run as root when setting up image, run as node before starting app. Set image build stage to release. Add files to dockerignore Build image Copy files from host to image Set image owner to node * Run as user node. Add prettier ignore Do not use cache when running ci test Do not run nyc in ci test Remove --no-cache * Copy from base image in test image. Run test with nyc Copy from base image in test image. Run test with nyc Do not run nyc in test due to mkdir permission error
1 parent b890b9b commit bd5f506

13 files changed

Lines changed: 109 additions & 32 deletions

File tree

.dockerignore

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,12 @@ Dockerfile
55
dist
66
node_modules
77
npm-debug.log
8-
README.md
8+
README.md
9+
loadtest
10+
coverage
11+
.nyc_output
12+
.github
13+
images
14+
.env
15+
.env.test
16+
debug.log

.github/workflows/build_and_push_image.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- uses: actions/checkout@v3
1616
- name: Build docker image
1717
id: build_image
18-
run: docker build -t "$IMAGE_REGISTRY/$APP_NAME:$GITHUB_SHA" .
18+
run: docker build -t "$IMAGE_REGISTRY/$APP_NAME:$GITHUB_SHA" --target release .
1919
- name: Log in to github container registry
2020
id: login_registry
2121
run: echo $REGISTRY_PASSWORD | docker login ghcr.io -u $REGISTRY_USER --password-stdin

.prettierignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.nyc_output
2+
dist
3+
coverage
4+
node_modules
5+
loadtest

Dockerfile

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
1-
# 1. --- Base ---
1+
# --- Base ---
22
FROM node:16-stretch-slim AS base
33

4-
# install dependencies first, in a different location for easier app bind mounting for local development
5-
# due to default /opt permissions we have to create the dir with root and change perms
64
RUN mkdir -p /opt/node_app && chown -R node:node /opt/node_app
75
WORKDIR /opt/node_app
8-
9-
# the official node image provides an unprivileged user as a security best practice
10-
# but we have to manually enable it. We put it here so npm installs dependencies as the same
11-
# user who runs the app.
12-
# https://github.com/nodejs/docker-node/blob/master/docs/BestPractices.md#non-root-user
136
COPY ./package*.json ./
147

15-
16-
# 2. --- dev ---
8+
# --- dev ---
179
FROM base AS dev
10+
RUN npm install
11+
1812
ENV NODE_ENV development
1913
ENV PORT 3000
20-
21-
RUN npm install
2214
ENV PATH /opt/node_app/node_modules/.bin:$PATH
2315

2416
# copy in our source code last, as it changes the most
2517
COPY --chown=node:node . .
26-
RUN npm run build
18+
USER node
2719
CMD [ "npm", "run", "debug" ]
2820

21+
# --- test ---
22+
FROM base as test
23+
RUN npm ci
2924

30-
# 3. --- test ---
31-
FROM dev as test
3225
ENV NODE_ENV test
3326
ENV PORT 3000
34-
CMD ["npm", "test"]
35-
36-
# 4. --- Release ---
37-
FROM base AS release
38-
ENV NODE_ENV production
39-
ENV PORT 3000
4027
ENV PATH /opt/node_app/node_modules/.bin:$PATH
4128

42-
COPY --from=dev /opt/node_app/dist ./dist
43-
RUN npm ci --only=production
29+
COPY --chown=node:node . .
4430
USER node
31+
CMD ["npm", "run", "test:ci"]
32+
33+
## --- Build ---
34+
FROM dev as build
35+
RUN npm run build
36+
37+
# --- Release ---
38+
FROM build AS release
39+
COPY --from=build /opt/node_app/dist ./dist
40+
RUN npm ci --only=production
41+
42+
ENV PATH /opt/node_app/node_modules/.bin:$PATH
43+
ENV NODE_ENV production
44+
ENV PORT 3000
4545
CMD [ "npm", "run", "start" ]

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,5 @@ Each key contains 6 characters.
157157
* URL shortening: Check if URL is stored in the database. If it is, return the short url
158158
* Key generation: do not check against the database for existing short urls because there can still be a conflict due to concurrency. Let the database handle the conflict
159159
* Handle expired keys
160-
* load test with multiple instances of API service and database
160+
* Collect metric
161+
* Centralised log collection

docker-compose.dev.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ services:
3030
- POSTGRES_DB=url_shortener
3131
ports:
3232
- "5432:5432"
33+
volumes:
34+
- postgres-volume-dev:/var/lib/postgresql/data
3335

3436
redis:
3537
ports:
36-
- "6379:6379"
38+
- "6379:6379"
39+
volumes:
40+
- redis-volume-dev:/data
41+
42+
volumes:
43+
postgres-volume-dev:
44+
redis-volume-dev:

docker-compose.test.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ services:
77
target: test
88
dockerfile: Dockerfile
99
image: url-shortener:test
10+
command: npm run test:ci
1011
environment:
1112
- NODE_ENV=test
1213
- POSTGRES_HOST=postgres
@@ -29,7 +30,15 @@ services:
2930
- POSTGRES_DB=url_shortener_test
3031
ports:
3132
- "5432:5432"
33+
volumes:
34+
- postgres-volume-test:/var/lib/postgresql/data
3235

3336
redis:
3437
ports:
35-
- "6379:6379"
38+
- "6379:6379"
39+
volumes:
40+
- redis-volume-test:/data
41+
42+
volumes:
43+
postgres-volume-test:
44+
redis-volume-test:

docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ services:
1414
postgres:
1515
image: postgres:13.1
1616
volumes:
17-
- postgres-volume:/var/lib/postgresql/data
1817
- ./src/db/postgres/schema.sql:/docker-entrypoint-initdb.d/schema.sql
1918

2019
redis:

package-lock.json

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@
3030
"start": "npm run serve",
3131
"build": "npm run build-ts",
3232
"serve": "node dist/bin/www.js",
33+
"test:ci": "npm run test-unit-ci && npm run test-integration-ci",
34+
"test-integration-ci": "ts-mocha test/dbhelper.ts test/integration/index.ts",
35+
"test-unit-ci": "ts-mocha test/unit/index.ts",
3336
"test": "npm run test-unit && npm run test-integration",
3437
"test-integration": "nyc ts-mocha test/dbhelper.ts test/integration/index.ts",
3538
"test-unit": "nyc ts-mocha test/unit/index.ts",
36-
"build-ts": "tsc",
39+
"build-ts": "tsc -p .",
3740
"watch-ts": "tsc -w",
3841
"prettier:fix": "prettier --write \"**/*.+(ts|js|json)\"",
3942
"prettier:check": "prettier --check \"**/*.+(ts|js|json)\"",
@@ -51,6 +54,7 @@
5154
"dependencies": {
5255
"bluebird": "^3.7.2",
5356
"body-parser": "^1.19.0",
57+
"cors": "^2.8.5",
5458
"dotenv": "^8.2.0",
5559
"express": "^4.17.1",
5660
"express-joi-validation": "^5.0.0",

0 commit comments

Comments
 (0)