background

After the current end is developed, you need to deploy the code to the server, which is done by the front end packaging the code and placing it on the static server. There are as many iterations of this step as there are iterations of this step. Wouldn’t it save a lot of effort if automatic deployment were introduced

What you need to know

  1. gitlab
  2. gitlab-runner
  3. nginx
  4. . Yml command
  5. Shell command

steps

1. Install gitlab-runner on the Nginx server

# Download the binary for your system
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

# Give it permissions to execute
sudo chmod +x /usr/local/bin/gitlab-runner

# Create a GitLab CI user
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

# Install and run as service
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start
Copy the code

2. Gitlab – runner registered

sudo gitlab-runner register --url https://xxx.cn/ --registration-token xxx
Copy the code

The URL and token can be obtained in the group or project.

Location: Settings => CI/CD => Runners => URL & Token

3. Project add.gitlab-ci.yml

Image: node:alpine # build phase: build & deploy 2 jobs; Stages: -build-deploy cache: Paths: # Here these two files are cached for use in the second job. -node_modules / -dist/build: stage: build only: -npm run build deploy: stage: deploy only: -dev script: Delete the project deployment location file (/home/web/xm/). -rm -rf /home/web/xm/ # copy the package (dist/*) to the project deployment location (/home/web/xm/) -cp -rf dist/* /home/web/xm/ # Update the CDN cache using curl "https://refreshCDN.com?url=https://hhhh.com" - curl "https://noticeAPI.com?t=666"Copy the code

4. Submit the code and test the process

Problem & Solution

1. The packaging fails on the server

You need to check that the version of Node.js on the server meets the requirements of the framework used for the current project.

  1. View the node.js version of the current environment
node -v
Copy the code
  1. Installing n Modules
cnpm i -g n
Copy the code
  1. Update Node.js to the latest stable version
n stable
Copy the code

2. The SCRIPT of the YML file fails

After we commit the code to trigger runner, we may be prompted with a command error in the.gitlab-ci.yml file. One of the more likely things to go wrong are special symbols in script, which need to be wrapped in quotes

; {} [], & * #? | - < > =! % @ `Copy the code

3. No operation permission

Putting a new package into a project location may involve deleting and writing files.

Linux system is multi-user and multi-task, so maybe the project location permission is not gitlab-runner, so I change the project location permission to Gitlab-runner. All this must be in the /etc/password file

Modifying File Permissions

chown -R gitlab-runner 8/
Copy the code

Modifying folder Permissions

chgrp -R gitlab-runner 8
Copy the code

There is also a way for Gitlab-Runner to install with the permission set to root, which means that Gitlab-Runner can change all resources on the current server

conclusion

For the first two steps above, you can obtain the installation method of the corresponding platform in Gitlab Runner. The three problems encountered and corresponding solutions can be referred to in the following.

Follow the above steps to complete the nGINX deployment of Gitlab-runner function, my process will work. In my understanding, gitlab-runner is a kind of communication between two servers, with the help of.gitlab-ci.yml file to pass command scripts.

Feel free to leave a comment if you have any other weird questions