python:git及gitlab服务器部署

Git简介
• Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。
• Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。
• Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不需要服务器端软件支持。

git工作流程
在这里插入图片描述

工作区、暂存区和版本库
• 工作区:就是你在电脑里能看到的目录
• 暂存区:英文叫stage, 或index。一般存放在 “.git目录下” 下的index文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)
• 版本库:工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库
在这里插入图片描述
准备一台虚拟机(192.168.8.110),需要可以访问互联网(NAT)

[root@localhost ~]# ifconfig virbr0 down
[root@localhost ~]# brctl delbr virbr0
[root@localhost ~]# ifdown eth0; ifup eth0
[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=54 time=14.0 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=54 time=13.1 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=3 ttl=54 time=62.3 ms
^C

git安装及配置

[root@localhost ~]# yum -y install git
[root@localhost ~]# git config --global user.name 'liuxe1990'
[root@localhost ~]# git config --global user.email 'liuxe1990@163.com'
[root@localhost ~]# git config --global core.editor vim
[root@localhost ~]# git config --list
user.name=liuxe1990
user.email=liuxe1990@163.com
core.editor=vim
[root@localhost ~]# cat ~/.gitconfig
[user]
	name = liuxe1990
	email = liuxe1990@163.com
[core]
	editor = vim

初始化

  • 新建项目时计划使用git
[root@localhost ~]# git init myproject
初始化空的 Git 版本库于 /root/myproject/.git/
[root@localhost ~]# ls -A myproject/
.git
  • 在已经存在的项目中使用git
[root@localhost ~]# mkdir devops
[root@localhost ~]# cd devops/
[root@localhost devops]# echo '<h1>hello world!</h1>' > index.html
[root@localhost devops]# git init .
初始化空的 Git 版本库于 /root/devops/.git/
[root@localhost devops]# ls -A
.git  index.html

git应用

[root@localhost devops]# git status		//查看状态
# 位于分支 master
#
# 初始提交
#
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#	index.html
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@localhost devops]# git status -s		//简要信息
?? index.html
[root@localhost devops]# git add .		//将目录下所有内容加入暂存区,开始跟踪
[root@localhost devops]# git status
# 位于分支 master
#
# 初始提交
#
# 要提交的变更:
#   (使用 "git rm --cached <file>..." 撤出暂存区)
#
#	新文件:    index.html
#
[root@localhost devops]# git status -s
A  index.html
[root@localhost devops]# git rm --cached index.html    //撤出暂存区
rm 'index.html'
[root@localhost devops]# git status
# 位于分支 master
#
# 初始提交
#
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#	index.html
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@localhost devops]# git status -s
?? index.html
[root@localhost devops]# git add .		//重新加入暂存区
[root@localhost devops]# git status -s
A  index.html
[root@localhost devops]# git commit		//确认至版本库,需要写日志
[master(根提交) b951f4a] myproject
 1 file changed, 1 insertion(+)
 create mode 100644 index.html
[root@localhost devops]# git status
# 位于分支 master
无文件要提交,干净的工作区
[root@localhost devops]# git status -s

# 接下来的常规则应用,就是修改代码、加入跟踪、确认至版本库
[root@localhost devops]# echo '<h2>nice to meet you</h2>' >> index.html 
[root@localhost devops]# cp /etc/hosts .
[root@localhost devops]# git status -s
 M index.html
?? hosts
[root@localhost devops]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
#	修改:      index.html
#
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#	hosts
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@localhost devops]# git add .
[root@localhost devops]# git status -s
A  hosts
M  index.html
[root@localhost devops]# git status 
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#	新文件:    hosts
#	修改:      index.html
#
[root@localhost devops]# git commit -m "modify index.html, add hosts"		//添加注释(“”内内容)并将暂存区中的所有修改提交到本地仓库
[master 666d37a] modify index.html, add hosts
 2 files changed, 3 insertions(+)
 create mode 100644 hosts
[root@localhost devops]# git status
# 位于分支 master
无文件要提交,干净的工作区

恢复误删除的文件

