简体   繁体   English

通过ssh执行git pull的脚本并绕过密码短语要求?

[英]Script to perform a git pull via ssh and bypass passphrase requirements?

I'm trying to set up continuous integration with Bamboo. 我正在尝试与Bamboo建立持续集成。 I want to configure a task that ssh's into our stage server, cd's into the proper directory and performs a git pull. 我想配置一个任务,将ssh放入我们的舞台服务器,将cd放入适当的目录并执行git pull。

I've been able to set up the ssh task, but doing the git pull has been difficult. 我已经能够设置ssh任务,但是执行git pull却很困难。

Steps I've taken: 我已采取的步骤:

  • Configured an ssh task to cd into the project directory and run the following script: 将ssh任务配置为cd到项目目录并运行以下脚本:

     #!/bin/bash echo "pulling from master" git pull origin master 
  • The script runs, but the logs show a Permission denied (publickey). 脚本运行,但是日志显示Permission denied (publickey). error after it tries to pull. 尝试拉后出现错误。

  • I switched my remote-url from HTTPS to ssh and created a publickey. 我将远程URL从HTTPS切换到ssh并创建了一个公共密钥。 Now when I try to do a manual pull it asks for the key's passphrase. 现在,当我尝试进行手动拉动时,它会询问钥匙的密码。

  • Used ssh-agent to cache the passphrase for a session. 使用ssh-agent缓存会话的密码。

  • Realized that this cache only persists until I close my session so I followed the steps from this article ( https://confluence.atlassian.com/display/BITBUCKET/Set+up+SSH+for+Git ) to start ssh-agent with every new session. 意识到此缓存仅会持续到我关闭会话,因此我按照本文中的步骤( https://confluence.atlassian.com/display/BITBUCKET/Set+up+SSH+for+Git )启动ssh-agent每个新的会话。 Namely I added this script to my .bashrc : 也就是说,我将此脚本添加到了.bashrc

     SSH_ENV=$HOME/.ssh/environment # start the ssh-agent function start_agent { echo "Initializing new SSH agent..." # spawn ssh-agent /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}" echo succeeded chmod 600 "${SSH_ENV}" . "${SSH_ENV}" > /dev/null /usr/bin/ssh-add } if [ -f "${SSH_ENV}" ]; then . "${SSH_ENV}" > /dev/null ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || { start_agent; } else start_agent; fi 

and added this to the .ssh/config file: 并将其添加到.ssh / config文件中:

Host myStashInstance.org
 IdentityFile ~/.ssh/id_rsa
  • The article said that I should be prompted to enter the passphrase and the agent would start up but that hasn't happened. 文章说,应该提示我输入密码,代理会启动,但是没有发生。 I still need manually start ssh-agent. 我仍然需要手动启动ssh-agent。

I would like to know what the next steps would be to getting ssh-agent to start when I start a new session so I can continue figuring out how to finish configuring this job. 我想知道启动新会话时启动ssh-agent的下一步是什么,以便我继续找出如何完成配置此作业的方法。 (Also open to suggestions for other avenues to pursue if I'm completely on the wrong path.) (如果我完全走错了道路,也可以寻求其他途径的建议。)

To summarize, you're running into the following issue: 总而言之,您遇到了以下问题:

  • The Bamboo Agent runs on server A Bamboo代理在服务器A上运行
  • Server B is your staging server 服务器B是您的登台服务器
  • As part of the build running on server A, you want to SSH into B and do a git pull there. 作为在服务器A上运行的构建的一部分,您想通过SSH进入B并在其中进行git pull
  • Authentication to the Git repo requires you to either enter a password or a passphrase (even when using ssh-agent ). 对Git存储库的身份验证要求您输入密码或密码短语(即使使用ssh-agent )。

There are several ways this could be solved: 有几种解决方法:

Option 1: Run Bamboo Agent on the Staging Server 选项1:在登台服务器上运行Bamboo Agent

To simplify this environment, install the Bamboo Agent on your Staging Server. 为了简化此环境,请在登台服务器上安装Bamboo代理。 Enter the Git URL and credentials in the Bamboo build plan and let Bamboo take care of the work. 在Bamboo的构建计划中输入Git URL和凭据,然后让Bamboo负责这项工作。

Bamboo will pull the Git repo, you can then run whatever steps you want and deploy to your staging server process. Bamboo将拉出Git存储库,然后您可以运行所需的任何步骤并将其部署到登台服务器进程中。

Option 2: Deploy to the Staging Server 选项2:部署到登台服务器

Instead of doing the Git operation on your staging server, do it on server A. Enter the Git URL and credentials in the Bamboo build plan and let Bamboo take care of the work. 而不是在登台服务器上执行Git操作,而是在服务器A上进行操作。在Bamboo生成计划中输入Git URL和凭据,然后让Bamboo负责工作。

Once your project is checked out on server A, run whatever build steps you want to do there, then package (zip/tar/jar/...) the build results and copy them over to the Staging Server. 在服务器A上签出项目后,在此处执行您要执行的任何构建步骤,然后打包(zip / tar / jar / ...)构建结果并将其复制到Staging Server。 Maybe you don't need to use Git on the Staging Server at all. 也许您根本不需要在登台服务器上使用Git。

Option 3: Use the .netrc file for Git authentication 选项3:使用.netrc文件进行Git身份验证

This is the least secure of the options. 这是最不安全的选项。 Switch back your remote URL to HTTPS, then create a ~/.netrc file on the staging server and add an entry for your Git server in there, providing username and password, as described here: https://confluence.atlassian.com/display/STASH/Permanently+authenticating+with+Git+repositories#PermanentlyauthenticatingwithGitrepositories-Usingthe.netrcfile 将您的远程URL切换回HTTPS,然后在登台服务器上创建~/.netrc文件,并在其中为您的Git服务器添加一个条目,提供用户名和密码,如下所述: https : //confluence.atlassian.com/显示/存储/永久+通过+ Git +存储库进行身份验证#永久与Git存储库进行身份验证-使用.netrc文件

Example: 例:

machine mygitserver
login mario
password SECRET

Caution, this requires you to store your password in plain text in the .netrc file. 注意,这要求您将密码以纯文本格式存储在.netrc文件中。 Only use this method if you can live with this risk. 仅当您可以承受此风险时,才使用此方法。 If the password used for this account changes, you will have to change it in this file as well. 如果此帐户使用的密码更改,则也必须在此文件中更改它。 You probably want to use a technical/service account for this, not a real user account. 您可能要为此使用技术/服务帐户 ,而不是真实用户帐户。 Lock down this account as much as possible. 尽可能锁定此帐户。

One of these three options should help you to solve your problem. 这三个选项之一应该可以帮助您解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM