From 5b2239afd9bbba15fbf1b2a64716f7cf0880f2b5 Mon Sep 17 00:00:00 2001 From: Zhenglai Zhang Date: Sun, 19 Jan 2020 17:02:22 +0800 Subject: [PATCH] Improve dockerfile (#61) * improve docker file * merge multiple RUNs into single one to reduce image size * fix nodegit getReferences issue * use multi stage to optimzie docker * move swagger-ui-express from dev to prod --- .dockerignore | 6 ++- Dockerfile | 40 ++++++++++++---- package-lock.json | 67 ++++++++++++++++++++------- package.json | 6 +-- src/services/branch/branch.service.ts | 4 +- 5 files changed, 90 insertions(+), 33 deletions(-) diff --git a/.dockerignore b/.dockerignore index e7cfeec..5252360 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1 +1,5 @@ -buildcache \ No newline at end of file +tmp +bin +buildcache +node_modules +.git \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index cb56d87..596a40e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,36 @@ -FROM node:10-alpine +# Base Stage +FROM alpine:3.11 as base + +RUN mkdir /app +WORKDIR /app + +ENV APK_ADD="nodejs-npm krb5-libs" +ENV APK_ADD_BUILD="nodejs-npm python krb5-dev curl-dev build-base libssh2-dev" + +RUN apk update && apk upgrade && apk add --no-cache ${APK_ADD} + + +# Build Stage +FROM base as builder +RUN apk add --no-cache ${APK_ADD_BUILD} +COPY package.json package-lock.json .prettierrc.yml ./ +RUN BUILD_ONLY=true JOBS=`nproc` npm ci + +COPY . . +RUN npm run build +ARG SKIP_TEST +RUN if [ -z "${SKIP_TEST}" ]; then npm run test; fi +RUN npm prune --production + + +# Prod Stage +FROM base as prod + +COPY package.json package-lock.json ./ +COPY --from=builder /app/bin ./bin +COPY --from=builder /app/node_modules ./node_modules EXPOSE 3009 -COPY ./ ./ - -# Remove .npmrc in case it was injected at build time -RUN rm -f .npmrc - - -# Set environment to production ENV NODE_ENV=production - CMD npm run start:prod \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 77256df..592362f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "git-rest-api", - "version": "0.3.2", + "version": "0.3.3", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -641,9 +641,9 @@ } }, "@types/nodegit": { - "version": "0.24.8", - "resolved": "https://registry.npmjs.org/@types/nodegit/-/nodegit-0.24.8.tgz", - "integrity": "sha512-MgeLmeBsi8sOn0IzQuw+Il4Cw71MLUNm4du44ewd5riLXbni0DZLjWx7PJO2mVegsnv0WNosXrpifQnWJjjNkg==", + "version": "0.26.1", + "resolved": "https://registry.npmjs.org/@types/nodegit/-/nodegit-0.26.1.tgz", + "integrity": "sha512-p9qGQImDs5QYCX44Njq/7chqW6DDq5y/Qb8H1yWhNLN6hCOBRiY8EIOmuPDqBLgrJqSM13dlbRDuyeYlEaAUlg==", "dev": true, "requires": { "@types/node": "*" @@ -6240,20 +6240,53 @@ } }, "nodegit": { - "version": "0.24.3", - "resolved": "https://registry.npmjs.org/nodegit/-/nodegit-0.24.3.tgz", - "integrity": "sha512-F9XpC5xzpoBgJXmdIRaD2z5DVG+iMttxFlzyCqmOu3y5y/DFuxBpzQtRND75oUOxJZh8sSlReVnXFV3PEyzvIw==", + "version": "0.26.4", + "resolved": "https://registry.npmjs.org/nodegit/-/nodegit-0.26.4.tgz", + "integrity": "sha512-HbGdk6zqI+3u3HhRLoOigut/Ns0Bm04WL0JPwsxJKalvpihOevrdTj7461wofANu/Vn5sdaq//VpeHLZTJQrlQ==", "requires": { "fs-extra": "^7.0.0", "json5": "^2.1.0", - "lodash": "^4.17.11", - "nan": "^2.11.1", + "lodash": "^4.17.14", + "nan": "^2.14.0", "node-gyp": "^4.0.0", - "node-pre-gyp": "^0.11.0", + "node-pre-gyp": "^0.13.0", "promisify-node": "~0.3.0", "ramda": "^0.25.0", "request-promise-native": "^1.0.5", "tar-fs": "^1.16.3" + }, + "dependencies": { + "nan": { + "version": "2.14.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.14.0.tgz", + "integrity": "sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==" + }, + "node-pre-gyp": { + "version": "0.13.0", + "resolved": "https://registry.npmjs.org/node-pre-gyp/-/node-pre-gyp-0.13.0.tgz", + "integrity": "sha512-Md1D3xnEne8b/HGVQkZZwV27WUi1ZRuZBij24TNaZwUPU3ZAFtvT6xxJGaUVillfmMKnn5oD1HoGsp2Ftik7SQ==", + "requires": { + "detect-libc": "^1.0.2", + "mkdirp": "^0.5.1", + "needle": "^2.2.1", + "nopt": "^4.0.1", + "npm-packlist": "^1.1.6", + "npmlog": "^4.0.2", + "rc": "^1.2.7", + "rimraf": "^2.6.1", + "semver": "^5.3.0", + "tar": "^4" + } + }, + "nopt": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/nopt/-/nopt-4.0.1.tgz", + "integrity": "sha1-0NRoWv1UFRk8jHUFYC0NF81kR00=", + "requires": { + "abbrev": "1", + "osenv": "^0.1.4" + } + } } }, "nodegit-promise": { @@ -7661,16 +7694,14 @@ } }, "swagger-ui-dist": { - "version": "3.22.3", - "resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-3.22.3.tgz", - "integrity": "sha512-tmjAsqT43pqg5UoiQ2805c+juX0ASSoI/Ash/0c19jjAOFtTfE93ZrzmFd9hjqVgre935CYeXT0uaku42Lu8xg==", - "dev": true + "version": "3.25.0", + "resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-3.25.0.tgz", + "integrity": "sha512-vwvJPPbdooTvDwLGzjIXinOXizDJJ6U1hxnJL3y6U3aL1d2MSXDmKg2139XaLBhsVZdnQJV2bOkX4reB+RXamg==" }, "swagger-ui-express": { - "version": "4.0.6", - "resolved": "https://registry.npmjs.org/swagger-ui-express/-/swagger-ui-express-4.0.6.tgz", - "integrity": "sha512-7YkBfVWRYjvnGITs7vygM8VNF91iUwX8ReHQaTD9I7q8Ky8SYXZ81gyFc+kovE34iSbCC1yW+n2oII3b3uSxXQ==", - "dev": true, + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/swagger-ui-express/-/swagger-ui-express-4.1.2.tgz", + "integrity": "sha512-bVT16qj6WdNlEKFkSLOoTeGuqEm2lfOFRq6mVHAx+viA/ikORE+n4CS3WpVcYmQzM4HE6+DUFgAWcMRBJNpjcw==", "requires": { "swagger-ui-dist": "^3.18.1" } diff --git a/package.json b/package.json index b9aaf57..2b0824d 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "@types/helmet": "0.0.43", "@types/jest": "^24.0.13", "@types/node-fetch": "^2.3.5", - "@types/nodegit": "^0.24.8", + "@types/nodegit": "^0.26.1", "@types/rimraf": "^2.0.2", "@types/triple-beam": "^1.3.0", "@types/uuid": "^3.4.4", @@ -49,7 +49,6 @@ "jest": "^24.8.0", "jest-junit": "^6.4.0", "prettier": "^1.18.2", - "swagger-ui-express": "^4.0.6", "ts-jest": "^24.0.2", "tslint": "^5.17.0", "tslint-config-prettier": "^1.18.0", @@ -73,11 +72,12 @@ "helmet": "^3.18.0", "hot-shots": "^6.3.0", "node-fetch": "^2.6.0", - "nodegit": "^0.24.3", + "nodegit": "^0.26.4", "reflect-metadata": "^0.1.13", "rimraf": "^2.6.3", "rxjs": "^6.5.2", "sqlite3": "^4.0.9", + "swagger-ui-express": "^4.1.2", "triple-beam": "^1.3.0", "typeorm": "^0.2.18", "uuid": "^3.3.2", diff --git a/src/services/branch/branch.service.ts b/src/services/branch/branch.service.ts index d9e7f68..6e05cc5 100644 --- a/src/services/branch/branch.service.ts +++ b/src/services/branch/branch.service.ts @@ -1,5 +1,5 @@ import { Injectable } from "@nestjs/common"; -import { Reference, Repository } from "nodegit"; +import { Repository } from "nodegit"; import { GitBranch } from "../../dtos"; import { GitBaseOptions, RepoService } from "../repo"; @@ -19,7 +19,7 @@ export class BranchService { } public async listGitBranches(repo: Repository): Promise { - const refs = await repo.getReferences(Reference.TYPE.LISTALL); + const refs = await repo.getReferences(); const branches = refs.filter(x => x.isRemote()); return Promise.all(