1. 程式人生 > >版本管理之gitlab實踐教程:基礎篇(14)

版本管理之gitlab實踐教程:基礎篇(14)

這篇文章整理一下對gitlab/github進行clone或者push操作時,不需要每次提交都輸入使用者名稱/密碼的幾種方式。

場景

何時需要輸入使用者名稱/密碼

gitlab可以建立private/protected/public的倉庫,private的倉庫,在git clone執行時會提示輸入使用者名稱/密碼,以確認此使用者是否有許可權。

何時希望不輸入使用者名稱/密碼

互動方式需要中斷程式執行,如果此項操作是在CI整合的指令碼中被自動呼叫,往往希望能夠提供自動互動方式。

解決方式

方式1: SSH設定

使用場景:
遠端倉庫為gitlab,客戶端使用git,通過使用ssh-key的設定,來實現對private倉庫的git clone的免密碼。

方式2: 活用git的多種方式中包含使用者名稱/密碼的方式

使用場景
使用git clone http://使用者名稱:密碼@機器名:port/groupname/project.git方式進行操作,因為密碼和使用者名稱已經提供,自然不必再輸入

方式3: 設定.git-credentials

使用場景:
通過設定不同層次的.git-credentials來設定使用者名稱/密碼

方式4: 設定config

使用場景:
通過設定不同層次的config檔案來設定使用者名稱/密碼

方式5: 在remote中加入使用者名稱/密碼

使用場景:
通過對git的remote進行設定使其包含使用者名稱/密碼來實現git push等操作時無需再次輸入使用者名稱密碼

事前準備

以下使用rest api方式進行準備,如果使用gitlab的介面操作,登入進去建立一個private的倉庫即可。

獲取使用token

gitlab相關資訊如下:

設定 詳細
gitlab版本 8.12.9
實驗用Token 9-9rh18mQz4h3eDMQf5M
[root@platform tmp]# curl http://192.168.163.118:32001/api/v3/session --data 'login=root&password=12345678' 2>/dev/null |jq . |grep token 
"private_token": "9-9rh18mQz4h3eDMQf5M" [root@platform tmp]#

建立專案

[[email protected] ~]# curl --request POST --header "PRIVATE-TOKEN: 9-9rh18mQz4h3eDMQf5M" --data "name=demotest" "http://192.168.163.118:32001/api/v3/projects" |jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1444  100  1431  100    13    671      6  0:00:02  0:00:02 --:--:--   671
{
  "id": 2,
  "description": null,
  "default_branch": null,
  "tag_list": [],
  "public": false,
  "archived": false,
  "visibility_level": 0,
  "ssh_url_to_repo": "[email protected]:root/demotest.git",
  "http_url_to_repo": "http://1c79a0cf3ed8/root/demotest.git",
  "web_url": "http://1c79a0cf3ed8/root/demotest",
  "owner": {
    "name": "Administrator",
    "username": "root",
    "id": 1,
    "state": "active",
    "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
    "web_url": "http://1c79a0cf3ed8/u/root"
  },
  "name": "demotest",
  "name_with_namespace": "Administrator / demotest",
  "path": "demotest",
  "path_with_namespace": "root/demotest",
  "container_registry_enabled": true,
  "issues_enabled": true,
  "merge_requests_enabled": true,
  "wiki_enabled": true,
  "builds_enabled": true,
  "snippets_enabled": true,
  "created_at": "2018-07-30T08:57:42.975Z",
  "last_activity_at": "2018-07-30T08:57:42.975Z",
  "shared_runners_enabled": true,
  "lfs_enabled": true,
  "creator_id": 1,
  "namespace": {
    "id": 1,
    "name": "root",
    "path": "root",
    "owner_id": 1,
    "created_at": "2018-07-29T22:13:42.122Z",
    "updated_at": "2018-07-29T22:13:42.122Z",
    "description": "",
    "avatar": null,
    "share_with_group_lock": false,
    "visibility_level": 20,
    "request_access_enabled": true,
    "deleted_at": null,
    "lfs_enabled": null
  },
  "avatar_url": null,
  "star_count": 0,
  "forks_count": 0,
  "open_issues_count": 0,
  "runners_token": "RJVU4HPv6ug-bkPNzKyh",
  "public_builds": true,
  "shared_with_groups": [],
  "only_allow_merge_if_build_succeeds": false,
  "request_access_enabled": true
}
[[email protected] ~]#

