CentOS 7 安装 Git 与 Gitlab-runner 实例 原

CentOS 7 安装 Git 与 Gitlab-runner 实例

  1. 查看系统版本

    lsb_release -a
    

    本笔记系统环境:

    LSB Version:    :core-4.1-amd64:core-4.1-noarch
    Distributor ID: CentOS
    Description:    CentOS Linux release 7.5.1804 (Core) 
    Release:        7.5.1804
    Codename:       Core
    
  2. 安装 git 源码构建

    1. 检查 git 当前版本
      # 执行
      git --version
      
      # 显示老版本
      git version 1.8.1
      
    2. 安装编译依赖软件
      yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel asciidoc
      yum install gcc perl-ExtUtils-MakeMaker
      
    3. 卸载老版本
      yum remove git
      
    4. 下载最新版本

      git 官网

      on kernel.org 官方网址

      cd /usr/local/src/
      wget https://www.kernel.org/pub/software/scm/git/git-2.9.5.tar.xz
      tar -vxf git-2.9.5.tar.xz
      cd git-2.9.5
      
    5. 编译
      # 编译时发生错误,可能未安装依赖软件包
      make prefix=/usr/local/git all
      make prefix=/usr/local/git install
      
    6. 环境变量设置
      1. Root 用户

        # 添加环境变量
        echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/profile
        # 生效环境变量
        source /etc/profile
        
      2. 其他用户,

        登录该用户,配置该用户下的环境变量

        echo "export PATH=$PATH:/usr/local/git/bin" >> ~/.bashrc
        source ~/.bashrc
        
    7. 验证
      git --version
      
      git version 2.9.5
      
  3. 安装 gitlab-runner

    Gitlab runner 官方文档

    根据官方文档可快速安装,本文只修了安装路径,并手动授权。

    /gitlab/ 为安装路径,gitlab-runner为执行文件

    1. 下载执行文件
      # sudo wget -O /保存路径 https://网址
      sudo wget -O /gitlab/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
      
    2. 允许运行权限
      sudo chmod +x /gitlab/gitlab-runner
      
    3. 创建 gitlab-runner 用户
      sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
      
    4. 安装作为服务运行
      # 由于没有设置为全局变量,添加为服务时需要指定绝对路径
      # 由于该服务指定用户 gitlab-runner 执行,所以需要给该用户配置相关文件夹权限
      # --working-directory 参数为配置缓存地址,可自定义,注意需要文件夹权限
      sudo /gitlab/gitlab-runner install --user=gitlab-runner --working-directory=/gitlab/work
      

      /gitlab/work 该目录会生成一些 gitlab-runner 的配置文件,与高速缓存仓库,需要授权权限。

      如果不需要强调权限问题,可以使用 chmod 分配:
      chmod 777 /gitlab/work/
      

      ps:该权限方案适合快速使用,web 站点目录,都需要权限。

      如需要强调权限,可以使用 ACL 分配制定用户:
      # 给用户 gitlab-runner 设置 /gitlab/work 文件夹 rwx 权限
      setfacl -m u:gitlab-runner:rwx -R /gitlab/work
      # 设置后期添加的子目录和文件继承父目录权限
      setfacl -m d:u:gitlab-runner:rwx -R /gitlab/work
      

      ps:该方案需要一定的权限分配能力

    5. 用户权限

      本文使用 apache 代理网站。

      apache 用户添加至 gitlab-runner 组,允许 apache 有权读写 gitlab-runner 生成的文件
      usermod -a -G 'gitlab-runner' apache
      
      实例
      [[email protected] /]# usermod -a -G 'gitlab-runner' apache
      [[email protected] /]# id apache     
      uid=48(apache) gid=48(apache) groups=48(apache),1000(gitlab-runner)
      [[email protected] /]# id gitlab-runner
      uid=1000(gitlab-runner) gid=1000(gitlab-runner) groups=1000(gitlab-runner)
      
    6. 注册服务

      Gitlab runner 官方文档

      文档非常详细,自行查找

      本文使用 shell常规执行,功能有限,更多高级功能查看官方文档

    7. 运行程序

      sudo /gitlab/gitlab-runner start
      
    8. 更多功能

      Gitlab runner 官方文档

  4. gitlab-ci.yml 例子

    使用简单的shellgit代码,实现拉取仓库文件至指定位置

    # 工作类型标签
    stages:
        # 自定义工作类名称
        - 同步
    # 自定义工作名
    dev:
      tags:
        # 注册 gitlab-runner 时填写的机器人标签
        - gitlab
      stage: 同步
      script:
        # 打开目录
        - cd /Web
        # 判断是否存在 .git 文件 true 状态
        - if [ ! -d ".git" ];then
        # 初始化仓库
        - git init
        - else
        # 已经存在,跳过 git 仓库初始化
        - echo 'skip git int'
        - fi
        # 执行 git 根据仓库路径拉取文件
        - git pull $CI_PROJECT_DIR
      environment:
        # 环境名称
         name: dev
      only:
        # 只有指定分支推送时才执行
        - dev
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章