GitLab的一键安装方式以及使用Git工具上传代码
一、安装环境的介绍
Centos 6.7 x86_64 2.6.32-573.el6.x86_64 IP:192.168.159.129 #公司内部测试服务器
GitLab版本:9.1.2
软件包下载地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el6/
二、安装过程
#rpm -ivh gitlab-ce-9.1.2-ce.0.el6.x86_64.rpm Preparing... ########################################### [100%] 1:gitlab-ce ########################################### [100%]hostname: Host name lookup failure *. *. *** *** ***** ***** .****** ******* ******** ******** ,,,,,,,,,***********,,,,,,,,, ,,,,,,,,,,,*********,,,,,,,,,,, .,,,,,,,,,,,*******,,,,,,,,,,,, ,,,,,,,,,*****,,,,,,,,,. ,,,,,,,****,,,,,, .,,,***,,,, ,*,. _______ __ __ __ / ____(_) /_/ / ____ _/ /_ / / __/ / __/ / / __ `/ __ \ / /_/ / / /_/ /___/ /_/ / /_/ / \____/_/\__/_____/\__,_/_.___/gitlab: Thank you for installing GitLab!gitlab: To configure and start GitLab, RUN THE FOLLOWING COMMAND:sudo gitlab-ctl reconfiguregitlab: GitLab should be reachable at http://gitlab.example.comgitlab: Otherwise configure GitLab for your system by editing /etc/gitlab/gitlab.rb filegitlab: And running reconfigure again.gitlab: gitlab: For a comprehensive list of configuration options please see the Omnibus GitLab readmegitlab: https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.mdgitlab: It looks like GitLab has not been configured yet; skipping the upgrade script.根据上面的提示执行:#sudo gitlab-ctl reconfigureStarting Chef Client, version 12.12.15resolving cookbooks for run list: ["gitlab"]Synchronizing Cookbooks: - gitlab (0.0.1) - package (0.0.0) - runit (0.14.2)Installing Cookbook Gems:Compiling Cookbooks...Recipe: gitlab::default * directory[/etc/gitlab] action create - change mode from '0755' to '0775'init (upstart 0.6.5) Converging 378 resources * directory[/etc/gitlab] action create (up to date) * directory[Create /var/opt/gitlab] action create - create new directory /var/opt/gitlab - change mode from '' to '0755' - change owner from '' to 'root' - change group from '' to 'root' * directory[/opt/gitlab/embedded/etc] action create - create new directory /opt/gitlab/embedded/etc - change mode from '' to '0755' - change owner from '' to 'root' - change group from '' to 'root' * template[/opt/gitlab/embedded/etc/gitconfig] action create - create new file /opt/gitlab/embedded/etc/gitconfig - update content in file /opt/gitlab/embedded/etc/gitconfig from none to 5fe039 --- /opt/gitlab/embedded/etc/gitconfig 2017-05-25 10:46:30.027207736 +0800 +++ /opt/gitlab/embedded/etc/.chef-gitconfig20170525-6666-jc6wi6 2017-05-25 10:46:30.026207736 +0800 @@ -1 +1,10 @@ +[pack] + threads = 1 +[receive] + fsckObjects = true +[repack] + writeBitmaps = true +[transfer] + hideRefs=^refs/tmp/ +hideRefs=^refs/keep-around/ - change mode from '' to '0755'Recipe: gitlab::web-server * group[Webserver user and group] action create - create group gitlab-www * user[Webserver user and group] action create - create user gitlab-wwwRecipe: gitlab::users……………………………… #会输出很多初始化的信息 + - change owner from '' to 'root' - change group from '' to 'root' * ruby_block[reload gitlab-monitor svlogd configuration] action nothing (skipped due to action :nothing) * file[/opt/gitlab/sv/gitlab-monitor/down] action delete (up to date) * link[/opt/gitlab/init/gitlab-monitor] action create - create symlink at /opt/gitlab/init/gitlab-monitor to /opt/gitlab/embedded/bin/sv * link[/opt/gitlab/service/gitlab-monitor] action create - create symlink at /opt/gitlab/service/gitlab-monitor to /opt/gitlab/sv/gitlab-monitor * ruby_block[supervise_gitlab-monitor_sleep] action run - execute the ruby block supervise_gitlab-monitor_sleep * service[gitlab-monitor] action nothing (skipped due to action :nothing) * execute[/opt/gitlab/bin/gitlab-ctl start gitlab-monitor] action run [execute] ok: run: gitlab-monitor: (pid 7720) 1s - execute /opt/gitlab/bin/gitlab-ctl start gitlab-monitorRecipe: gitlab::default * link[/opt/gitlab/service/gitlab-git-http-server] action delete (up to date) * directory[/opt/gitlab/sv/gitlab-git-http-server] action delete (up to date)Recipe: gitlab::gitlab-rails * execute[clear the gitlab-rails cache] action run - execute /opt/gitlab/bin/gitlab-rake cache:clearRecipe: gitlab::redis * ruby_block[reload redis svlogd configuration] action create - execute the ruby block reload redis svlogd configurationRecipe: gitlab::postgresql * ruby_block[reload postgresql svlogd configuration] action create - execute the ruby block reload postgresql svlogd configurationRecipe: gitlab::unicorn * ruby_block[reload unicorn svlogd configuration] action create - execute the ruby block reload unicorn svlogd configurationRecipe: gitlab::sidekiq * ruby_block[reload sidekiq svlogd configuration] action create - execute the ruby block reload sidekiq svlogd configurationRecipe: gitlab::gitaly * service[gitaly] action restart - restart service service[gitaly] * ruby_block[reload gitaly svlogd configuration] action create - execute the ruby block reload gitaly svlogd configurationRecipe: gitlab::gitlab-workhorse * service[gitlab-workhorse] action restart - restart service service[gitlab-workhorse] * ruby_block[reload gitlab-workhorse svlogd configuration] action create - execute the ruby block reload gitlab-workhorse svlogd configurationRecipe: gitlab::nginx * ruby_block[reload nginx svlogd configuration] action create - execute the ruby block reload nginx svlogd configurationRecipe: gitlab::logrotate * ruby_block[reload logrotate svlogd configuration] action create - execute the ruby block reload logrotate svlogd configurationRecipe: gitlab::prometheus * service[prometheus] action restart - restart service service[prometheus] * ruby_block[reload prometheus svlogd configuration] action create - execute the ruby block reload prometheus svlogd configurationRecipe: gitlab::node-exporter * ruby_block[reload node-exporter svlogd configuration] action create - execute the ruby block reload node-exporter svlogd configurationRecipe: gitlab::redis-exporter * ruby_block[reload redis-exporter svlogd configuration] action create - execute the ruby block reload redis-exporter svlogd configurationRecipe: gitlab::postgres-exporter * service[postgres-exporter] action restart - restart service service[postgres-exporter] * ruby_block[reload postgres-exporter svlogd configuration] action create - execute the ruby block reload postgres-exporter svlogd configurationRecipe: gitlab::gitlab-monitor * service[gitlab-monitor] action restart - restart service service[gitlab-monitor] * ruby_block[reload gitlab-monitor svlogd configuration] action create - execute the ruby block reload gitlab-monitor svlogd configurationRunning handlers:Running handlers completeChef Client finished, 325/427 resources updated in 03 minutes 29 secondsgitlab Reconfigured!可以看到提示执行了3分钟29秒!查看启动情况#netstat -tunlpActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:9121 0.0.0.0:* LISTEN 7685/redis_exporter tcp 0 0 127.0.0.1:9090 0.0.0.0:* LISTEN 7839/prometheus tcp 0 0 127.0.0.1:9187 0.0.0.0:* LISTEN 7850/postgres_expor tcp 0 0 127.0.0.1:9100 0.0.0.0:* LISTEN 7638/node_exporter tcp 0 0 127.0.0.1:9168 0.0.0.0:* LISTEN 7865/ruby tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 7519/unicorn master tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7561/nginx tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 5506/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 5585/master tcp 0 0 0.0.0.0:8060 0.0.0.0:* LISTEN 7561/nginx tcp 0 0 :::3306 :::* LISTEN 6289/mysqld tcp 0 0 ::1:9168 :::* LISTEN 7865/ruby tcp 0 0 :::22 :::* LISTEN 5506/sshd tcp 0 0 ::1:25 :::* LISTEN 5585/master 关闭# sudo gitlab-ctl stopok: down: gitaly: 0s, normally upok: down: gitlab-monitor: 1s, normally upok: down: gitlab-workhorse: 0s, normally upok: down: logrotate: 0s, normally upok: down: nginx: 0s, normally upok: down: node-exporter: 0s, normally upok: down: postgres-exporter: 1s, normally upok: down: postgresql: 1s, normally upok: down: prometheus: 0s, normally upok: down: redis: 0s, normally upok: down: redis-exporter: 0s, normally upok: down: sidekiq: 0s, normally upok: down: unicorn: 0s, normally up启动# sudo gitlab-ctl startok: run: gitaly: (pid 8296) 0sok: run: gitlab-monitor: (pid 8301) 0sok: run: gitlab-workhorse: (pid 8304) 1sok: run: logrotate: (pid 8312) 0sok: run: nginx: (pid 8318) 1sok: run: node-exporter: (pid 8324) 0sok: run: postgres-exporter: (pid 8329) 1sok: run: postgresql: (pid 8334) 0sok: run: prometheus: (pid 8342) 0sok: run: redis: (pid 8350) 1sok: run: redis-exporter: (pid 8354) 0sok: run: sidekiq: (pid 8359) 1sok: run: unicorn: (pid 8362) 0s重启#sudo gitlab-ctl restart #其实不加sudo也可以!查看添加了几个用户,如下:#tail -5 /etc/passwdgitlab-www:x:498:499::/var/opt/gitlab/nginx:/bin/falsegit:x:497:498::/var/opt/gitlab:/bin/shgitlab-redis:x:496:497::/var/opt/gitlab/redis:/bin/falsegitlab-psql:x:495:496::/var/opt/gitlab/postgresql:/bin/shgitlab-prometheus:x:494:495::/var/opt/gitlab/prometheus:/bin/sh
三、登录验证
在浏览器输入http://192.168.159.129 就可以看到GitLab的界面了,如下:
第一次登录需要设置新密码,并且密码长度不能小于8!设置好之后,会跳转到登录界面,如下:
默认用户名是root,然后输入刚才新设置的密码,登录之后,界面如下:
到这里一键安装GitLab就完成了,就可以进行创建项目之类的操作了。
四、postgresql相关命令介绍
注:因为使用一键安装GitLab的rpm包,默认数据库是postgresql,所以对于不熟悉的朋友,可以参考!
查看postgresql的数据存放目录[root@web1 gitlab]# su - gitlab-psql-sh-4.1$ lltotal 4drwx------ 19 gitlab-psql root 4096 May 25 10:55 data-sh-4.1$ ll data/total 112drwx------ 6 gitlab-psql root 4096 May 25 10:46 basedrwx------ 2 gitlab-psql root 4096 May 25 10:55 globaldrwx------ 2 gitlab-psql root 4096 May 25 10:46 pg_clogdrwx------ 2 gitlab-psql root 4096 May 25 10:46 pg_commit_tsdrwx------ 2 gitlab-psql root 4096 May 25 10:46 pg_dynshmem-rw-r--r-- 1 gitlab-psql root 3198 May 25 10:46 pg_hba.conf-rw-r--r-- 1 gitlab-psql root 1789 May 25 10:46 pg_ident.confdrwx------ 4 gitlab-psql root 4096 May 25 10:46 pg_logicaldrwx------ 4 gitlab-psql root 4096 May 25 10:46 pg_multixactdrwx------ 2 gitlab-psql root 4096 May 25 10:55 pg_notifydrwx------ 2 gitlab-psql root 4096 May 25 10:46 pg_replslotdrwx------ 2 gitlab-psql root 4096 May 25 10:46 pg_serialdrwx------ 2 gitlab-psql root 4096 May 25 10:46 pg_snapshotsdrwx------ 2 gitlab-psql root 4096 May 25 10:55 pg_statdrwx------ 2 gitlab-psql root 4096 May 25 11:11 pg_stat_tmpdrwx------ 2 gitlab-psql root 4096 May 25 10:46 pg_subtransdrwx------ 2 gitlab-psql root 4096 May 25 10:46 pg_tblspcdrwx------ 2 gitlab-psql root 4096 May 25 10:46 pg_twophase-rw------- 1 gitlab-psql root 4 May 25 10:46 PG_VERSIONdrwx------ 3 gitlab-psql root 4096 May 25 11:10 pg_xlog-rw------- 1 gitlab-psql root 88 May 25 10:46 postgresql.auto.conf-rw-r--r-- 1 gitlab-psql root 20132 May 25 10:46 postgresql.conf-rw------- 1 gitlab-psql gitlab-psql 90 May 25 10:55 postmaster.opts-rw------- 1 gitlab-psql gitlab-psql 101 May 25 10:55 postmaster.pidpostgresql 版本#cat PG_VERSION 9.6查看postgresql的进程#ps aux | grep postgreroot 7378 0.0 0.0 3940 360 ? Ss 10:46 0:00 runsv postgresqlroot 7379 0.0 0.0 4084 472 ? S 10:46 0:00 svlogd -tt /var/log/gitlab/postgresqlroot 7697 0.0 0.0 3940 356 ? Ss 10:48 0:00 runsv postgres-exporterroot 7698 0.0 0.0 4084 476 ? S 10:48 0:00 svlogd -tt /var/log/gitlab/postgres-exporter495 13278 0.0 0.6 127968 11924 ? Ssl 11:41 0:03 /opt/gitlab/embedded/bin/postgres_exporter -web.listen-address=localhost:9187495 13283 0.0 1.7 545204 34160 ? Ss 11:41 0:00 /opt/gitlab/embedded/bin/postgres -D /var/opt/gitlab/postgresql/data495 13287 0.0 1.0 545320 20344 ? Ss 11:41 0:02 postgres: checkpointer process 495 13288 0.0 0.2 545204 5032 ? Ss 11:41 0:00 postgres: writer process 495 13289 0.0 0.8 545204 16228 ? Ss 11:41 0:00 postgres: wal writer process 495 13290 0.0 0.1 545776 2196 ? Ss 11:41 0:00 postgres: autovacuum launcher process 495 13291 0.0 0.0 31012 1460 ? Ss 11:41 0:00 postgres: stats collector process 495 13332 0.0 0.4 644080 8408 ? Ss 11:41 0:01 postgres: gitlab-psql postgres [local] idle 495 14219 0.0 0.4 644128 8892 ? Ss 11:50 0:00 postgres: gitlab gitlabhq_production [local] idle 495 15207 0.0 0.4 643884 8160 ? Ss 12:00 0:00 postgres: gitlab gitlabhq_production [local] idle 495 15208 0.0 0.4 643888 8392 ? Ss 12:00 0:00 postgres: gitlab gitlabhq_production [local] idle 495 19681 0.0 0.3 643428 6804 ? Ss 12:46 0:00 postgres: gitlab gitlabhq_production [local] idle 495 19706 0.0 0.3 643428 6812 ? Ss 12:47 0:00 postgres: gitlab gitlabhq_production [local] idle 可以看到启动之后的postgresql有2个默认数据库,一个是postgres,一个是gitlabhq_production!进入postgresql控制台方法,在postgreSQL的安装目的bin下执行命令: 用法:psql database_name,例:/usr/local/pgsql/bin/psql mydb指定host、用户名和数据库的话,如:/usr/local/pgsql/bin/psql -h localhost -U postgres -d testdb注:-h 也可以指定socket文件目录#########################################进入postgresql的gitlabhq_production库#su - gitlab-psql-sh-4.1$ /opt/gitlab/embedded/bin/psql -h /var/opt/gitlab/postgresql -d gitlabhq_productionpsql (9.6.1)Type "help" for help.gitlabhq_production=# helpYou are using psql, the command-line interface to PostgreSQL.Type: \copyright for distribution terms \h for help with SQL commands \? for help with psql commands \g or terminate with semicolon to execute query \q to quitgitlabhq_production=# \q #退出注意:-h 后面指定服务器ip或者socket文件所在目录进入postpgresql的postgres库#su - gitlab-psql-sh-4.1$ psql -h /var/opt/gitlab/postgresql -d postgrespsql (9.6.1)Type "help" for help.postgres=# ##############################################postgresql的其他用法介绍创建数据库CREATE DATABASE test WITH OWNER = postgres ENCODING = 'UTF8';查看版本: #psql --version 或 SELECT version();查看所有数据库:\l选择数据库:\c databasename查看所有表:\dt查看某个表的结构:\d tablename退出psql控制台:\q查看所有索引:\di查看表的索引:select * from pg_indexes where tablename='log';查看数据库大小:select pg_size_pretty(pg_database_size('test_db'));查看所有数据库大小select pg_database.datname, pg_database_size(pg_database.datname) AS size from pg_database;查看表的大小:select pg_size_pretty(pg_relation_size('test_table'));查看表的总大小,包括索引大小 select pg_size_pretty(pg_total_relation_size('test')); 查看所有表空间 select spcname from pg_tablespace; 查看表空间大小select pg_size_pretty(pg_tablespace_size('pg_default'));导出备份数据库:pg_dump -h localhost -U postgres databasename > /tmp/databasename.bak.yyyymmdd.sql导入恢复数据库(sql文件是pg_dump导出的文件就行,可以是整个数据库,也可以只是单个表,也可以只是结构等):psql -h localhost -U postgres -d databasename < /tmp/databasename.bak.yyyymmdd.sql导出某个表:pg_dump -h localhost -U postgres -t tablename dbname > test.sql导出某个表的结构,加参数"-s":pg_dump -h localhost -U postgres -t tablename -s dbname > test_construct.sql导出某个表的数据,加参数"-a":pg_dump -h localhost -U postgres -t tablename -a dbname > test_data.sql查看序列:select * from information_schema.sequences where sequence_schema = 'public';
五、通过Git工具提交windows上的代码到GitLab上
说明:很多开发人员喜欢使用Git工具来提交代码到GitLab,那么这里就着重介绍下。
1、首先下载Git并安装
下载地址:https://git-for-windows.github.io 下载好之后双击Git-2.13.0-64-bit.exe安装即可。
注:一路Next,按照默认的选择即可!(我是这么干的,如果你自己有特殊需要,可以自定义)
2、打开Git并配置,如下图:
双击"Git Bash"会弹出一个shell 方框,如下:
好了,Git Bash也打开了,下面我们在GitLab上注册一个普通用户,然后提交代码。
3、在GitLab上注册用户,如下图:
注册成功之后,会看到一个提示注册成功的页面,提示:Welcome! You have signed up successfully.
因为是内部测试虚拟机,网络比较慢,所以注册普通账号之后,发送邮件有些延迟,还发到了垃圾邮箱中,如下图:
可以看到邮件内容是提示你添加pub key,按照下面的步骤添加即可!
4、创建一个项目
注册好之后,点击右上角的"+"图标(创建新项目图标)开始创建新项目,如下图:
点击"Create project"之后,会重新打开一个页面,如下:
可以根据提示得知,新用户创建的项目需要将SSH key添加进来!点击"add an SSH key"会出现一个新的页面,提示你需要将生成的pub key粘贴在里面,如下图:
好的,执行到这一步我们将视线转移到windows机器的Git工具,通过这个工具生成公钥,然后添加。
5、使用Git生成公钥并添加到GitLab
生成公钥命令:ssh-keygen -t rsa -C "邮箱" 然后一路回车即可!
根据上图的提示,生成的公钥在C:\Users\Administrator.2345-20170207QA\.ssh目录下面,然后找到打开id_rsa.pub将其中的字符串原封不动的复制到GitLab页面,如下图:
点击"Add key"会弹出一个页面,表示key添加成功,Title 会自动补全生成key时候输入的邮箱地址!
6、然后测试上传代码到GitLab的mytest项目
其实上传代码的一些命令在GitLab界面的新项目页面是给到我们的,我们按照上面的步骤操作即可,如下命令:
Git global setup 全局设置$git config --global user.name "new_user_name" #注册时候的用户名$git config --global user.email "生成pub key时候的邮箱地址"Create a new repository 创建一个新的库$git clone git@gitlab.example.com:new_user_name/mytest.git$cd mytest$touch README.md$git add README.md$git commit -m "add README"$git push -u origin master因为是新项目,没有任何其他文件,所以执行以上8条命令即可!!!#####################################################################Existing folder 已经存在的目录cd existing_foldergit initgit remote add origin git@gitlab.example.com:new_user_name/mytest.gitgit add .git commitgit push -u origin masterExisting Git repository 已经存在的一个库cd existing_repogit remote add origin git@gitlab.example.com:new_user_name/mytest.gitgit push -u origin --allgit push -u origin --tags
在Git Bash工具中执行上面列出的8条命令,如下图:
注:执行第三条命令的时候,报错了,提示不能解析gitlab.example.com 因为是内部测试机我没有做任何解析,所以报错了。那么我就在本机windows机器的hosts文件中解析一下,然后重新执行就ok了!
192.168.159.129 gitlab.example.com
执行第三条命令的目的是将gitlab上面我们创建的项目mytest拉到本地,我们看看是否拉取成功,如下图:
可以看到mytest/ 已经成功拉取到了windows机器上!
继续执行命令,在mytest/项目下创建一个README.md文件,并上传到gitlab上,如下图:
上传成功之后,我们到gitlab上mytest项目中查看上传的文件,如下图:
可以看到README.md文件已经成功上传到mytest/项目中了!
############################################
上面介绍的是上传本地代码到gitlab上,对开发人员来说项目名和代码都是英文的,便于维护。但对于系统工程师来说可能需要上传一些文档到gitlab上以便其他技术人员参考,所以下面我介绍一下上传中文目录和文件到gitlab的方法:首先在本机windows机器的项目目录(这里以mytest项目为例)下面创建一个中文目录,如下图:
然后在"测试中文目录"下面创建一个"测试1.txt"文件,然后通过Git Bash工具上传到git上,如下图
执行标红的2、3、4这3条命令即可!然后到gitlab上查看是否可以正常显示中文,如下图:
可以看到中文在gitlab上显示正常。刚才我只是在"测试中文目录"中创建了"测试1.txt"文件,没有在其中写入中文,现在我写入一行中文测试,内容是"这是为了测试gitlab是否可以正常显示中文!"然后再push到gitlab上,如下图:
执行完成之后,到gitlab上查看,如下图:
可以看到也是正常的!!!
注意:在gitlab上不能直接创建中文或者带有中文的项目名,会报错的提示如下:
Path can contain only letters, digits, '_', '-' and '.'. Cannot start with '-', end in '.git' or end in '.atom'
你可以创建一个英文项目名,在此项目下创建中文目录,保存中文文档。不过据说可以使用工具来实现创建中文项目名的目的,这个就不介绍了。
六、gitlab忘记密码如何找回
注:有些朋友密码比较多容易记混或者忘记,那么如果忘记密码,如何找回呢?
首先你在gitlab的登录界面点击忘记密码(Forgot your password)的连接,如下:
然后会弹出一个页面,如下:
输入注册用户时候留下的邮箱地址,这里就显示出了邮箱地址的重要性,然后点击"Reset password",在弹出的页面上会提示你已经发送了重置密码的邮件到你的邮箱,然后你到自己邮箱查看,如下图:
可以在垃圾箱中看到邮件,查看邮件并点击"Reset password"会弹出重置密码页面,你重新输入自己的新密码即可。然后弹出重新登录界面,输入用户名和新设置的密码,就可以顺利登录了!最后会发送一封邮件告知你密码已经成功修改,如下图:
不足之处,请多多留言指教!