基于 GitLab 配置 CI

Posted by pggsnap on March 1, 2018

GitLab 的安装配置(CentOS 7)

  • 安装依赖库,开放 ssh,http 端口
    $ sudo yum install curl policycoreutils openssh-server openssh-clients
    $ sudo systemctl enable sshd
    $ sudo systemctl start sshd
    $ sudo yum install postfix
    $ sudo systemctl enable postfix
    $ sudo systemctl start postfix
    $ sudo firewall-cmd --permanent --add-service=http
    $ sudo systemctl reload firewalld
    
  • 下载 rpm 包安装
    curl -LJO https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-10.5.2-ce.0.el7.x86_64.rpm
    rpm -ivh gitlab-ce-10.5.2-ce.0.el7.x86_64.rpm
    
  • 启动 GitLab
    sudo gitlab-ctl reconfigure # 根据配置文件/etc/gitlab/gitlab.rb启动服务
    # 常见的一些命令
    sudo gitlab-ctl status
    sudo gitlab-ctl start
    sudo gitlab-ctl stop
    sudo gitlab-ctl restart
    sudo gitlab-ctl tail  # 查看日志
    sudo vim /etc/gitlab/gitlab.rb  # 修改默认配置
    
  • 浏览器访问

    • 虚拟机上安装 GitLab,ip 为 192.168.99.13,直接在浏览器输入 ip 即可;

    • 首次登陆,账号密码为 root 和 5iveL!fe,成功后强制修改密码;

    • 新增用户,配置 ssh-key 连接,创建项目…

  • 修改项目路径(domain 更改为 ip)

    • 新建项目后,发现项目路径名,比如 http://gitlab.example.com:pggsnap/gitlab-cicd-demo.git 为域名,这样的话每个客户端都需要在本机上配置 hosts 才能访问,或者在公司内网 dns 配置。通过修改 domain 为 ip 可以方便客户端访问。

    • vim /etc/gitlab/gitlab.rb,修改 external_url;之后 sudo gitlab-ctl reconfigure。
      # external_url 'http://gitlab.example.com'
      external_url 'http://192.168.99.13'
      
    • 查看 /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml 中 host 的值是否已变更为 ip 地址;如果没有,手动修改后重启 gitlab。
      production: &base
      #
      # 1. GitLab app settings
      # ==========================
      
      ## GitLab settings
      gitlab:
        ## Web server settings (note: host is the FQDN, do not include http://)
        host: 192.168.99.13
        port: 80
        https: false
      
        # Uncommment this line below if your ssh host is different from HTTP/HTTPS one
        # (you'd obviously need to replace ssh.host_example.com with your own host).
        # Otherwise, ssh host will be set to the `host:` value above
        ssh_host:
      

GitLab CI 的安装配置

  • 安装 GitLab-Runner

    如上图,GitLab Runner section 链接中指出了 runner 的安装方法,https://docs.gitlab.com/runner/install/linux-repository.html

    centos7为例,

    curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash
    sudo yum install gitlab-runner
    
  • 注册 Gitlab-Runner,为项目创建单独的 specific runner。

    [root@wuyf gitlab-runner]# gitlab-ci-multi-runner register
      Running in system-mode.
    
      Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
      http://10.192.1.221/
      Please enter the gitlab-ci token for this runner:
      g2fvscKpyBhgG9iUp-55
      Please enter the gitlab-ci description for this runner:
      [pggsnap-spring-cloud-demo]:
      Please enter the gitlab-ci tags for this runner (comma separated):
    
      Whether to lock the Runner to current project [true/false]:
      [true]:
      Registering runner... succeeded                     runner=g2fvscKp
      Please enter the executor: docker+machine, kubernetes, docker, docker-ssh, parallels, virtualbox, shell, ssh, docker-ssh+machine:
      shell
      Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
    
  • 新建了一个 Spring Boot 项目,在项目根目录创建 .gitlab-ci.yml 文件,配置如下:
    build:
      script: "mvn clean package"
    
  • 之后每次上传代码到 GitLab 仓库时,会自动执行脚本,检查通过之后才会成功上传。