git-jenkins阶段03 gitlab分支保护, 合并分支, Git-GUI使用, gitlab的备份与恢复, Jenkens安装及插件安装

1.分支保护

master不允许普通用户推送代码

要以管理员身份进行操作

#dev环境可以拉取master分支
[root@dev01 ~]# git clone git@10.0.0.81:OPS/git_data.git
Cloning into 'git_data'...
remote: Counting objects: 38, done.
remote: Compressing objects: 100% (29/29), done.
remote: Total 38 (delta 13), reused 0 (delta 0)
Receiving objects: 100% (38/38), done.
Resolving deltas: 100% (13/13), done.

#dev环境不能推送master分支
[root@dev01 git_data]# git push -u origin master
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 263 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: GitLab: You are not allowed to push code to protected branches on this project.
To git@10.0.0.81:OPS/git_data.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@10.0.0.81:OPS/git_data.git'

2.合并分支

#dev用户以dev01分支的权限推送代码
[root@dev01 git_data]# git branch dev01    #创建分支
[root@dev01 git_data]# git checkout dev01
Switched to branch 'dev01'
[root@dev01 git_data]# git branch
* dev01
  master
[root@dev01 git_data]# touch dev01.log
[root@dev01 git_data]# git add .
[root@dev01 git_data]# git commit -m "new file dev01.log"
[dev01 c9ba23f] new file dev01.log
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 dev01.log
[root@dev01 git_data]# git push -u origin dev01
Counting objects: 6, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 439 bytes | 0 bytes/s, done.
Total 5 (delta 2), reused 0 (delta 0)
remote: 
remote: To create a merge request for dev01, visit:
remote:   http://10.0.0.81/OPS/git_data/merge_requests/new?merge_request%5Bsource_branch%5D=dev01
remote: 
To git@10.0.0.81:OPS/git_data.git
 * [new branch]      dev01 -> dev01
Branch dev01 set up to track remote branch dev01 from origin.

#dev01用户登录gitlab,右上角会出现Create merge request按钮,点击

如图,填入合并分支标题和注释,点击合并。进行合并分支申请

#root用户进入gitlab,要以管理员的身份处理合并请求
左侧栏会看到 Merge Requests一栏显示1,说明有一个请求,点击

 如下图,可以看申请人,和合并请求,点击Merge即可同意合并

 

3.Git-GUI使用

windows上安装git

如下图,安装这两个,不用动

 如下图,选第二个,使用bash命令同时要能使用windows命令

 其他都不要更改,下一步到底就行(不需要点击,Enable file system caching)

#windows上安装git

#在gitlab上创建dev02,密码12345678 (表示windows开发的用户)

#进入git bash
>: ssh-keygen -t rsa    #创建密钥对
#用dev02登录gitlab, 把公钥添加到gitlab上

#在仓库目录终端下 - 设置全局用户
>: git config --global user.name dev02
>: git config --global user.email dev02@qq.com

右键进入Git GUI,拉取项目代码

如图,点击克隆已存在仓库

 输入,下载地址和放置路径如图下图

在下载的文件中创建windows_dev.txt
点击Git GUI中,Rescan,查看改变

如图

 点击Stage Changed,提交到暂存区

填写Commit Message注释,点击Commit,进行提交

点击Push,推到本地仓库(这里为了测试,gitlab上去除保护分支)

 

4.gitlab的备份与恢复

#注意:恢复,需要保证gitlab是一个版本

1.备份

#修改配置
[root@git ~]# vim /etc/gitlab/gitlab.rb
gitlab_rails['manage_backup_path'] = true    #开启备份
gitlab_rails['backup_path'] = "/data/backup/gitlab"  #备份目录
gitlab_rails['backup_archive_permissions'] = 0644  #备份文件的权限
gitlab_rails['backup_keep_time'] = 604800  #备份数据保留时间(秒),7天

#创建备份目录
[root@git ~]# mkdir -p /var/opt/gitlab/backups
#赋权,否则gitlab写不进去
[root@git ~]# chown -R git.git /data/backup/gitlab

#让其配置文件生效
[root@git ~]# gitlab-ctl reconfigure

#手动进行备份
[root@git ~]# gitlab-rake gitlab:backup:create

#检查备份结果
[root@git ~]# ll /data/backup/gitlab
total 600
-rw-r--r-- 1 git git 614400 Jun 12 04:41 1718138475_2024_06_12_10.2.2_gitlab_backup.tar
#名称: 时间戳 日期 gitlab版本

#误删除数据,进行恢复操作
[root@git git_data]# git rm -f *
rm 'aaa.txt'
rm 'bbb.txt'
...
[root@git git_data]# git add .
[root@git git_data]# git commit -m "delete"
[master bb87b2b] delete
 11 files changed, 9 deletions(-)
 delete mode 100644 aaa.txt
...
[root@git git_data]# git push -u origin master

