From 0d21dc8bf8a127fe46f1994c3bf1b9c002b9a501 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Thu, 6 Jun 2019 01:15:10 +0300 Subject: [PATCH] Add Docker configuration. Implemented graceful stopping. --- .dockerignore | 2 ++ .travis.yml | 29 +++++++++++++++++++++++++---- Dockerfile | 35 +++++++++++++++++++++++++++++++++++ package.json | 1 + src/server.tsx | 12 +++++++----- yarn.lock | 22 +++++++++++++++++++--- 6 files changed, 89 insertions(+), 12 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..f06235c --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +node_modules +dist diff --git a/.travis.yml b/.travis.yml index 8257451..c5d668c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,28 @@ cache: directories: - node_modules -script: - - yarn lint - - yarn tsc - - yarn build:quiet +stages: + - test + - name: deploy + if: branch = master + +jobs: + include: + - stage: test + script: + - yarn lint + - yarn tsc + - yarn build:quiet + - stage: deploy + services: + - docker + before_script: + - docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD" + script: + - export DOCKER_TAG="${TRAVIS_TAG:-dev}" + - > + docker build + --target app + -t elyby/emails-renderer:$DOCKER_TAG + . + - docker push elyby/emails-renderer:$DOCKER_TAG diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..31ec45d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,35 @@ +FROM node:12.4.0-alpine AS build + +RUN mkdir -p /build +WORKDIR /build + +# Dependencies to build node-canvas +RUN apk add --no-cache build-base g++ python cairo-dev jpeg-dev pango-dev giflib-dev + +COPY scripts /build/scripts +COPY webpack-utils /build/webpack-utils +COPY package.json /build/ +COPY yarn.lock /build/ + +RUN yarn install + +COPY . /build + +RUN yarn build:quiet \ + && rm dist/*.html \ + && rm dist/stats.json + +RUN yarn install --prod \ + && rm node_modules/.yarn-integrity + + +FROM node:12.4.0-alpine AS app + +RUN mkdir -p /usr/src/app +WORKDIR /usr/src/app +EXPOSE 3000 + +COPY --from=build /build/node_modules /usr/src/app/node_modules +COPY --from=build /build/dist /usr/src/app/ + +CMD ["node", "/usr/src/app/app.js"] diff --git a/package.json b/package.json index 16da6ae..dda781a 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ }, "dependencies": { "fastify": "^2.4.1", + "fastify-graceful-shutdown": "^2.0.1", "fastify-static": "^2.4.0", "react": "^16.8.4", "react-dom": "^16.8.4", diff --git a/src/server.tsx b/src/server.tsx index 63d3062..9e4de80 100644 --- a/src/server.tsx +++ b/src/server.tsx @@ -1,20 +1,21 @@ /* eslint-env node */ /* eslint-disable spaced-comment */ import React from 'react'; +import ReactDOMServer from 'react-dom/server'; import path from 'path'; import createFastify from 'fastify'; +// @ts-ignore +import fastifyGracefulShutdown from 'fastify-graceful-shutdown'; import fastifyStatic from 'fastify-static'; -import { AddressInfo } from 'net'; -import ReactDOMServer from 'react-dom/server'; import { Html } from 'components'; import App, { Params } from 'App'; const fastify = createFastify({ logger: { - level: 'warn', + level: 'info', }, }); @@ -27,6 +28,8 @@ interface GetTemplateUrlParams { template: string; } +fastify.register(fastifyGracefulShutdown); + fastify.register(fastifyStatic, { root: path.join(__dirname, 'assets'), prefix: '/assets/', @@ -72,8 +75,7 @@ fastify.get('/templates/:locale/:t (async () => { try { - await fastify.listen(3000); - fastify.log.info(`server listening on ${(fastify.server.address() as AddressInfo)!.port}`); + await fastify.listen(3000, '0.0.0.0'); } catch (err) { fastify.log.error(err); process.exit(1); diff --git a/yarn.lock b/yarn.lock index dd63353..9655352 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2731,11 +2731,11 @@ extend@~3.0.2: "extended-translations-loader@file:webpack-utils/extended-translations-loader": version "1.0.0" dependencies: - "@lesechos/image-size-loader" "file:../../.cache/yarn/v4/npm-extended-translations-loader-1.0.0-fe1c90c6-5a79-4e5e-8995-7c33cb1db7d9-1559762166005/node_modules/image-size-loader" + "@lesechos/image-size-loader" "file:../../.cache/yarn/v4/npm-extended-translations-loader-1.0.0-72c3a128-fbb9-4e3d-9a24-9f2972009e1b-1559771604821/node_modules/image-size-loader" glob "^7.1.4" loader-utils "^1.2.3" qs "^6.0.0" - text2png-loader "file:../../.cache/yarn/v4/npm-extended-translations-loader-1.0.0-fe1c90c6-5a79-4e5e-8995-7c33cb1db7d9-1559762166005/node_modules/text2png-loader" + text2png-loader "file:../../.cache/yarn/v4/npm-extended-translations-loader-1.0.0-72c3a128-fbb9-4e3d-9a24-9f2972009e1b-1559771604821/node_modules/text2png-loader" tmp "^0.0.33" external-editor@^3.0.3: @@ -2814,6 +2814,14 @@ fast-safe-stringify@^2.0.6: resolved "https://registry.yarnpkg.com/fast-safe-stringify/-/fast-safe-stringify-2.0.6.tgz#04b26106cc56681f51a044cfc0d76cf0008ac2c2" integrity sha512-q8BZ89jjc+mz08rSxROs8VsrBBcn1SIw1kq9NjolL509tkABRk9io01RAjSaEv1Xb2uFLt8VtRiZbGp5H8iDtg== +fastify-graceful-shutdown@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/fastify-graceful-shutdown/-/fastify-graceful-shutdown-2.0.1.tgz#2ed228dc8eb8b225cdff5ed5b714264462e64d38" + integrity sha512-7G+K5Iqo1VVPsykkn3yuXnZvwzAmdIOQWvHzfGZ7yJQW3y7DnHtbiVzLdYBMvbNHQf9nt4LN483heTzGaqz8LA== + dependencies: + fastify-plugin "^1.5.0" + fastparallel "^2.3.0" + fastify-plugin@^1.5.0: version "1.6.0" resolved "https://registry.yarnpkg.com/fastify-plugin/-/fastify-plugin-1.6.0.tgz#c8198b08608f20c502b5dad26b36e9ae27206d7c" @@ -2851,6 +2859,14 @@ fastify@^2.4.1: secure-json-parse "^1.0.0" tiny-lru "^6.0.1" +fastparallel@^2.3.0: + version "2.3.0" + resolved "https://registry.yarnpkg.com/fastparallel/-/fastparallel-2.3.0.tgz#1e709bfb6a03993f3857e3ce7f01311ce7602613" + integrity sha1-HnCb+2oDmT84V+POfwExHOdgJhM= + dependencies: + reusify "^1.0.0" + xtend "^4.0.1" + fastq@^1.6.0: version "1.6.0" resolved "https://registry.yarnpkg.com/fastq/-/fastq-1.6.0.tgz#4ec8a38f4ac25f21492673adb7eae9cfef47d1c2" @@ -6803,7 +6819,7 @@ ws@^6.0.0: dependencies: async-limiter "~1.0.0" -xtend@^4.0.0, xtend@~4.0.1: +xtend@^4.0.0, xtend@^4.0.1, xtend@~4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af" integrity sha1-pcbVMr5lbiPbgg77lDofBJmNY68=