[root@localhost devops]# rm -rf *
[root@localhost devops]# ls
[root@localhost devops]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add/rm <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
#	删除:      hosts
#	删除:      index.html
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@localhost devops]# git checkout -- *		//恢复删除的文件
[root@localhost devops]# ls
hosts  index.html

如果真想删除文件应该采用以下方式:

[root@localhost devops]# git rm hosts
rm 'hosts'
[root@localhost devops]# ls
index.html
[root@localhost devops]# git status -s
D  hosts
[root@localhost devops]# git status
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#	删除:      hosts
#
[root@localhost devops]# git reset HEAD hosts
重置后撤出暂存区的变更:
D	hosts
[root@localhost devops]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add/rm <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
#	删除:      hosts
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@localhost devops]# git checkout -- hosts
[root@localhost devops]# ls
hosts  index.html

删除文件的完整过程

[root@localhost devops]# git rm hosts
rm 'hosts'
[root@localhost devops]# git status -s
D  hosts
[root@localhost devops]# git commit -m "del hosts"
[master 7b9c7ba] del hosts
 1 file changed, 2 deletions(-)
 delete mode 100644 hosts

改名、移动

[root@localhost devops]# cp /etc/passwd .
[root@localhost devops]# ls
index.html  passwd
[root@localhost devops]# git add .
[root@localhost devops]# git commit -m "add passwd"
[master 5e83b42] add passwd
 1 file changed, 40 insertions(+)
 create mode 100644 passwd
[root@localhost devops]# git mv passwd mima
[root@localhost devops]# git status -s
R  passwd -> mima
[root@localhost devops]# git commit -m "rename passwd -> mima"
[master acc5fef] rename passwd -> mima
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename passwd => mima (100%)
 [root@localhost devops]# ls
index.html  mima
[root@localhost devops]# git status
# 位于分支 master
无文件要提交,干净的工作区

将HEAD指针指向以前的某个提交就可以切换到以前的某个状态

[root@localhost devops]# git log		//查看所有提交
commit acc5fef62c2cf2001c168a6d80efa49596db43d0
Author: liuxe1990 <liuxe1990@163.com>
Date:   Tue May 21 14:55:30 2019 +0800

    rename passwd -> mima

commit 5e83b42744978a7400c2e5a59f8c1a03e7e4f974
Author: liuxe1990 <liuxe1990@163.com>
Date:   Tue May 21 14:54:13 2019 +0800

    add passwd

commit 7b9c7bacdf6c8a8a477d299e2fa93bc968954071
Author: liuxe1990 <liuxe1990@163.com>
Date:   Tue May 21 14:46:00 2019 +0800

    del hosts

commit 666d37a0add3ca459ab642ddd434cc7fdbfb1115
Author: liuxe1990 <liuxe1990@163.com>
Date:   Tue May 21 14:36:54 2019 +0800

    modify index.html, add hosts
[root@localhost devops]# git checkout 666d37a0add3ca459ab642ddd434cc7fdbfb1115		//切换至以前某一版本
[root@localhost devops]# ls		//查看切换至某版本后git仓库文件
hosts  index.html
[root@localhost devops]# git  checkout master		//返回最新的提交
之前的 HEAD 位置是 666d37a... modify index.html, add hosts
切换到分支 'master'
[root@localhost devops]# ls
index.html  mima

查看分支

[root@localhost devops]# git branch
* master
[root@localhost devops]# ls
index.html  mima

新建分支

[root@localhost devops]# git branch fn1
[root@localhost devops]# git branch
  fn1
* master				//*表示所在分支

在fn1分支中编写程序

[root@localhost devops]# git checkout fn1
切换到分支 'fn1'
[root@localhost devops]# git branch
* fn1
  master

在fn1分支中编写程序

[root@localhost devops]# cp ~/anaconda-ks.cfg .
[root@localhost devops]# git add .
[root@localhost devops]# git commit -m "fn1 add anaconda"
[fn1 f0cbaae] fn1 add anaconda
 1 file changed, 65 insertions(+)
 create mode 100644 anaconda-ks.cfg
[root@localhost devops]# ls			//查看fn1分支文件
anaconda-ks.cfg  index.html  mima

