Recently, I have been researching the automatic deployment of code to the server. At first, I was going to use GitLab Runner to do it. It can trigger automatic deployment when pushing code to a specified remote branch, but the front-end code takes up a lot of memory. The build failed because The process exited too early. This probably means The system ran out of memory or someone Called kill-9 on the process (the test server itself is also relatively bad), then abandoned. This is now implemented in written scripts.

Write summary

For the code in GitLab and the code does not need to be packaged, or the server comparison 6 can be successfully packaged in the server, then use GitLab Runner to achieve automatic deployment, after all, after the configuration, only need to push the code mindless. In other cases, use a script and run it every time you need to deploy.

GitLab Runner

Go directly to the official documents and follow them, as mentioned below.

1 Install GitLab Runner on the server

Sudo gitlab-runner install –user=gitlab-runner –working-directory=/home/gitlab-runner It was done by the user gitlab-runner.

2 Register GitLab Runner on the server

3 Write the configuration file named.gitlab-ci.ymlPut it in the project root directory

I wrote a very simple one as follows:

<>codestages: - build js_build: stage: build before_script: - PATH = / root/NVM/versions/node/v9.3.0 / bin: $PATH - the export PATH script: -npm I -npm run build -cp -r build/WWW /certui-test/ VIP

Note that in before_script you need to define PATH to include node, because user was gitlab-runner when registered, it has no node environment, NPM I will report an error. (I tried to register user as root, but still no node, I don’t know why…)

Script implementation

Run the build.sh script and deploy. Sh script to deploy. The idea is: package locally -> upload files to the server -> back up the server’s original files, and then move the new code to the destination folder.

build.sh

#! /bin/bash npm run build ./deploy.shCopy the code

It simply does the package, package OK, and execute deploy.sh.

deploy.sh

#! /usr/bin/expect set user Ubuntu # username set IP address 1.2.3.4 # server IP address set password 123456 # SSH password set timeout 1200 set date [timestamp -format {%m-%d-%T}] set dir certui spawn SCP -r./build ${user}@${IP}:/ TMP / # SCP ${password}\r" expect {eof {puts "Upload success"} timeout {puts "*** upload failure ***" exit Spawn SSH ${user}@${IP} # Expect "password" send "${password}\r" expect "Last login" send "su - CD/WWW \r" send "mv ${dir} ${dir}.${date} \r" # send "mkdir ${dir} \r" mv /tmp/build/ ${dir}/vip \r" interactCopy the code

The SSH password is written directly into the script, or it can be manually entered during script execution and then read by the script.

On second thought, this is like the first script I’ve ever written? Feel the power of the script