background

There is no CI/CD in our project team, because the deployment is in K8S, so the CD process is to log in to the master node to manually modify the YML file, and then apply to restart the service. So all you have to do is ci.

Conventional understanding

Start a Node image on the CI server, git pull the code down, then NPM I, then NPM run build, then put the Dist code outside the package container, and then package it into the deployment image according to the Dockerfile outside.

Pit 1, the mirror image can be split

I understood that I wanted to build and deploy in one image, so I wanted to have an integrated image with node and Nginx images, but it was not easy to install either Nginx in node or Node in Nginx.

Package when you can, deploy when you can.

Two mirror images.

Pit 2. Node-sass is not installed in the Node image

Failed to install Node-sass in node: lastest.

It was later changed to Node :14.10.0 and barely installed.

Node-sass was always a sinkhole, but it will be in the framework code again.

In addition, the general operation of NPM:

Install the NPM

NPM config set sass_binary_site=npm.taobao.org/mirrors/nod…

Pit 3, NPM I in node image is too slow

This could be a network problem, and switching sources can’t fix it. The solution I came up with was to package node_modules in an image.

Each time only the recent code copy into the folder, do not re NPM I

Pit 4, hidden file without copy

Both CP and Docker CP are the same.

-r copies all directories, but does not copy hidden files, such as. Env.production configuration files.

This leads to build errors.

The solution is to use the parameter -a

Or several hidden files one at a time.

5. Parameterization of CI

It is mainly the label of the mirror.

I can do this in Jenkins,

Then use ${image_tag} in the execution shell.

Pit 6. Parameterization of shell

To receive parameters, execute NPM run build:env

To do this, just sh xxx.sh master.

But don’t use Spaces when defining variables in the shell.

my_env=”prod”

my_env = “prod”

This is different.


The body of the

Step 1: Build the build image

VTS. Registry/build/node: 14.10.0-1

Node: 14.10.0 + /home/vts-web2, /hoome/code has full node_modules.

Step 2: NPM run build on Jenkins

Docker run – v/home/Jenkins/workspace/VTS – web2: / home/VTS – web2 – w/home VTS. Registry/build/node: / bin/sh 14.10.0-1 – c “sh /home/vts-web2/build.sh stage”

/ home/Jenkins/workspace/VTS – web2 is ci working directory on the server, the code git pull here.

And the build container path to each other. Then execute the build.sh script.

#! /bin/sh # npm install --registry=https://registry.npm.taobao.org my_env="prod" if [ $1 ] then my_env=$1 echo "env is" $1  fi cp -R -a /home/vts-web2/* /home/code cd /home/code npm run build:$my_env mkdir /home/vts-web2/dist cp -R /home/code/dist/* /home/vts-web2/distCopy the code

The main thing to do is to cp the latest code pulled down to /home/code, overwrite.

Then execute NPM run build:env

After executing dist, cp the dist file back to /home/vts-web2, i.e. ci server working directory.

Third, docker Build

Docker build-t vts.registry/ VTS /vts-web-test:${image_tag}

Dockerfile is placed in the root directory.

FROM nginx:latest
WORKDIR /
COPY default.conf /etc/nginx/conf.d/
COPY deploy.sh /home/ws/
COPY dist/ /home/ws/vts-web/
RUN chmod 777 /home/ws/deploy.sh
Copy the code

Just pull the nginx image and add conf, dist, and deploy copy.

Docker Push

docker push vts.registry/vts/vts-web-test:${image_tag}

Step 5, CD/test

docker run -tid -p 8888:5000 vts.registry/vts/vts-web-test:${image_tag} sh /home/ws/deploy.sh

The contents of the deploy script are:

#! /bin/bash /usr/sbin/nginx -g "daemon off;"Copy the code

This way the nginx service can run and be accessed externally based on port.


digression

Originally intended to use Node to start the service, not nginx. Start with node Pm2.

But nginx was chosen.

I’ll write koA for node after I have BFF.

If YOU use Node,

DockerFILE

WORKDIR /
COPY deploy.sh /home/ws/
COPY vts-web/ /home/ws/vts-web/
RUN chmod 777 /home/ws/deploy.sh
Copy the code

depoly.sh

#! /bin/bash cd /home/ws/vts-web node server.js

server.js

Const Koa = require(' Koa ') // Koa2 middleware relies on const app = new Koa() //js's extension const static = require('koa-static') // static resource service plugin // App.use (static('./')); app.use(async (ctx) => { console.log(ctx, '123') ctx.body = 'VTS -web'}) app.listen(5000) // Service startup port console.log(' started successfully ') // Logs are printedCopy the code