或者直接在gitlab上建立一個私有的倉庫
這裡寫圖片描述

現象

因為是私有倉庫,預設設定下,git clone需要輸入使用者名稱/密碼

[[email protected] ~]# git clone http://192.168.163.118:32001/root/demotest.git
Cloning into 'demotest'...
Username for 'http://192.168.163.118:32001':

設定

方式1: SSH設定

使用場景:
遠端倉庫為gitlab,客戶端使用git,通過使用ssh-key的設定,來實現對private倉庫的git clone的免密碼。

設定客戶端ssh-key

使用ssh-keygen生成預設的rsa方式的密碼對

[[email protected] ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:z4LsUjifKSVJD2mZhTIfv1rPyAOD2yN+zX7A9OUBQto [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|     o.          |
|  o ooo .        |
|   +.BE. .       |
|    O o   o      |
|   + B oSo .     |
|  . B.O..o.      |
|   o #oB. o      |
|  o B.X +.       |
| ..o =oo         |
+----[SHA256]-----+
[[email protected] ~]#

將生成的rsa公鑰id_rsa.pub,新增到gitlab中

[root@platform ~]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDgF7YowO3D+ow6PQ54iu5LU93NlZ19sQlKnbH1dp+pKb/hNh7hnj4aVkDtrTrFnYBULGgmQU8Z1hd9zZT5sAaWUAOiloilT/Kj6qhvf1N7QDmT0Dq3pA3WkBuk7n9RjzqLUfFx79jpR1LBh41qUjEtyClOzw9523Wf9PV1eveHrwcNItEHYoh9aMSa44EGDba2JMVeCNw8p2x+I2fOwFy7WGaEFFydMo3hJkvaoiVZ0ND/k+adaNSViXA3Nm1blMd+dnQ++uqjdjGJYVvlrXm1epmb1d6+BL2y1ro2NqetJ88Jp860/Aw+YZt8ildSYt0a2hD1a7VF7a9W6cvvgc4T root@platform
[root@platform ~]#

在gitlab的profile settings的ssh keys的選項頁,將上述公鑰輸入,並點選add key按鈕
這裡寫圖片描述

這種方式就是基於ssh的通道進行的,也是使用最多的方式之一,當然在不同的環境下可能還有其他的一些問題,這裡不再贅述。

方式2: 活用git的多種方式中包含使用者名稱/密碼的方式

使用場景
使用git clone http://使用者名稱:密碼@機器名:port/groupname/project.git方式進行操作,因為密碼和使用者名稱已經提供,自然不必再輸入

[[email protected] ~]# git clone http://192.168.163.118:32001/root/demotest.git
Cloning into 'demotest'...
Username for 'http://192.168.163.118:32001': ^C
[[email protected] ~]# git clone http://root:[email protected]:32001/root/demotest.git
Cloning into 'demotest'...
warning: You appear to have cloned an empty repository.
[[email protected] ~]#

可以看到這種方式也可以直接clone了,問題之一就在於密碼的明文顯示,如果你不是那麼糾結,這種方式是最簡單的了。
其實這種方式也是通過修改config的設定起到作用的,可以看到設定的資訊在config的url中有儲存,所以後面才不需要重新輸入密碼。

[root@platform ~]# cd demotest
[root@platform demotest]# cd .git
[root@platform .git]# ls
HEAD  branches  config  description  hooks  info  objects  refs
[root@platform .git]# cat config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = http://root:12345678@192.168.163.118:32001/root/demotest.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
[root@platform .git]#

方式3: 設定.git-credentials

使用場景:
通過設定不同層次的.git-credentials來設定使用者名稱/密碼
就像git的config檔案一樣,分成系統級/使用者級/專案級三層,.git-credentials也是一樣。
這裡使用使用者級的設定,執行日誌如下所示:

[root@platform ~]# touch ~/.git-credentials
[root@platform ~]# echo "http://root:[email protected]:32001" >~/.git-credentials
[root@platform ~]# git config --global credential.helper store
[root@platform ~]#

執行對私有倉庫git clone可以不必輸入密碼,這種方式是通過.git-credentials來進行的

[[email protected] ~]# git clone http://192.168.163.118:32001/root/demotest.git
Cloning into 'demotest'...
warning: You appear to have cloned an empty repository.
[[email protected] ~]#

這種的好處是每次提交的時候不必看見明文的密碼了,但是還是有儲存。
破壞一下這種設定

[[email protected] ~]# git config --global credential.helper get
[[email protected] ~]# git clone http://192.168.163.118:32001/root/demotest.git
fatal: destination path 'demotest' already exists and is not an empty directory.
[[email protected] ~]# rm -rf demotest/
[[email protected] ~]# git clone http://192.168.163.118:32001/root/demotest.git
Cloning into 'demotest'...
git: 'credential-get' is not a git command. See 'git --help'.
Username for 'http://192.168.163.118:32001':

刪除剛剛生成的使用者級檔案

[root@platform ~]# rm ~/.git-credentials 
rm: remove regular file '/root/.git-credentials'? y
[root@platform ~]# 

另外,使用者級別的~/.gitconfig檔案中也生成了如下的資訊

[root@platform ~]# cat ~/.gitconfig 
[user]
    email = liumiaocn@outlook.com
    name = Liu Miao
[credential]
    helper = get
[root@platform ~]#

刪除credential相關的設定,就基本清場完畢了

[root@platform ~]# cat ~/.gitconfig 
[user]
    email = liumiaocn@outlook.com
    name = Liu Miao
[root@platform ~]# 

方式4: 設定config

使用場景:
通過設定不同層次的config檔案來設定使用者名稱/密碼

先通過使用者名稱和密碼方式進行clone

[[email protected] ~]# git clone http://192.168.163.118:32001/root/demotest.git
Cloning into 'demotest'...
Username for 'http://192.168.163.118:32001': root
Password for 'http://[email protected]:32001': 
warning: You appear to have cloned an empty repository.
[[email protected] ~]#

新增一個檔案並push會需要輸入使用者名稱/密碼

[root@platform ~]# cd demotest
[root@platform demotest]# echo "hello liumiaocn" >demotest.txt
[root@platform demotest]# git add demotest.txt 
[root@platform demotest]# git commit -m "init"
[master (root-commit) d7a4826] init
 1 file changed, 1 insertion(+)
 create mode 100644 demotest.txt
[root@platform demotest]# git remote -v
origin  http://192.168.163.118:32001/root/demotest.git (fetch)
origin  http://192.168.163.118:32001/root/demotest.git (push)
[root@platform demotest]# 
[root@platform demotest]# git push origin master
Username for 'http://192.168.163.118:32001': ^C
[root@platform demotest]# 
[root@platform demotest]# cd .git
[root@platform .git]# ls
COMMIT_EDITMSG  HEAD  branches  config  description  hooks  index  info  logs  objects  refs
[root@platform .git]# cat config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = http://192.168.163.118:32001/root/demotest.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
[root@platform .git]#

修改後

[root@platform .git]# cat config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = http://root:12345678@192.168.163.118:32001/root/demotest.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
[root@platform .git]#

再次執行git push

[root@platform demotest]# pwd
/root/demotest
[root@platform demotest]# ls
demotest.txt
[root@platform demotest]# git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 228 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://root:12345678@192.168.163.118:32001/root/demotest.git
 * [new branch]      master -> master
[root@platform demotest]# 

方式5: 在remote中加入使用者名稱/密碼

使用場景:
通過對git的remote進行設定使其包含使用者名稱/密碼來實現git push等操作時無需再次輸入使用者名稱密碼

輸入使用者名稱/密碼clone倉庫

[[email protected] ~]# git clone "http://192.168.163.118:32001/root/demotest.git"
Cloning into 'demotest'...
Username for 'http://192.168.163.118:32001': root
Password for 'http://[email protected]:32001': 
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
[[email protected] ~]#

修改remote

[root@platform ~]# cd demotest/
[root@platform demotest]# git remote -v
origin  http://192.168.163.118:32001/root/demotest.git (fetch)
origin  http://192.168.163.118:32001/root/demotest.git (push)
[root@platform demotest]# git remote set-url origin http://root:[email protected]:32001/root/demotest.git
[root@platform demotest]# git remote -v
origin  http://root:12345678@192.168.163.118:32001/root/demotest.git (fetch)
origin  http://root:12345678@192.168.163.118:32001/root/demotest.git (push)
[root@platform demotest]#

實際修改的內容,最終還是落到了專案級別的config檔案上

[root@platform demotest]# cd .git
[root@platform .git]# ls
HEAD  branches  config  description  hooks  index  info  logs  objects  packed-refs  refs
[root@platform .git]# cat config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = http://root:12345678@192.168.163.118:32001/root/demotest.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
[root@platform .git]#

新增一個檔案,然後push

[root@platform .git]# cd ..
[root@platform demotest]# touch hellotest
[root@platform demotest]# git add hellotest 
[root@platform demotest]# git commit -m "add hellotest"
[master 9c80bdd] add hellotest
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 hellotest
[root@platform demotest]# git push origin master
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 276 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://root:12345678@192.168.163.118:32001/root/demotest.git
   d7a4826..9c80bdd  master -> master
[root@platform demotest]#

相關推薦

版本管理gitlab實踐教程基礎(14)

這篇文章整理一下對gitlab/github進行clone或者push操作時,不需要每次提交都輸入使用者名稱/密碼的幾種方式。 場景 何時需要輸入使用者名稱/密碼 gitlab可以建立private/protected/public的倉庫,priv

版本管理gitlab實踐教程基礎(1)

這幾篇文章主要面向對git和gitlab稍有基礎,但是又不太會用的使用者,通過這幾文章你可以學到如下gitlab的使用方法: sshkey的設定 建立專案 分支操作 使用者/許可權/組的操作 配置專案可見性 Merge Request Issues操作

版本管理gitlab實踐教程基礎(16)

這篇文章介紹一下如何修改初始管理賬號root的密碼。 命令列方式 確認gitlab-rails 使用gitlab-rails進行設定 which gitlab-rails /opt/gitlab/bin/gitlab-rails # #

版本管理SVN實踐教程基礎(6)tag的查詢/建立/刪除/設定

這篇文章介紹一下在svn中tag操作的查詢/建立/刪除與設定。 查詢tag 因為因為tags目錄是apache建議的方式,其本質也就是一個目錄,所以查詢的方式可以結合svn list命令確認改目錄下的檔案資訊,以及svn log確認提交資訊來進行。

版本管理gitlab實踐教程進階(1)

實踐gitflow 結合git flow,使用gitlab作為遠端倉庫管理,在實際的專案中是一種可行的方式,而且這種方式對與複雜大型的專案有較好的適應方式。 git flow git flow源於Vincent Driessen在2010年提出的一個分

版本管理gitlab實踐教程 進階 1

實踐gitflow 結合git flow,使用gitlab作為遠端倉庫管理,在實際的專案中是一種可行的方式,而且這種方式對與複雜大型的專案有較好的適應方式。 git flow git flow源於Vincent Driessen在2010年提出的一個分支模型: 主要特點 兩個長期

版本管理gitlab實踐教程 基礎 1

這幾篇文章主要面向對git和gitlab稍有基礎,但是又不太會用的使用者,通過這幾文章你可以學到如下gitlab的使用方法: sshkey的設定 建立專案 分支操作 使用者/許可權/組的操作 配置專案可見性 Merge Re

版本管理gitlab實踐教程 基礎 3

symbol 為什麽 reg details pro == 什麽 操作 requests comment是版本管理中非常重要的內容,尤其是在經年累月的大型項目中,鐵打的項目,流水的SE,哪怕只言片語的留下,對後來者問題的對應很多時候都能起到重要作用,這篇文章用來講解

持續整合jenkins實踐教程基礎(2): 整合gitlab

作為持續整合的利器Jenkins已經得到了廣泛地應用,僅僅作為一個工具,Jenkins已然有了自己的生態圈,支援其的plugin更是超過1300+。在實際中如何使用以及如何更好地使用jenkins,一直是大家在實踐並討論的。本系列文章將會從如何使用jenki

持續整合jenkins實踐教程基礎(7): 控制檯輸出的中文亂碼

jenkins是Java開發的應用程式,很多東西可以通過-D傳入選項來進行設定,這篇文章以中文亂碼的調整為例子,來說明一下常見修改的方式。 現象 使用jenkinsfile定義的流水線,如果stage名稱為中文的時候,圖形顯示沒有問題,但是在consol

持續整合jenkins實踐教程基礎(6): 在jenkins的容器中進行映象的構建

作為持續整合的利器Jenkins已經得到了廣泛地應用,僅僅作為一個工具,Jenkins已然有了自己的生態圈,支援其的plugin更是超過1300+。在實際中如何使用以及如何更好地使用jenkins,一直是大家在實踐並討論的。本系列文章將會從如何使用jenki

持續整合jenkins實踐教程基礎(1): 郵件設定

作為持續整合的利器Jenkins已經得到了廣泛地應用,僅僅作為一個工具,Jenkins已然有了自己的生態圈,支援其的plugin更是超過1300+。在實際中如何使用以及如何更好地使用jenkins,一直是大家在實踐並討論的。本系列文章將會從如何使用jenki

.net 開源模板引擎jntemplate 教程基礎語法

## 一、基本概念 上一篇我們簡單的介紹了jntemplate並寫了一個hello world(如果沒有看過的,[點選檢視](https://www.cnblogs.com/hnvvv/p/14476803.html)),本文將繼續介紹jntemplate的模板語法。 我們在講解語法前,首先要了解一下標籤

持續集成jenkins實踐教程 基礎 4 集成redmine

如何 沒有 iss 技術 文章 proc head pac redmin 作為持續集成的利器Jenkins已經得到了廣泛地應用,僅僅作為一個工具,Jenkins已然了了自己的生態圈,支持其的plugin更是超過1300+。在實際中如何使用以及如何更好地使用jenki

window nodejs 版本管理器 nvm-windows 教程

pda 打開 版本 解壓 cmd appdata .com mod 成功 先去https://github.com/coreybutler/nvm-windows/releases 下載nvm-setup.zip 安裝 安裝的過程中會提示是否獲取nodejs的管理權限,點確

Spring Boot實踐教程開篇

mongo code 消息 發布 搭建 jdk1.8 hello 初學者 框架 前言 ??Java項目開發Spring應該是最常被用到的框架了,但是老式的配置方式讓人覺得特別的繁瑣,雖然可以通過註解去簡化xml文件的配置,但是有沒有更簡單的方式來幫我們完成這些重復性的事情呢

詳解功能版本管理使用eoLinker

object 可見 nts 平臺 sso fonts 一覽 多次 動態 先看一個對話: “這裏,你改一下,這裏返回一個object。” “好...好......” “還有這裏,返回個String。” ...... 在接口文檔改了7、8個version後,組長又發話了,“

visual studio code教程基礎使用和自定義設置

麻煩 安裝 顏色 必須 寫入 全局環境變量 註意 一個 preview 一、界面介紹 1.1 界面介紹 1.2 文件夾和文件的打開 文件——>打開文件夾/打開文件 1.3 新建文件/文件夾 新建文件: a. 文件——>新建文件; b. 按Ctrl+n; c

SVN版本管理器入門使用教程

功能 負責 訪問 一個 span IT 進行 pan 簡單 一、SVN結構作用概述: SVN目錄下一般采用標準的source/branches、source/trunk、source/tags結構。 trunk目錄下放置開發的主代碼,1新功能模塊的開發首先放在主幹上

Java爬坑基礎

1.方法有返回值時儘量把返回值寫上,儘量避免不必要的坑。例如陣列的擴容中