切换回master分支

[root@localhost devops]# git checkout master
切换到分支 'master'
[root@localhost devops]# ls		//主干并不存在fn1分支编写的程序anaconda-ks.cfg 
index.html  mima

合并fn1分支到主干

[root@localhost devops]# git merge fn1
更新 acc5fef..f0cbaae
Fast-forward
 anaconda-ks.cfg | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
 create mode 100644 anaconda-ks.cfg
[root@localhost devops]# ls		//fn1分支编写的程序合并到主干
anaconda-ks.cfg  index.html  mima

fn1分支使命已经达成,可以删除

[root@localhost devops]# git branch -d fn1
已删除分支 fn1(曾为 f0cbaae)。
[root@localhost devops]# git branch
* master

#################################################################
gitlab服务器
为了实现多人共享代码,可以将git内容上传到gitlab服务器。
准备一台虚拟机(gitlab:192.168.122.124),要求可以访问外网,内存4G以上。安装docker

[root@room9pc01 ~]# ls /var/ftp/docker/		//配置宿主机(192.168.122.1)为docker网络yum源
docker-engine-1.12.1-1.el7.centos.x86_64.rpm          docker_images.zip  repodata
docker-engine-selinux-1.12.1-1.el7.centos.noarch.rpm  docker_uname.txt
[root@gitlab ~]# yum -y install docker-engine		//安装docker
[root@gitlab ~]# systemctl start docker		//启动docker
[root@gitlab ~]# systemctl enable docker
[root@gitlab ~]# docker load < gitlab_zh.tar		//导入gitlab镜像
[root@gitlab ~]# vim /etc/ssh/sshd_config		//修改docker宿主机的ssh端口为2022
17 Port 2022
[root@gitlab ~]# systemctl restart sshd
[root@room9pc01 ~]# ssh -p2022 192.168.122.124		//测试宿主机ssh连接

#启动gitlab容器,-h 容器主机名,--name 容器名称,-p 开放端口,--restart 出问题总是重启,-v 映射本地目录到容器
[root@gitlab ~]# docker run -d -h gitlab --name gitlab \
>-p 443:443 -p 80:80 -p 22:22 --restart always \
>-v /srv/gitlab/config:/etc/gitlab -v /srv/gitlab/logs:/var/log/gitlab \
>-v /srv/gitlab/data:/var/opt/gitlab gitlab_zh:latest
[root@gitlab ~]# docker ps		//当状态是healthy才能正常工作

配置gitlab服务器
访问http://192.168.122.124,初始化密码必须是8位以上,复杂。登陆时的用户名是root。

gitlab中重要的概念

  • 群组group:对应成一个部门、开发团队
  • 成员:对应用户
  • 项目:对应软件项目

1、新建组,名为devops。群组等级为公开。
2、为devops组中的成员创建用户zzg。新建用户的时候,不能创建密码。用户建立好之后,修改用户,可以为其加密码http://192.168.122.124/admin/users。
3、新建项目devops。新建的用户zzg是新项目的主程序员。可见等级为公开。项目创建完成后,点击左下角的“折叠边栏”=>“设置”=>“成员”=>邀请上一步创建的用户,角色是“主程序员”。

新建的用户上传代码
上传代码有两种方式,一种是http的方式,这种方式,每次上传代码都需要填写用户名和密码。另一种是通过ssh实现免密登陆
ssh免密登陆需要将用户的公钥上传:

[root@localhost devops]# ssh-keygen -t rsa -C "project01@baidu.cn" -b 4096		//生成密钥
[root@localhost devops]# cat ~/.ssh/id_rsa.pub		//查看生成的公钥

在用户的设置页面中,点击左侧的ssh密钥,把公钥内容粘贴进去。

[root@localhost devops]# git remote rename origin old-origin		//如果出现以下报错,忽略
error: 不能重命名配置小节 'remote.origin' 到 'remote.old-origin'
[root@localhost devops]# git remote add origin  git@192.168.122.124:devops/devops.git
[root@localhost devops]# git push -u origin --all
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值