3
Purpose: Get rid of Jenkins deployment; if the front end has its own server environment; you can use gitlab-runner realize CI automatic deployment and send result notification messages

1. Server installation gitlab-runner

After confirming the server environment information, find the corresponding gitlab-runner installation package
  1. Use uname -a view server version information
    image.png
  2. Here find the installation package that matches your version information
  3. Use the installation package to install the gitlab-runner program; if it prompts sudo: curl: the command is not found, use yum install curl and then reinstall the program yum update -y && yum install curl -y (because the yum update will be performed before installing curl).

    sudo curl -L --output /usr/local/bin/gitlab-runner "https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64"
  4. Given execution permission

    sudo chmod +x /usr/local/bin/gitlab-runner
  5. Create a user

    sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
  6. Install and run services

    sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
    sudo gitlab-runner start

    If this step also prompts sudo: gitlab-runner: command not found; required

    # 修改/etc/sudoers文件;找到这一行
    Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin  
    # 将要执行的命令所在的目录添加到后面,即可
    Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin 

    Re-execute the installation and run the service to continue

  7. Verify installation information gitlab-runner -v
    image.png

2. Configure and register the Runner

  1. Find the corresponding information in your gitlab warehouse> Settings -> CI/CD -> Runners -> Specific Runners
    image.png
  2. Execute the registration command sudo gitlab-runner register
    image.png
  3. View the active Runner gitlab-runner verify
    image.png
  4. You can also see that it has been activated CI
    image.png

3. Write yml configuration file

.gitlab-ci.yml script file in the root path of your own project
image.png
Write your own program

stages: # 分阶段
  - install
  - build
  - deploy
  - notify

cache: # 缓存内容
  paths:
    - node_modules/
    - dist/

install-job:
  tags: # 触发runner(这个就是注册的时候填写的tag)
    - master
  only: # 哪些分支触发
    - master
  stage: install # 当前阶段
  script:
    - echo '安装依赖阶段....'
    - npm install

build-job:
  tags:
    - master
  only:
    - master
  stage: build
  script:
    - npm run build:dev

deploy-job:
  tags:
    - master
  only:
    - master
  stage: deploy
  script:
    - whoami
    - sudo scp -r ./dist/* /www/html/kangshifu/kangshifu-vue/mars/dist


# 构建成功时的通知消息
notifySuccessWeChat:
  stage: notify
  script:
    - echo $title $sucContent
    - curl 'http://pushplus.hxtrip.com/send' -H 'Content-Type:application/json' -d "{\"token\":\"130a550e3a6d4dd2a019a98a5c022900\",\"title\":\"康师傅_前端_智云系统-部署结果(ci)\",\"content\":\"成功\",\"template\":\"html\",\"topic\":\"kangshifuFeZhiyun\"}"
  only:
    - master
  tags:
    - master
  when: on_success    

# 构建失败时的通知消息
notifyFailWeChat:
  stage: notify
  script:
    - curl 'http://pushplus.hxtrip.com/send' -H 'Content-Type:application/json' -d "{\"token\":\"130a550e3a6d4dd2a019a98a5c022900\",\"title\":\"康师傅_前端_智云系统-部署结果(ci)\",\"content\":\"失败\",\"template\":\"html\",\"topic\":\"kangshifuFeZhiyun\"}"
  only:
    - master
  tags:
    - master
  when: on_failure  
This script contains a notice deployment results; specific configurations can view corresponding documents configured, you can use nail message or enterprise micro-channel messages, etc.

Possible problems encountered here

  1. git version problem

    报错:
    Getting source from Git repository
    00:01
    Fetching changes with git depth set to 50...
    Reinitialized existing Git repository in /home/gitlab-runner/builds/ErNGnaLr/0/ivm/mars/.git/
    fatal: git fetch-pack: expected shallow list
    fatal: The remote end hung up unexpectedly
    Cleaning up project directory and file based variables
    00:00
    ERROR: Job failed: exit status 1
    
    原因:
    centos7 基础仓库,提供的 git 版本只有到 1.8.3,沒办法使用 git 2 的一些新功能
    
    解决方法:
    安装最新版本的git就可以了
  2. Copy file problem
    sshpass is not used to manipulate files; the currently used scp is used to manipulate files; you may be prompted

    报错: 
    We trust you have received the usual lecture from the local System
    Administrator. It usually boils down to these three things:
     #1) Respect the privacy of others.
     #2) Think before you type.
     #3) With great power comes great responsibility.
    sudo: no tty present and no askpass program specified
    Cleaning up project directory and file based variables
    00:00
    ERROR: Job failed: exit status 1
    
    原因:
    由于尝试使用sudo运行命令引起的 ,但是调用用户没有被授权使用 sudo
    
    解决:
    打开 sudoers 文件`vim /etc/sudoers` ;将以下内容添加到文件底部
    gitlab-runner ALL=(ALL) NOPASSWD: ALL

So far gitlab-runner can successfully run "Manual Spreading Flowers🎉"; you can also receive the push message of the deployment result
image.png
image.png


songxianling1992
1.3k 声望3.6k 粉丝

当你不知道该选择哪条路的时候;可能你已经走了好一阵子了~