CentOS 7 安裝 Bitbucket 自動更新教學 #1
有時候會把團隊的程式放在Bitbucket上,並直接用Server將Bitbucket上的Repository拉到Server上!
這就可以達成自動化更新Server上的程式!
但是本篇將介紹的是Git和Bitbucket基礎環境設定。把這些都設定好,才能達到自動化的目標!
自動化的設定在下一篇喔!敬請期待!
(本設定適用於CentOS 7, CentOS 6.8)
將CentOS的SSH Public Key設定至Bitbucket
複製你的的SSH Public Key!
1 2 |
[Andy@www ~]$ cat ~/.ssh/id_rsa.pub ssh-rsa AAAABAFASDFATRT$#%xxxxxxxxxxxxxxxxxxxxxxxxx |
如果發現找不到檔案,可以使用ssh-keygen建立一個SSH Public Key
其中下列第3, 4, 5行都可以直接按Enter即可,可以不需多作其他設定
123456789101112131415161718192021 [Andy@www ~]$ ssh-keygen -t rsaGenerating public/private rsa key pair.Enter file in which to save the key (/home/Andy/.ssh/id_rsa):Enter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in /home/Andy/.ssh/id_rsa.Your public key has been saved in /home/Andy/.ssh/id_rsa.pub.The key fingerprint is:xx:xx:xx:xx:xx:xx:55:xx:xx:xx:xx:xx:xx:xx:xx:xx Andy@testThe key's randomart image is:+--[ RSA 2048]----+| .... || . Y || .+ || .o o || ..V . . || +.. . || .T .. || .=x=. . || .. **. E. |+-----------------+接下來再執行上一段的指令『cat ~/.ssh/id_rsa.pub』,把public key的內容複製起來!
設定Bitbucket
做法有兩種:全域設定、依Repositoryg設定
1. 全域設定:Profiles >> Bitbucket Settings >> SSH Keys >> Add Key
2. 依Repository(倉庫)設定:Repository >> 選則欲連結的Repository >> Settings >> Access Keys >> Add key
確認CentOS是否有安裝Git
1 2 |
[Andy@www ~]$ git --version git version 1.7.1 |
切換到你想要設定為git的專案目錄
1 2 |
[Andy@www ~]$ cd /home/Andy/Firebase/myFirebaseProj/public [Andy@www public]$ |
初始化一個空的Git Repository
1 2 |
[Andy@www public]$ git init ./ Initialized empty Git repository in /home/Andy/Firebase/myFirebaseProj/public/.git/ |
CentOS與Bitbucket連線
如果你的CentOS是第一次與Bitbucket連線,就會出現下列第4行的問題。
『Are you sure you want to continue connecting?』意思是『你確定要繼續連線嗎?』
1 2 3 4 5 6 7 8 |
[Andy@www public]$ ssh -T git@bitbucket.org The authenticity of host 'bitbucket.org (xxx.xxx.143.20)' can't be established. RSA key fingerprint is xx:xx:xx:xx:xx:xx:55:xx:xx:xx:xx:xx:xx:xx:xx:xx. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'bitbucket.org,xxx.xxx.143.20' (RSA) to the list of known hosts. logged in as polun-wang. You can use git or hg to connect to Bitbucket. Shell access is disabled. |
複製專案(git clone)
到Bitbucket複製[SSH] Clone address!
在CentOS貼上Bitbucket的SSH連結『git clone Bitbucket的SSH連結』
git就會開始複製Repository到你指定的專案目錄,git會自動幫你以Repository名稱建立一個資料夾!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[Andy@www public]$ git clone git@bitbucket.org:xxxxxx/xxxxxx.git [sudo] password for Andy: [Andy@www public]$ git clone git@bitbucket.org:xxxxxx/xxxxxx.git Initialized empty Git repository in /home/Andy/Firebase/myFirebaseProj/public/xxxxxx/.git/ Warning: Permanently added the RSA host key for IP address 'xxx:xxx:xxx::xxx' to the list of known hosts. remote: Counting objects: 3, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0) Receiving objects: 100% (3/3), done. [Andy@www public]$ ls -al total 0 drwxrwxr-x 4 Andy Andy 35 Feb 2 12:48 . drwxrwxr-x 3 Andy Andy 20 Feb 2 10:16 .. drwxrwxr-x 7 Andy Andy 119 Feb 2 10:24 .git drwxrwxr-x 3 Andy Andy 35 Feb 2 12:48 test_repo |
如果你看到如下錯誤訊息,表示很有可能沒把SSH Key設定好喔!可能需要再去確認一次!
123456789 [Andy@www public]$ git clone git@bitbucket.org:xxxxxx/xxxxxx.git[sudo] password for Andy:Initialized empty Git repository in /home/Andy/Firebase/myFirebaseProj/public/xxxxxx/.git/The authenticity of host 'bitbucket.org (xxx:xxx:xxx::xxx)' can't be established.RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added 'bitbucket.org,xxx:xxx:xxx::xxx' (RSA) to the list of known hosts.Permission denied (publickey).fatal: The remote end hung up unexpectedly
設定你的E-mail、姓名,這個姓名與E-mail會出現在Git變更記錄中『git log』之中。
在CentOS上可以下『git log』來看看喔!
1 2 |
[Andy@test test_repo]$ git config --global user.email "Andy@test" [Andy@test test_repo]$ git config --global user.name "Andy Wang" |
看現在public目錄下就會出現以Bitbucket上的Repository為名的資料夾!
因為這是一個空的Repository,所以只有一個README.md檔。
接著,在該資料夾中開一個index.html,並且在index.html中隨意編輯一些內容!
1 2 3 4 5 6 |
[Andy@www public]$ ls test_repo [Andy@www public]$ cd test_repo [Andy@www test_repo]$ ls README.md [Andy@www xxxxxx]$ vim index.html |
可以透過『 git status 』查看Git Repository狀態
1 2 3 4 5 6 7 |
[Andy@www xxxxxx]$ git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # index.html nothing added to commit but untracked files present (use "git add" to track) |
『git add』可以將檔案加入Repository
1 |
[Andy@www xxxxxx]$ git add index.html |
『 git commit -m ‘版本訊息’』跟Git確認哪些檔案需要上傳,請Git確認是否有檔案衝突(conflict)需要合併(merge)!
1 2 3 4 |
[Andy@test test_repo]$ git commit -m '第一個版本' [master f25c4fa] 第一個版本 1 file changed, 1 insertion(+) create mode 100644 index.html |
如果你看到以下訊息,代表你還沒設定Git user.name 和 user.email 哦!
請參考上一步驟或是提示訊息中的指令!
12345678910111213141516 [Andy@www www]$ git commit -m '第一個版本'[master xxx330x] 第一個版本Committer: Andy <www.xxx.com>Your name and email address were configured automatically basedon your username and hostname. Please check that they are accurate.You can suppress this message by setting them explicitly:git config --global user.name "Your Name"git config --global user.email you@example.comIf the identity used for this commit is wrong, you can fix it with:1 files changed, 131 insertions(+), 0 deletions(-)create mode 100644 index.html
查看Git log(修改記錄)
下方第3行可以看見前面步驟設定的Git user.name 和 user.email
1 2 3 4 5 6 7 8 9 10 11 12 |
[Andy@test test_repo]$ git log commit xxxxxxaxxxx53cxxxxxxc0effaxxxxc5aexxxxxx Author: Andy Wang@VM.test <Andy@test> Date: Sat Feb 3 07:27:20 2018 -0500 第一個版本 commit xxxxxxaf8exxxxb8b88xxxxxxd58xxxxe3xxxxxx Author: Andy Wang <xxxxxx@gmail.com> Date: Fri Feb 2 15:45:10 2018 +0000 README.md 已使用 Bitbucket 在线创建 |
推送更新(push)至Bitbucket
確認之後,把檔案推上Bitbucket
1 2 3 4 5 6 7 |
[Andy@www xxxxxx]$ git push Counting objects: 5, done. Compressing objects: 100% (4/4), done. Writing objects: 100% (4/4), 1.81 KiB, done. Total 4 (delta 0), reused 0 (delta 0) To git@bitbucket.org:xxxxxx/xxxxxx.git fedeaac..xxx310x master -> master |
如果你出現下列2~13行的警告訊息,表示你缺少設定『指定分支』!
123456789101112131415161718192021 [Andy@test test_repo]$ git pushwarning: push.default is unset; its implicit value is changing inGit 2.0 from 'matching' to 'simple'. To squelch this messageand maintain the current behavior after the default changes, use:git config --global push.default matchingTo squelch this message and adopt the new behavior now, use:git config --global push.default simpleSee 'git help config' and search for 'push.default' for further information.(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode'current' instead of 'simple' if you sometimes use older versions of Git)Counting objects: 4, done.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 316 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)To git@bitbucket.org:xxxxxx/test_repo.git3xxxxxx..fxxxxxx master -> master建議指定預設值:Simple
1 [Andy@test test_repo]$ git config --global push.default simple
完成!
這時候可以去Bitbucket上看看,檔案應該都在上面了!
而且這邊可以看到剛才設定的Git user.name, user.email
Reference: