我正在嘗試使用 crontab 自動運行 git pull 命令。我在 MacOs 機器上。當我在沒有 cron 的情況下使用它但使用 cron 出錯時,Git pull 作業正常。我已經嘗試了各種解決方案。其中一些在下面。他們都手動作業正常。
我嘗試將下面的命令放在腳本automatic.sh 中,然后使用cron 運行它。
ssh-agent bash -c 'ssh-add /Users/{username}/.ssh/id_rsa; /usr/bin/git pull'eval `ssh-agent -s` && ssh-add ~/.ssh/id_rsa && ssh-add -l && git pull
但我總是低于錯誤,
fatal: could not read Username for 'https://git.{domain}.com': Device not configured
為什么?
uj5u.com熱心網友回復:
我認為問題在于您需要的引號。嘗試在沒有括號 ( )的變數周圍使用雙引號{}
ssh-agent bash -c 'ssh-add /Users/'"$username"'/.ssh/id_rsa; /usr/bin/git pull'
注意:變數相同 domain
看看這個問題
uj5u.com熱心網友回復:
TL;DR:如果可以,請使用 ssh URL。(并且:不要git pull從腳本運行。)
您在此處顯示的URL,來自您的錯誤訊息:
https://git.{domain}.com
不是ssh(安全外殼)URL。它是一個 HTTPS (HTTP-over-SSL) URL。這是兩種完全不同的協議。它們通常在不同的 IP 埠上運行(ssh 為 22,https 為 443)。并非所有 Git服務器都回應這兩種協議,但如果您的服務器回應,則您的 Git 作為客戶端存在兩個關鍵差異:
Git 作為客戶端,通過 連接
https://,必須向服務器提供兩項:用戶名和密碼。(密碼不必是文字密碼,例如可以是 PAT。)Git 作為客戶端,通過 連接
ssh://,必須向服務器提供兩項:用戶名和某種密鑰。
除了“密鑰”與“密碼”,它們看起來——實際上——非常相似,但 https 和 ssh 客戶端以完全不同的方式獲取這兩個專案:
https 客戶端通常直接從用戶的鍵盤讀取用戶名和密碼,繞過所有重定向嘗試。
ssh 客戶端通常將用戶名作為引數,并從公共和/或私有密鑰對檔案中讀取密鑰和/或從代理獲取密鑰。
這兩個要點中的單詞通常做了很多繁重的作業,但這就是您看到的錯誤的原因。當您git pull從 cron 作業運行時,沒有鍵盤可供讀取。這是物理不可能的cron -這,而你甚至不是可以運行在計算機有你在鍵入用戶名和密碼。在 Mac 上嘗試打開會/dev/tty導致Device not configured錯誤。
收到該錯誤后,libCURL 庫1放棄并且整個拉取失敗。
現在,請注意ssh從引數中獲取用戶名——而不是讓你輸入它——并從檔案和/或ssh 代理中獲取密鑰,而不是讓你輸入它。所以 ssh 已經非常強大了這里的優勢:它不需要您坐在鍵盤前,準備好需要輸入用戶名和密碼。因此,如果您讓 Git 使用 ssh, 2您更有可能繼續。
這不是答案的結束(盡管這是開始,因此是上面的 TL;DR)。使用 https 時,您可以告訴 libCURL不要從用戶那里讀取用戶名和密碼。具體如何操作取決于作業系統,但一般來說,您可以使用以下形式的 URL:
https://user@host:password/path/to/repo.git
這樣做的缺點是您將用戶名和密碼以明文形式放在那里供所有人查看。避免這種情況,除非這是您唯一的選擇。
或者,Git 可以使用憑據幫助程式將用戶名和密碼提供給libCURL 。這是關于 Git 的另一個大秘密:Git根本不進行任何身份驗證。如果你想聲稱自己是 Barack Obama,3你可以繼續這樣做,Git 會相信你的。
它是其他程式進行身份驗證。Git 依賴于這些其他程式(尤其是 Web 服務器和 ssh 服務器)進行身份驗證;這決定了您可以在其他機器上讀取和寫入哪些存盤庫。在您的本地機器上,本地作業系統的權限決定了您可以讀寫哪些存盤庫。
由于 ssh 有它自己的相當龐大和復雜的方法來處理身份驗證(包括 ssh 代理),我們在這里根本不會介紹它,但我將談談使用 libCURL 的憑證助手。Git 總是帶有兩個簡單的,store并且cache:store助手只是將用戶名和密碼保存在一個檔案中(它不加密,所以考慮避免這種情況,或者至少,小心保護這個檔案)。該cache助手不保存憑證永久,而只是暫時的,所以它的危險性較低,但它的意思被卡在前面的一些其他幫手。其他幫手可能會要求輸入密碼,并避免在每個時間,您可以在兩者之間插入快取助手:如果快取條目已過期,則快取助手從下一級助手獲取密碼,現在您必須輸入密碼;但除此之外,它會傳回快取的條目,這樣您這次就不必鍵入它了。
用于各種作業系統的 Git 附帶了額外的特定于作業系統的幫助程式。特別是在 OSX 上,有一個git-credential-osxkeychain使用 OS X Keychain 軟體的助手。(我不使用這個:我使用 ssh。)
有關所有這些的完整說明,請參閱gitcredentials 檔案。某些特定幫助程式何時以及是否適用于您的特定 https 設定取決于太多因素,無法在這里猜測。
1Git doesn't have all of the https protocol built into it; instead, Git just links against libCURL. The libCURL for your OS is OS-dependent, so this helps Git avoid being quite so OS-dependent.
2When using ssh URLs, Git literally just runs ssh, too.
3If you really are Barack Obama, why are you reading this?
About git pull
The git pull command does two things:
First, it runs—or tries to run—
git fetch. This connects to some other system and obtains new commits, or, as in your case, fails to connect (which then stopsgit pullin its tracks).If all has gone well in step 1,
git pullnow runs a second Git command. You choose, in advance, whether this should begit rebaseorgit merge.
Command #2 is meant to work interactively. Regardless of which command you choose, Git will do its best to combine any work you have done, making new commits in your repository, with any new commits that came in during the fetch step (command #1). This may require user assistance. If it does, command #2 prints the messages as to what assistance is required, and terminates with an error status, leaving a mess in your Git repository. This mess must be cleaned up before you attempt further operations.
Because we don't know whether command #2 will succeed in the first place, it's always a bad idea to use git pull in an unattended script. We can guess whether command #1 is likely to succeed or not, and check for that, in a script, so it's OK to use git fetch in a script. It's never OK to use git rebase or git merge in any unattended script though, unless your script checks for failure and arranges for something useful to happen (alert a human, stop attempting further commands, and so on).
To do all this correctly in a script, you must break the git pull into its constituent steps, because a failure from git pull could mean:
- 獲取失敗:可能是暫時的網路故障;沒什么大不了; 請稍后再試!
- 第二步失敗:災難! 不要繼續。
您需要知道其中發生了哪些,因此您不能使用git pull.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/341645.html
