CI&CD

Modify the. Gitlab-ci. yml file as follows:

stages: 
  - build
  - deploy
before_script: 
  - pwd
  - ls
build: 
  script: 
    - yarn
    - tsc
    - "cp -r dist /data/sites/koa-server"
    - "cp -r package.json /data/sites/koa-server"
    - "cp -r node_modules /data/sites/koa-server"
  stage: build

deploy: 
  script: 
    - "[ -d /data/sites/koa-server ] && echo ok || mkdir -p /data/sites/koa-server" If the directory does not exist, create it
    - "pm2 start /data/sites/koa-server/dist/index.js" Use PM2 to start the service
  stage: deploy

Copy the code

This file is a lot more complicated than the last one, so let’s take a look at what’s in it.

First of all, we define stages, which is used to define the stages for your CI implementation. Here we define two stages, one is build and one is deploy. This should make a lot of sense. Build and then deploy, right? Interim build and deploy are the names of stages, which are custom.

Next is before_script, where all scripts defined here are executed before all scripts in stages run.

Next, we define two jobs. Note that the build and deploy names do not need to be the same as the stages above.

During job definition, you use stages to specify which stage to associate with.

In simple terms, you define stages, then jobs, and then specify stages in jobs so that at run time, you know which jobs each stage needs to run.

Then let’s see what it looks like:

The effect

After writing the CI file, submit it to the GitLab server. Take a look at the running effect:

When this is done, open your browser and go to mygitlab.com:3000/users

Results: