NODE.
JS
CI/CD
PIPELINES
Mohammad-Ali A’râbi
Docker All-Hands #5
01 02
TABLE OF
INTRO INGREDIENTS
CONTENTS We’re going to cook Node.js, Docker,
a Node.js exec and 2 teaspoons
Docker image database mocks
03 04 05
BUILD TEST PUBLISH
Let the TS dough Test away: the It’s pu(bli)sh:
rest and ferment dough, the cook, You publish by
into JS the end result pushing
01
INTRODUCTION
FROM node:17-alpine
LABEL maintainer “aerabi”
MOHAMMAD-ALI
A’RÂBI
Software Engineer, Mathematician,
Iranian, based in Germany:
● Writing code since 2004
● Docker user since 2015
● Docker Community Leader of
Switzerland
Tweet me @MohammadAliEN
GitHub, Medium, LinkedIn @aerabi
NODE.JS
A backend
project written
BUILD NODE.JS
in TypeScript
FOR PRODUCTION
DOCKER
An executable
Node.js backend
application
Docker image
02
INGREDIENTS
Node.js, GitLab-CI,
Gorgonzola, and Pesto
TECHNOLOGIES
1. Node.js
2. TypeScript
3. Express.js (a web framework)
4. MongoDB (a database)
5. Jest/Jasmine (a testing framework)
6. Docker-Compose
7. GitLab-CI
03 BUILD
Trim your beard while
the project is building
PRE-TRANSPILE
You want to rebuild the
routes?
BUILD TRANSPILE
Make it JS, minify,
PHASES uglify, binify
DOCKER IMAGE
Put minified uglified
binified into Alpine
BUILD STEPS
Create package.json commands:
- build:pre
- build:transpile
- build:prod
- build:docker
BUILD COMMANDS
package.json
{
“scripts”: {
“build:pre”: “npm run build:routes && npm run build:swagger”,
“build:transpile”: “tsc -p src”,
“build:prod”: “npm run build:webpack && npm run build:pkg”,
“build:docker”: “docker build -t aerabi/website-node .”,
“publish:docker”: “docker push aerabi/website-node”
}
}
BUILD DOCKERFILE
FROM node:16 as builder
WORKDIR /usr/src/app
COPY . /usr/src/app/
RUN npm install
RUN npm run build:pre &&
npm run build:transpile &&
npm run build:prod
RUN chmod +x bin/website-node
FROM aerabi/base:latest as runner
COPY --from=builder /usr/src/app/bin/website-node /opt/website-node
CMD ["/opt/website-node"]
CI/CD: DOCKER BUILD JOB
# .gitlab-ci.yml
build:docker:
stage: build
image: aerabi/docker:19.03.12-node
services:
- docker:19.03.12-dind
before_script:
- docker info
- docker login -u $DOCKER_USER -p $DOCKER_PASSWORD
script:
- npm run build:docker
- npm run publish:docker
when: manual
04
TESTING
You have an extra space
in your line
INTEGRATION TEST
Docker-Compose
NODE.JS
MONGODB REPLICA SET
BUILD COMMANDS
package.json
{
“scripts”: {
“build:pre”: “npm run build:routes && npm run build:swagger”,
“build:transpile”: “tsc -p src”,
“build:prod”: “npm run build:webpack && npm run build:pkg”,
“build:docker”: “docker build -t aerabi/website-node .”,
“test:int”: “docker-compose up --exit-code-from node node”,
“publish:docker”: “docker push aerabi/website-node”
}
}
PIPELINE
BUILD:PRE BUILD:DOCKER
Integration test Create the Docker
needs it image
TEST:INT DEPLOY
Docker-Compose in Let the clients
Docker enjoy it
OTHER TESTS
- code format
- commit lint
- test coverage
- dependency check
- package lock check
ALMOST
THERE
PUBLISH
Give all your bugs to
the end user
05
PUBLISH, CHECK, ROLLOUT
PUBLISH PREVIEW
Push the Docker Create a preview
image to registry environment
CHECK ROLLOUT
Do more manual or Rollout or
automated tests deploy the image
TIPS SAVE TIME TEST AWAY
You can share The more tests
artifacts the merrier
FAIL FAST PARALLELIZE
Optimize the Your jobs can run
pipeline DAG in parallel
KEEP SHORT SPLIT
Keep it under Put your CI conf
10 minutes in multiple files
REPO
The slides + more material will be
available on
github.com/aerabi/docker-2022
gitlab.com/aerabi/docker-2022
ALTERNATIVE RESOURCES
RESOURCES
GITLAB-CI
● Docker-in-Docker
● Environments
DOCKER
● Multi-stage builds
MORE TALKS BY DOCKER COMMUNITY
● Docker image optimization workshop
● Hackdockerfest: Security
THANKS!
DO YOU HAVE ANY
QUESTIONS?
mohammad-ali@aerabi.com
Twitter @MohammadAliEN
GitHub, LinkedIn @aerabi
CREDITS: This presentation template was created
by Slidesgo, including icons by Flaticon and
infographics & images by Freepik