关于前端:服务器配置gitlabrunner流程问题总结

9次阅读

共计 3113 个字符,预计需要花费 8 分钟才能阅读完成。

目标:解脱 Jenkins 部署;如果前端有本人的服务器环境;能够借助 gitlab-runner 实现 CI 主动部署并且发送后果告诉音讯

1. 服务器装置 gitlab-runner

先确认服务器环境信息之后找到对应的 gitlab-runner 安装包

  1. 应用uname -a 查看服务器版本信息
  2. 在此处找到合乎本人版本信息的安装包
  3. 应用安装包装置 gitlab-runner 程序;如果提醒 sudo: curl:找不到命令, 则应用yum 装置 curl 后从新安装程序yum update -y && yum install curl -y,(因为装置 curl 之前会先进行 yum 的 update 所以这个工夫挺长的)

    sudo curl -L --output /usr/local/bin/gitlab-runner "https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64"
  4. 给定执行权限

    sudo chmod +x /usr/local/bin/gitlab-runner
  5. 创立一个用户

    sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
  6. 装置运行服务

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

    如果这一步也提醒sudo: gitlab-runner:找不到命令;须要

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

    从新执行装置运行服务就能够持续了

  7. 验证装置信息gitlab-runner -v

2. 配置注册 Runner

  1. 在本人 gitlab 仓库找到对应的信息 > Settings -> CI/CD -> Runners -> Specific Runners
  2. 在服务器下执行注册命令sudo gitlab-runner register
  3. 查看正在激活的 Runner gitlab-runner verify
  4. 也能够在 CI 处看到曾经被激活

3. 编写 yml 配置文件

在本人我的项目的根门路下编写 .gitlab-ci.yml 脚本文件

编写合乎本人的程序

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  

此脚本中蕴含了部署后果的告诉;具体配置形式能够查看对应的文档进行配置,也能够应用 钉钉音讯 或者 企业微信 音讯等形式

此处可能遇到的问题

  1. git 版本问题

    报错:
    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. 拷贝文件问题
    因为没有应用sshpass 的形式操作文件;以后应用的scp 进行文件的操作;可能会提醒

    报错: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

至此 gitlab-runner 曾经能够胜利的运行了「手动撒花🎉」

正文完
 0