持续集成环境搭建(1)GitLab搭建和使用
概述
- GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的web服务。
环境准备
- 操作系统 CentOS7.6.1810
镜像下载地址: http://mirrors.nju.edu.cn/centos/7.6.1810/isos/x86_64/CentOS-7-x86_64-Minimal-1810.iso
GitLab搭建
1. 安装和配置必要的依赖
首先,在系统控制台执行以下命令打开系统防火墙的http和ssh访问
[root@localhost ~]# sudo yum install -y curl policycoreutils-python openssh-server
[root@localhost ~]# sudo systemctl enable sshd
[root@localhost ~]# sudo systemctl start sshd
[root@localhost ~]# sudo firewall-cmd --permanent --add-service=http
[root@localhost ~]# sudo systemctl reload firewalld
接下来,安装Postfix,用于发送邮件通知。如果想采用其他方式来发送邮件,可以跳过此步骤,在GitLab安装完的时候,再进行配置。
[root@localhost ~]# sudo yum install postfix
[root@localhost ~]# sudo systemctl enable postfix
[root@localhost ~]# sudo systemctl start postfix
2.添加GitLab package仓库和安装GitLab的package
添加package仓库
[root@localhost ~]# curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh | sudo bash
安装GitLab的package
[root@localhost ~]# sudo yum install -y gitlab-ee
3.修改GitLab配置
配置文件路径:/etc/gitlab/gitlab.rb
找到 external_url,修改为想要的域名
external_url = http://gitlab.whenling.com
4.启动GitLab
[root@localhost ~]# sudo gitlab-ctl reconfigure
5.访问GitLab
访问地址(GitLab服务器ip的80端口): http://192.168.1.177
GitLab配置
采用其他端口
修改配置文件/etc/gitlab/gitlab.rb
external_url = http://gitlab.whenling.com:8888
nginx['listen_port'] = 8888
保存退出,让GitLab执行重新配置且重启服务
[root@localhost ~]# sudo gitlab-ctl reconfigure
[root@localhost ~]# sudo gitlab-ctl restart
注意开放对应的防火墙端口
[root@localhost ~]# firewall-cmd --zone=public --add-port=8888/tcp --permanent
[root@localhost ~]# sudo systemctl reload firewalld
GitLab邮件服务
修改配置文件:/etc/gitlab/gitlab.rb
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "375391531@qq.com"
gitlab_rails['smtp_password'] = "ddvwqdmbiwkubjia"
gitlab_rails['smtp_domain'] = "qq.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
user['git_user_email'] = "375391531@qq.com"
gitlab_rails['gitlab_email_from'] = '375391531@qq.com'
保存退出,让GitLab执行重新配置
[root@localhost ~]# sudo gitlab-ctl reconfigure
测试邮件发送
进入控制台:
[root@localhost ~]# gitlab-rails console
执行以下命令
irb(main):001:0> Notify.test_email('156801554@qq.com','the title', 'the content').deliver_now
GitLab使用
创建一个项目
采用root账号登录 首次登陆后页面 选择Create a project 输入新建项目的信息测试本地是否能提交到GitLab
在本地创建一个文件夹,初始化git仓库
kongxiangxis-MacBook-Pro:~ kongxiangxi$ mkdir test
kongxiangxis-MacBook-Pro:~ kongxiangxi$ cd test
kongxiangxis-MacBook-Pro:test kongxiangxi$ git init
添加一个文件
kongxiangxis-MacBook-Pro:test kongxiangxi$ echo "hello world" >> file1
kongxiangxis-MacBook-Pro:test kongxiangxi$ git add *
kongxiangxis-MacBook-Pro:test kongxiangxi$ git commit -m "test"
配置远程仓库
kongxiangxis-MacBook-Pro:test kongxiangxi$ git remote add origin http://gitlab.whenling.com:8888/root/gitlab-example.git
kongxiangxis-MacBook-Pro:test kongxiangxi$ git branch --set-upstream-to=origin/master master
拉取远程文件
kongxiangxis-MacBook-Pro:test kongxiangxi$ git pull origin master --allow-unrelated-histories
提交本地修改到远程仓库
kongxiangxis-MacBook-Pro:test kongxiangxi$ git push --set-upstream origin master
GitLab注册需要验证邮箱
注册需要验证邮箱选择后需要点击下面的 Save Changes 按钮
采用SSH方式
创建公钥和私钥
kongxiangxis-MacBook-Pro:~ kongxiangxi$ ssh-keygen -t rsa
私钥保存于 /Users/kongxiangxi/.ssh/id_rsa.
公钥保存于 /Users/kongxiangxi/.ssh/id_rsa.pub.
把公钥的内容添加到GitLab
把公钥的内容添加到GitLab
测试ssh方式提交
kongxiangxis-MacBook-Pro:~ kongxiangxi$ mkdir testssh
kongxiangxis-MacBook-Pro:~ kongxiangxi$ cd testssh/
kongxiangxis-MacBook-Pro:testssh kongxiangxi$ git init
kongxiangxis-MacBook-Pro:testssh kongxiangxi$ git remote add origin git@gitlab.whenling.com:dev/group-test.git
kongxiangxis-MacBook-Pro:testssh kongxiangxi$ echo "sss" >> file2.txt
kongxiangxis-MacBook-Pro:testssh kongxiangxi$ git add *
kongxiangxis-MacBook-Pro:testssh kongxiangxi$ git commit -m "Initial commit"
kongxiangxis-MacBook-Pro:testssh kongxiangxi$ git push -u origin master