美文网首页
gitlan-runner安装及ci/cd实战

gitlan-runner安装及ci/cd实战

作者: King斌 | 来源:发表于2021-02-07 15:14 被阅读0次

    说明:记得安装jdk, maven,node等
    安装

    curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash
    yum install gitlab-runner
    
    在runner执行过程中大多数是文件夹不存在,无权限。
    sudo gitlab-runner uninstall # 删除gitlab-runner
    gitlab-runner install --working-directory /home/gitlab-runner --user root   # 安装并设置--user(设置为root)
    sudo service gitlab-runner restart  # 重启gitlab-runner
    
    ps aux|grep gitlab-runner  # 查看当前runner用户
    
    

    yum install gitlab-runner-xxx后面可以跟版本号,不跟版本号的话默认安装最新的

    注册,先别慌,先登录gitlab,
    超级管理员登录gitlab, 点击小板手,点击Runners,就可以看到注册gitlab-runner要用的url和token

    开始注册,注册完成之后会有一个gitlab-runner实例

    gitlab-ci-multi-runner register                         
    ======================================================
    Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
    http://192.168.1.101:10080/    #看上图
    Please enter the gitlab-ci token for this runner:
    UuoZ7t3fsb7xjy6Pcm7C   #看上图
    Please enter the gitlab-ci description for this runner:
    [localhost]: runner-test
    Please enter the gitlab-ci tags for this runner (comma separated):
    test
    Whether to run untagged builds [true/false]:
    [false]: 
    Whether to lock Runner to current project [true/false]:
    [false]: 
    Registering runner... succeeded                     runner=UuoZ7t3f
    Please enter the executor: docker, docker-ssh, parallels, shell, ssh, docker+machine, virtualbox, docker-ssh+machine, kubernetes:
    shell
    Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 
    [root@localhost ~]# 
    
    

    查看gitlab-runner

    [root@localhost ~]# gitlab-runner list
    Listing configured runners    ConfigFile=/etc/gitlab-runner/config.toml
    runner-test                   xecutor=shell Token=FAHVYLEhG-9A46xDy8yG URL=http://192.168.1.101:10080/
    [root@localhost ~]# 
    
    

    如果你安装的gitlab和gitlab runner不在一个服务器上,那么,在gitlab runner这台服务器上要安装git工具,方便拉去代码
    CentOS7上安装Git工具:

    yum install git -y 安装
    git --version 安装后查看版本
    
    

    创建文件等下要用的脚本文件,也可以稍后再配置

    [root@centos7 ~]# su  gitlab-runner
    
    [gitlab-runner@centos7 root]$ cd /home/gitlab-runner/
    
    [gitlab-runner@centos7 ~]$ vi  pull
    #!/bin/bash
    if [ $# -ne 2 ]
    then
          echo "Arguments  error!"
          exit 1
    else
          pull_code_path="/home/webroot/$2"
          if [ ! -d "$pull_code_path" ]
          then
                  project_path=$1
                  git clone  -b  master  $project_path  $pull_code_path
          else
                  cd $pull_code_path
                  git pull
          fi
    fi
    # 保存退出
    #这个脚本的意思是 if [ $# -ne 2 ] 判断是否穿进来两个参数,如果没有就输出"Arguments  error!" ,如果有两个参数,
    判断[ ! -d "$pull_code_path" ] 文件夹存在不,不存在,直接克隆代码,会自动创建文件夹,存在则切换至该目录,拉取代码,
    
    

    给gitlab-runner执行需要的文件夹的权限

    [gitlab-runner@centos7 ~]$ chmod  +x  pull
    
    #注意脚本里面的deploy_path="/home/webroot/$2"
    #我们需要给gitlab-runner执行/home/webroot/该文件夹的权限
    [gitlab-runner@centos7 ~]$ su root   #切换会root用户
    [root@centos7 ~]$ chown -hR gitlab-runner:gitlab-runner /home/webroot/  #给gitlab-runner执行执行权限
    
    

    生成私钥

    [gitlab-runner@localhost root]$ ssh-keygen
    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/gitlab-runner/.ssh/id_rsa): 
    Created directory '/home/gitlab-runner/.ssh'.
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /home/gitlab-runner/.ssh/id_rsa.
    Your public key has been saved in /home/gitlab-runner/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:7CbuqeDWPwfUj94t/3pFo6/JR/pIfLUPH8nu4pS4c+w gitlab-runner@localhost
    The key's randomart image is:
    +---[RSA 2048]----+
    |                 |
    |                 |
    |       .         |
    |      ...      ..|
    |     .  So    ..o|
    |      ... . o.o.=|
    |  ..  .oo. o.==B |
    | ......+o o.*=*=o|
    | .. .+=o   +BEB+o|
    +----[SHA256]-----+
    [gitlab-runner@localhost root]$ cat ~/.ssh/id_rsa.pub
    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD9pWbAvHHNZLKsi2CcQpNmjh8MCQnQVWKr3uHT5SMW6zHjdyAZYIxL/a+hprwt9uDFhIz9CgmRX3JB4gUmEU1/5Q1tEbC/tMuFGdmSEv3QEecD1hPbP+XaKPSYmJobSoLXv1dLFx3rf/5tQ//xLZeRczxIUUTPO1mwob1aiAHz9ATrxkb8abhRp/1QORmlwuObIcjJGFGvPizo3yQkCJnPk+TWMtfbA0V86tHwyvQqUudP85zlmJq3yyyWwb2NEIlQHHkZBy8jOqlaHP7d2XI31nr5RFjv6UDv6UXlLu3/L9JBHizV0HmQ8wP26c4AbnbTQWHao3y8tBZ8edU6uEBJ gitlab-runner@localhost
    
    

    将该公钥复制到gitlab上面


    在gitlab上创建gitlab-runner用户,并授权给你创建的项目

    在自己本地更改项目
    创建.gitlab-ci.yml文件


    推送代码
    查看gitlab面板


    再看服务器 /home/webroot/文件夹下面是否有了刚才拉取的代码


    ok,至此简单的gitlab-runner服务器搭建完毕

    后续操作,编译,打包,发布
    创建子项目


    java的.gitlab-ci.yml文件

    stages:
      - pull
      - build
      - release
    
    pull:
      stage: pull
      script:
        - echo "开始拉取代码"
        - echo $CI_PROJECT_DIR
        - /home/gitlab-runner/pull   ${git_url}     ${parent_project_url}
      only:
        - master
      tags:
        - test #对应runner的tags
    
    build:
      stage: build
      script:
        - echo "开始编译"
        - cd ${parent_project_url}
        - mvn clean package -Dmaven.test.skip=true
      only:
        - master
      tags:
        - test #对应runner的tags
    
    release:
      stage: release
      script:
        - echo "开始发布"
        - /home/gitlab-runner/shell_run ${parent_project_url} ${project_name} ${project_jar_name}
      only:
        - master
      tags:
        - test  #对应runner的tags
    
    #相当于定义全局常量  如果改字段定义在job内,相当于局部常量
    variables:
      # 项目父路径
      parent_project_url: /home/webroot/root/项目名
      # 子项目名,及jar包名
      project_name: 项目名
      project_jar_name:  jar包名称
      git_url: 项目的git地址
    

    在/home/gitlab-runner/文件夹下,其实就是gitlab-runner的根目录下创建shell_run执行脚本,记得切换为gitlab-runner用户

    #!/bin/bash
    #因为执行脚本的时候 nohup命令没有生效,百度说这样干能行,所以加了一句刷新命令,这里我填坑一天,一直在找nohup命令不生效这个问题。也有可能是版本的问题,我看百度,好多博主都没有遇到这样的问题
    source /etc/profile
    if [ $# -ne 3 ]
    then
            echo "arguments error!"
            exit 1
    else
            project_file=$1
            project_name=$2
            jar_name=$3
            echo $jar_name
            pid=`ps -ef | grep "$jar_name.jar"|grep -v grep|awk '{print $2}'`
            for id in $pid
            do
                    echo $id
                    kill -9 $id
                    echo "killed $id"
            done
            nohup java -jar $project_file/$project_name/target/$jar_name.jar > /home/webroot/$jar_name.log 2>&1 &
    fi
    
    

    拉取,打包,发布三个阶段都执行成功

    vue的.gitlab-ci.yml文件

    stages:
      - pull
      - build
    
    cache:
      key: ${CI_BUILD_REF_NAME}
      paths:
        - node_modules/
    pull:
      stage: pull
      only:
        - master
      script:
        - /home/gitlab-runner/pull  ${git_url} ${project_name}
      tags: 
        - api-test #对应runner的tags
    build:
      stage: build
      only:
        - master
      script:
        - cd ${CI_PROJECT_DIR}
        - cnpm install #--registry=https://registry.npm.taobao.org
        - cnpm run build:prod
        - cp -r dist/* /usr/myproject/     #将打包好的文件复制到其他地方
      tags: 
        - api-test #对应runner的tags
    variables:
      project_name: 项目名
      git_url: 项目的git地址
    

    相关文章

      网友评论

          本文标题:gitlan-runner安装及ci/cd实战

          本文链接:https://www.haomeiwen.com/subject/noyztltx.html