#恢复数据
#停服务(只要停队列和动态web程序就可以了,不用全部都停)
[root@git git_data]# gitlab-ctl stop unicorn
ok: down: unicorn: 0s, normally up
[root@git git_data]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up
[root@git git_data]# gitlab-ctl status
run: gitaly: (pid 5866) 34732s; run: log: (pid 5865) 34732s
run: gitlab-monitor: (pid 5857) 34732s; run: log: (pid 5856) 34732s
run: gitlab-workhorse: (pid 5864) 34732s; run: log: (pid 5863) 34732s
run: logrotate: (pid 64507) 2332s; run: log: (pid 5848) 34732s
run: nginx: (pid 5840) 34732s; run: log: (pid 5839) 34732s
run: node-exporter: (pid 5858) 34732s; run: log: (pid 5855) 34732s
run: postgres-exporter: (pid 5871) 34732s; run: log: (pid 5868) 34732s
run: postgresql: (pid 5867) 34732s; run: log: (pid 5847) 34732s
run: prometheus: (pid 5872) 34732s; run: log: (pid 5859) 34732s
run: redis: (pid 5870) 34732s; run: log: (pid 5862) 34732s
run: redis-exporter: (pid 5861) 34732s; run: log: (pid 5860) 34732s
down: sidekiq: 9s, normally up; run: log: (pid 5846) 34732s
down: unicorn: 23s, normally up; run: log: (pid 5851) 34732s

#按照指定备份文件恢复数据 (BACKUP需要的参数是备份文件前面时间到版本部分)
[root@git git_data]# gitlab-rake gitlab:backup:restore BACKUP=1718138475_2024_06_12_10.2.2
Unpacking backup ... done
Before restoring the database, we will remove all existing
tables to avoid future upgrade problems. Be aware that if you have
custom tables in the GitLab database these tables and all data will be
removed.
Do you want to continue (yes/no)? yes
This will rebuild an authorized_keys file.
You will lose any data stored in authorized_keys file.
Do you want to continue (yes/no)?yes

#启动服务
[root@git git_data]# gitlab-ctl start

#检查数据是否恢复

5.Jenkens

#官网
https://www.jenkins.io/zh/

准备环境
一台服务器    内存 >2G

#安装包文件参考地址:
https://www.cnblogs.com/yanjieli/p/10606674.html

[root@jenkins ~]# rz
jdk-8u181-linux-x64.rpm
jenkins-2.99-1.1.noarch.rpm
plugins.tar.gz

#安装jdk
[root@jenkins ~]# rpm -ivh jdk-8u181-linux-x64.rpm

#安装Jenkins
[root@jenkins ~]# rpm -ivh jenkins-2.99-1.1.noarch.rpm

#修改jenkins配置,修改其运行用户
[root@jenkins ~]# vim /etc/sysconfig/jenkins
JENKINS_USER="root"        #默认Jenkins用户不好用,很多不足,直接用root用户

#启动Jenkins,8080是java运行jenkins的端口
[root@jenkins ~]# systemctl start jenkins
[root@jenkins ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      5778/rpcbind        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      6863/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7260/master         
tcp6       0      0 :::111                  :::*                    LISTEN      5778/rpcbind        
tcp6       0      0 :::8080                 :::*                    LISTEN      8745/java           
tcp6       0      0 :::22                   :::*                    LISTEN      6863/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      7260/master

#浏览器登录Jenkins(第一次访问会远程连接下载更新,如果网不好,卡住了,拔掉网线等一会就好了,再插上网线)
http://10.0.0.83:8080/

#根据页面提醒,读取密码,在页面输入密码
[root@jenkins ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
dd8d5fbacf924b02b5a0fe01efb375fa

 跳过插件安装,如下图

 点击 Start using Jenkins,进入Jenkins,如下图

 点击设置,修改密码,如下图

 账号admin,密码123, 点击保存。下次登录,直接输入该用户,密码即可

 

6.插件安装

#在页面左侧系统管理,管理插件。可以在这里安装插件

#解压,这是企业中用的比较多的插件
[root@jenkins ~]# tar xf jenkins-plugins.tar.gz
[root@jenkins ~]# ll plugins |wc -l
191
#进入Jenkins目录下
[root@jenkins ~]# cd /var/lib/jenkins
[root@jenkins jenkins]# ll plugins/    参看jenkins当前插件数量
total 0
#把插件都移动到Jenkins插件文件夹内
[root@jenkins jenkins]# mv /root/plugins/* ./plugins/

#重启Jenkins服务,插件就生效了
[root@jenkins plugins]# systemctl restart jenkins.service

jenkins重要的目录文件

#存放Jenkins的安装程序目录,所有的war包都在里面
[root@jenkins ~]# ll /usr/lib/jenkins/
total 72584
-rw-r--r-- 1 root root 74325146 Jan  1  2018 jenkins.war

#Jenkins的配置文件
[root@jenkins ~]# ll /etc/sysconfig/jenkins 
-rw------- 1 root root 3113 Jun 12 07:52 /etc/sysconfig/jenkins

#默认Jenkins的家目录
[root@jenkins ~]# ll /var/lib/jenkins -d
drwxr-xr-x 10 jenkins jenkins 4096 Jun 12 21:39 /var/lib/jenkins

#jenkins的日志
[root@jenkins ~]# ll /var/log/jenkins/jenkins.log 
-rw-r--r-- 1 root root 36619 Jun 12 21:40 /var/log/jenkins/jenkins.log

 

posted @ 2024-06-12 14:16  战斗小人  阅读(30)  评论(0编辑  收藏  举报