GitLab基本使用

一、引言

  在微服务架构中,由于我们对系统的划分粒度足够小,服务会很多,而且也存在经常迭代的情况。如果还按照以前的部署方式显得非常吃力和复杂,并且很容易出现错误。而随着容器技术的发展,这个时候持续集成(CI)和持续部署(DI)也相应的流行起来,极大的方便了微服务的部署,而GitLab正式这样的一个DevOps工具。

  GitLab是由GitLabInc.开发,使用MIT许可证的基于网络的Git仓库管理工具,且具有wiki和issue跟踪功能。使用Git作为代码管理工具,并在此基础上搭建起来的web服务。关于GitLab的信息可以查看官网: https://about.gitlab.com/ 。

  接下来我们先来看下在Docker中如何安装GitLab。

二、安装GitLab

  具体的安装可以在 https://docs.gitlab.com/omnibus/docker/README.html 看到详细的信息。

docker run --detach --hostname gitlab.example.com --publish 443:443 --publish 80:80 --publish 22:22 --name gitlab --restart always --volume /srv/gitlab/config:/etc/gitlab --volume /srv/gitlab/logs:/var/log/gitlab --volume /srv/gitlab/data:/var/opt/gitlab gitlab/gitlab-ce:latest

   安装完成后可以通过浏览器访问到,作为git仓库其用法和github类似。

三、持续集成CI

  GitLab Runner是一个开源项目,用于运行您的作业并将结果发送回GitLab。它与GitLab CI一起使用,GitLab CI是GitLab随附的开源持续集成服务,用于协调作业。https://docs.gitlab.com/runner/ 。

  具体对于gitlab runner的详细介绍就请阅读官方文档。这里直接介绍其安装,那么我使用的是centos,就以linux x86-64安装为例。

  1. 下载二进制文件

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

  2. 赋予其执行权限

chmod +x /usr/local/bin/gitlab-runner

  3. 创建用户

useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

  4. 安装并运行,如果gitlab-runner安装并作为服务运行,它将以root用户身份运行,但将按照install命令指定的用户执行作业。 这意味着某些作业函数(如缓存和工件)需要执行/usr /local/bin/gitlab-runner命令,因此运行作业的用户需要具有对可执行文件的访问权限。

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

  5. 将gitlab runner注册到gitlab上

  (1) 运行命令

gitlab-runner register

  (2) 输入GitLab的URL

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
https://gitlab.example.com

  (3) 输入token,token的获取在Gitlab中,如下图

 Please enter the gitlab-ci token for this runner
 xxx

  (4) 输入runner的描述

 Please enter the gitlab-ci description for this runner
 [hostname] my-runner

  (5) 输入标签

 Please enter the gitlab-ci tags for this runner (comma separated):
 my-tag,another-tag

  (6) 选择Runner的执行者,这里使用shell

 Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
 shell

  完成之后就可以在GitLab中查看到Runner了

  6. 将.Net Core项目提交到GitLab完成自动化集成部署

  (1)  在.git 根目录下创建.gitlab-ci.yml文件

rtest:
  script:
    - cd User.Api
    - docker-compose up -d --build --force-recreate

  (2) 提交代码到GitLab上,由于22端口被占用改了其他端口,在git 的时候可以使用下面的命令来改变端口

git remote set-url origin ssh://git@172.18.6.155:23/jesen/user.api.git

  或者clone仓库的时候直接指定

git clone ssh://git@172.18.6.155:23/jesen/user.api.git

  (3) 提交完后,gitlab ci会自动构建镜像并运行,在这中间可能会有问题发生,我遇到的问题是找不到  devtoolset-7 和 git,这时可以是使用yum安装

yum install devtoolset-7
yum install git

  (4) 再次重试构建,依然出错

    出现此错误是因为权限的问题,之前安装gitlab时创建的gitlab-runner用户没有指定用户组docker,可以查看/etc/group是否存在docker用户组,不存在则先创建 groupadd docker,然后将gitlab-runner用户添加到docker用户组中

gpasswd -a gitlab-runner docker
或
usermod -aG docker gitlab-runner

  再次Retry,自动构建成功了

 三、部署本地镜像仓库Registry,详细可参考 https://docs.docker.com/registry/

  1、安装

docker run -d -p 5000:5000 --restart=always --name registry registry:2

  2、给镜像打标签后推送到本地仓库

docker image tag ubuntu localhost:5000/myfirstimage
docker push localhost:5000/myfirstimage

  3、从本地仓库拉取镜像

docker pull localhost:5000/myfirstimage

  4、停止并移除所有数据

docker container stop registry && docker container rm -v registry

  5、添加可视化UI:https://github.com/kwk/docker-registry-frontend

docker run -d --name registry-web --link registry:registry -e ENV_DOCKER_REGISTRY_HOST=registry -e ENV_DOCKER_REGISTRY_PORT=5000 -p 8080:80 konradkleine/docker-registry-frontend:v2

 四、参考资料

  Gitlab CI 官方配置文件参数说明快速入门 :https://docs.gitlab.com/ce/ci/quick_start/README.html 

  配置文档讲解 :https://docs.gitlab.com/ce/ci/yaml/

  持续集成的概念:http://www.ruanyifeng.com/blog/2015/09/continuous-integration.html

原文地址:https://www.cnblogs.com/jesen1315/p/11471354.html