美文网首页
gitlab搭建及其使用方法(开发用的)

gitlab搭建及其使用方法(开发用的)

作者: linux_python | 来源:发表于2020-05-19 17:13 被阅读0次

    CICD流程图详解

    image.png

    代码管理仓库gitlab

    gitlab是个私有的代码管理仓库, 可以运行在企业内部的网络中, 使企业开发人员可以保持代码的私有性; 同时也方便自行管理代码; gitlab有很多CI功能, 但是通常还是采用Jenkins, 原因就是Jenkins的功能要更加强大; 下面看一看gitlab的部署过程及操作流程;

    gitlab version: 12.5下载链接; 右键点击即可下载;

    gitlab部署步骤:

    $ sudo yum -y install epel-release
    $ sudo yum -y install gitlab-ce-12.5.3-ce.0.el7.x86_64.rpm
    

    gitlab配置步骤:

    $ sudo vim /etc/gitlab/gitlab.rb
    external_url 'http://gitlab.cicds.com'  -->> 更改为自定义域名
    $ sudo gitlab-ctl reconfigure
    ...
    ...
    ...
    Running handlers:
    Running handlers complete
    Chef Client finished, 543/1455 resources updated in 02 minutes 57 seconds
    gitlab Reconfigured!
    

    -->>打开浏览器访问http://gitlab.cicds.com即可;

    gitlab的控制命令:

    # 修改配置后生效操作
    $ sudo gitlab-ctl reconfigure
    # 查看gitlab服务状态
    $ sudo gitlab-ctl status
    # 开启/关闭/重启服务
    $ sudo gitlab-ctl start/stop/restart
    # 查看gitlab日志
    $ sudo gitlab-ctl tail [component]
    

    从开发者角度操作gitlab

    开发者对于gitlab的操作集中在创建项目,添加用户,配置项目, 本地环境, gitlab连接, 拉取代码, 推送代码等操作; 通常都是由运维人员创建相应的分组或者相应的项目, 再去设置相应的分组负责人及项目负责人(owner)后, 负责人添加开发人员(developer)至组中或项目中就可以开始开发工作了;

    • 创建组的操作


      image.png

      在项目中添加负责人(owner)


      image.png
    • 负责人添加开发者(developer)
    image.png
    负责人创建项目 image.png

    创建项目成功后可以添加sshkey至用户中方便以后拉取代码及上传代码

    [ziyins@gitlab ~]$ ssh-keygen -t rsa -b 4096 -N "" -f $HOME/.ssh/id_rsa -C ziyins@163.com
    Generating public/private rsa key pair.
    Created directory '/home/ziyins/.ssh'.
    Your identification has been saved in /home/ziyins/.ssh/id_rsa.
    Your public key has been saved in /home/ziyins/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:nNVDMz7XfdbmXL2NQ6qdt8Byh8Xc5RYEDvVyT/LISpQ ziyins@163.com
    The key's randomart image is:
    +---[RSA 4096]----+
    |           .=o.. |
    |           +o+o.+|
    |          . Eo++%|
    |       . o . Xo&O|
    |        S   o O.X|
    |           = = o |
    |          o O o  |
    |           o + . |
    |              .  |
    +----[SHA256]-----+
    
    [ziyins@gitlab ~]$ cat $HOME/.ssh/id_rsa.pub
    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC58dlDzXyjVliHBR0LLZgHy72LoMM8mSXLxYJbuCA7YvqfiHEBXri4NF1TBAjRQdnwTB1VwOYdNWb0wOfkLYFElkn6ZK0tqQsxIpartx7Dyhig/qY78e6sklpDYafh0AdJEEuYm2LCY2GT/v27at7G5oPVCW9nBKMcr1iPbbxF04mZcXVRZ80QmPb3TZH1o9qu7l//KE2a8kBG6gMTzOLAtau3qZJLleVOQ2SRVn5lH/FAZxDkTwOFf7+VsG05TzXhks3/tXXTKdj5mNsCQ0Lul6tCXUy3Vwy3PFsX+ajyKmrfidfV0cA8kXxQORo3H7PiZfCnM3Op2MAQ0pFU8GE2Ao6+QbLGLj4/WqM/kCegzX8sw8/nymFkCxdEGV6QPW62NHMyXmhKuJxvanthEZ6Q1oxNVZb5niNMGlX7GlQt9sziumwsvRQ153RymHnQ6UxEx/M/ZlABhsB+Wqs8tb0Y31bL4IpwMEYps1wscxQV5mvi2/gP3EXoG+ejOqgVxwlTYhD6lSt8XmUmW+bnVk9jCdjx+n9bQJwvP4it74WYGNNtTr5oFgb9r7q8b9Gmpe8HUmXv9woczf3NQVHeAcM4tC4+DVDAIJeTy4oILiGihe0zzP27VM+wqPnbGJKlFXNc3p8bzNAnu1PFPvxs11U3mow6ACwvHtQum8dBg+Y+ww== ziyins@163.com
    
    image.png

    其余developer角色用户仿照owner用户添加sshkey即可;

    • 开发者(developer)如何进行开发? 拉取代码, 上传代码, 创建分支, 提交合并请求;
    # owner创建项目描述, 随后通知开发人员进行分支开发
    [ziyins@gitlab ~]$ git clone git@gitlab.cicds.com:videosclient/clientide.git
    Cloning into 'clientide'...
    The authenticity of host 'gitlab.cicds.com (192.168.123.11)' can't be established.
    ECDSA key fingerprint is SHA256:eVRgfOwZaEj9VCiHFe8/PqeyDEBWqxqdo7vsLtRJ+Uw.
    ECDSA key fingerprint is MD5:21:fb:93:5f:2b:ca:17:b7:e6:53:85:8c:26:b6:79:d8.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'gitlab.cicds.com,192.168.123.11' (ECDSA) to the list of known hosts.
    warning: You appear to have cloned an empty repository.
    [ziyins@gitlab ~]$
    [ziyins@gitlab ~]$ ls
    clientide
    [ziyins@gitlab ~]$ cd clientide/
    [ziyins@gitlab clientide]$ vim README.md
    [ziyins@gitlab clientide]$ git add README.md
    [ziyins@gitlab clientide]$ git commit -m "Add README.md"
    
    *** Please tell me who you are.
    
    Run
    
      git config --global user.email "you@example.com"
      git config --global user.name "Your Name"
    
    to set your account's default identity.
    Omit --global to set the identity only in this repository.
    
    fatal: empty ident name (for <ziyins@gitlab.(none)>) not allowed
    [ziyins@gitlab clientide]$ git config --global user.email "ziyins@163.com"
    [ziyins@gitlab clientide]$ git config --global user.name "ziyins"
    [ziyins@gitlab clientide]$ git commit -m "Add README.md"
    [master (root-commit) e63eac5] Add README.md
     1 file changed, 3 insertions(+)
     create mode 100644 README.md
    [ziyins@gitlab clientide]$ git push -u origin master
    Counting objects: 3, done.
    Writing objects: 100% (3/3), 259 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To git@gitlab.cicds.com:videosclient/clientide.git
     * [new branch]      master -> master
    Branch master set up to track remote branch master from origin.
    
    # 开发者克隆库到本地, 创建分支进行开发
    [bavduer@gitlab ~]$ git clone git@gitlab.cicds.com:videosclient/clientide.git
    Cloning into 'clientide'...
    remote: Enumerating objects: 3, done.
    remote: Counting objects: 100% (3/3), done.
    remote: Total 3 (delta 0), reused 0 (delta 0)
    Receiving objects: 100% (3/3), done.
    [bavduer@gitlab ~]$ cd clientide
    [bavduer@gitlab clientide]$ ls
    README.md
    [bavduer@gitlab clientide]$ git branch
    * master
    [bavduer@gitlab clientide]$ git checkout -b interface
    Switched to a new branch 'interface'
    [bavduer@gitlab clientide]$ git branch
    * interface
      master
    [bavduer@gitlab clientide]$ vim ide_interface.py
    [bavduer@gitlab clientide]$ git add ide_interface.py
    [bavduer@gitlab clientide]$ git config --global user.email "bavduer@163.com"
    [bavduer@gitlab clientide]$ git config --global user.name "bavduer"
    [bavduer@gitlab clientide]$ git commit -m "Add ide_interface.py"
    [interface 189c405] Add ide_interface.py
     1 file changed, 7 insertions(+)
     create mode 100644 ide_interface.py
    [bavduer@gitlab clientide]$
    [bavduer@gitlab clientide]$ git push -u origin interface
    Counting objects: 4, done.
    Delta compression using up to 2 threads.
    Compressing objects: 100% (3/3), done.
    Writing objects: 100% (3/3), 384 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    remote:
    remote: To create a merge request for interface, visit:
    remote:   http://gitlab.cicds.com/videosclient/clientide/merge_requests/new?merge_request%5Bsource_branch%5D=interface
    remote:
    To git@gitlab.cicds.com:videosclient/clientide.git
     * [new branch]      interface -> interface
    Branch interface set up to track remote branch interface from origin.
    [bavduer@gitlab clientide]$
    
    • 开发者要求合并到master分支时可以创建一个merge request请求, 由负责人(owner), 审查通过后, 即可合并到主分支;
    • git命令行其他常用操作
    # 开发人员经常会误删除一些文件或者改动文件中的内容, 此时想要回退到原来的文件内容时,很是抓狂,所以有了版本回退
    [bavduer@gitlab clientide]$ git branch
    * interface
      master
    [bavduer@gitlab clientide]$
    [bavduer@gitlab clientide]$ kls
    -bash: kls: command not found
    [bavduer@gitlab clientide]$ ls
    ide_interface.py  README.md
    [bavduer@gitlab clientide]$
    [bavduer@gitlab clientide]$ vim ide_interface.py
    ->>>添加一行内容后保存退出vim编辑器
    
    [bavduer@gitlab clientide]$ git add --all
    [bavduer@gitlab clientide]$ git commit -m "add content code"
    [interface c635131] add content code
     1 file changed, 1 insertion(+)
    [bavduer@gitlab clientide]$ git log
    commit c635131fb240b8fe179722e553769542cad165b7
    Author: bavduer <bavduer@163.com>
    Date:   Sun Dec 8 13:09:36 2019 +0800
    
        add content code
    
    commit 189c405736956e07119d646ade595c8d4b4dc752
    Author: bavduer <bavduer@163.com>
    Date:   Sun Dec 8 12:08:24 2019 +0800
    
        Add ide_interface.py
    
    commit e63eac5d99026b09d7357d87f58796daeafcf4b7
    Author: ziyins <ziyins@163.com>
    Date:   Sun Dec 8 11:58:10 2019 +0800
    
        Add README.md
    [bavduer@gitlab clientide]$ git reset --hard 189c405
    HEAD is now at 189c405 Add ide_interface.py
    [bavduer@gitlab clientide]$ cat ide_interface.py
    #!/usr/local/bin/python3
    #
    # author: bavduer
    # time: 2019/12/08
    # filepath: ide_interface.py
    
    print("hello world")
    [bavduer@gitlab clientide]$
    
    # 更新gitlab仓库中的代码, 到本地
    [bavduer@gitlab clientide]$ git branch
      interface
    * master
    [bavduer@gitlab clientide]$ git pull
    remote: Enumerating objects: 5, done.
    remote: Counting objects: 100% (5/5), done.
    remote: Compressing objects: 100% (4/4), done.
    remote: Total 4 (delta 1), reused 0 (delta 0)
    Unpacking objects: 100% (4/4), done.
    From gitlab.cicds.com:videosclient/clientide
       e63eac5..21e8ba4  master     -> origin/master
    Updating e63eac5..21e8ba4
    Fast-forward
     ide_interface.py | 7 +++++++
     1 file changed, 7 insertions(+)
     create mode 100644 ide_interface.py
    

    从运维人员角度操作gitlab

    备份和还原gitlab仓库是很重要的操作, 必要时可以避免很多不必要的损失;

    gitlab的备份操作及备份恢复

    ##备份<设置备份目录及保存天数>
    [root@gitlab ~]# vim /etc/gitlab/gitlab.rb
    gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"
    gitlab_rails['backup_keep_time'] = 604800
    [root@gitlab ~]# gitlab-rake gitlab:backup:create
    
    ##恢复
    [root@gitlab ~]# cd /var/opt/gitlab/backups
    [root@gitlab backups]# chmod 777 1541872172_2018_11_11_11.4.5_gitlab_backup.tar
    [root@gitlab backups]# gitlab-ctl stop unicorn
    ok: down: unicorn: 0s, normally up
    [root@gitlab backups]# gitlab-ctl stop sidekiq
    ok: down: sidekiq: 0s, normally up
    [root@gitlab backups]# gitlab-rake gitlab:backup:restore BACKUP=<备份编号>
    [root@gitlab backups]# gitlab-ctl reconfigure
    

    相关文章

      网友评论

          本文标题:gitlab搭建及其使用方法(开发用的)

          本文链接:https://www.haomeiwen.com/subject/sivbgctx.html