使用jenkins通过capistrano 3部署项目

时间:2014-04-18 23:17:42

标签: git deployment ssh jenkins capistrano

我试图通过jenkins工作中的capistrano 3部署到服务器。

"上限环境部署"用我的信条从我的mac完美地工作,但在jenkins,部署信用,我遇到了问题。

最奇怪的是前几个git命令成功,但是当capistrano进入" git远程更新"时,服务器挂起了一个"未授权的"错误。我已经通过ssh验证了这一点,并且运行了“git remote update'来自/ var / www / repo。

我有密钥转发设置 - 下面是我的Jenkins工作:

eval $(ssh-agent)
ssh-add ~/.ssh/keyname.pem
cap staging deploy

我不知道在capistrano中发生了什么会导致第一个git命令成功(' git ls-remote'),但是" git remote update"失败。是的,密钥已在github上作为部署密钥添加到repo中,并且在deploy.rb和〜/ .ssh / config中启用了密钥转发。

以下是来自' cap staging deploy'的失败日志示例,在Jenkins框上以Jenkins用户身份运行:

 INFO [d3e19149] Running /usr/bin/env mkdir -p /tmp/project/ on server.xxxxxyyyy.com
DEBUG [d3e19149] Command: /usr/bin/env mkdir -p /tmp/project/
 INFO [d3e19149] Finished in 0.943 seconds with exit status 0 (successful).
DEBUG Uploading /tmp/project/git-ssh.sh 0.0%
 INFO Uploading /tmp/project/git-ssh.sh 100.0%
 INFO [43533553] Running /usr/bin/env chmod +x /tmp/project/git-ssh.sh on server.xxxxxyyyy.com
DEBUG [43533553] Command: /usr/bin/env chmod +x /tmp/project/git-ssh.sh
 INFO [43533553] Finished in 0.052 seconds with exit status 0 (successful).
DEBUG [40e092d5] Running /usr/bin/env git ls-remote git@github.com:Organization/project.git on server.xxxxxyyyy.com
DEBUG [40e092d5] Command: ( GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/project/git-ssh.sh /usr/bin/env git ls-remote git@github.com:Organization/project.git )
DEBUG [40e092d5]    69a8427388e2958e9b2c67e6048a46cb3a2544a5    HEAD
DEBUG [40e092d5]    69a8427388e2958e9b2c67e6048a46cb3a2544a5    refs/heads/master

DEBUG [40e092d5] Finished in 1.635 seconds with exit status 0 (successful).
 INFO [3d9e0765] Running /usr/bin/env mkdir -pv /var/www/shared /var/www/releases on server.xxxxxyyyy.com
DEBUG [3d9e0765] Command: /usr/bin/env mkdir -pv /var/www/shared /var/www/releases
 INFO [3d9e0765] Finished in 0.046 seconds with exit status 0 (successful).
 INFO [3ef2c63f] Running /usr/bin/env mkdir -pv /var/www/shared/log /var/www/shared/tmp/pids /var/www/shared/tmp/cache /var/www/shared/tmp/sockets /var/www/shared/vendor/bundle /var/www/shared/public/system on server.xxxxxyyyy.com
DEBUG [3ef2c63f] Command: /usr/bin/env mkdir -pv /var/www/shared/log /var/www/shared/tmp/pids /var/www/shared/tmp/cache /var/www/shared/tmp/sockets /var/www/shared/vendor/bundle /var/www/shared/public/system
 INFO [3ef2c63f] Finished in 0.043 seconds with exit status 0 (successful).
 INFO [a0820d89] Running /usr/bin/env mkdir -pv /var/www/shared/config on server.xxxxxyyyy.com
DEBUG [a0820d89] Command: /usr/bin/env mkdir -pv /var/www/shared/config
 INFO [a0820d89] Finished in 0.050 seconds with exit status 0 (successful).
DEBUG [19575759] Running /usr/bin/env [ -f /var/www/shared/config/database.yml ] on server.xxxxxyyyy.com
DEBUG [19575759] Command: [ -f /var/www/shared/config/database.yml ]
DEBUG [19575759] Finished in 0.063 seconds with exit status 0 (successful).
DEBUG [06e522d5] Running /usr/bin/env [ -f /var/www/repo/HEAD ] on server.xxxxxyyyy.com
DEBUG [06e522d5] Command: [ -f /var/www/repo/HEAD ]
DEBUG [06e522d5] Finished in 0.056 seconds with exit status 0 (successful).
 INFO The repository mirror is at /var/www/repo
DEBUG [062dd56a] Running /usr/bin/env if test ! -d /var/www/repo; then echo "Directory does not exist '/var/www/repo'" 1>&2; false; fi on server.xxxxxyyyy.com
DEBUG [062dd56a] Command: if test ! -d /var/www/repo; then echo "Directory does not exist '/var/www/repo'" 1>&2; false; fi
DEBUG [062dd56a] Finished in 0.053 seconds with exit status 0 (successful).
DEBUG [65e9187e] Running /usr/bin/env cd /var/www/repo && git rev-parse --short HEAD on server.xxxxxyyyy.com
DEBUG [65e9187e] Command: cd /var/www/repo && git rev-parse --short HEAD
DEBUG [65e9187e]    69a8427
DEBUG [65e9187e] Finished in 0.080 seconds with exit status 0 (successful).
 INFO [349bd507] Running /usr/bin/env git remote update on server.xxxxxyyyy.com
DEBUG [349bd507] Command: cd /var/www/repo && /usr/bin/env git remote update
DEBUG [349bd507]    Fetching origin
DEBUG [349bd507]    Fetching origin
DEBUG [349bd507]    ERROR: Repository not found.
DEBUG [349bd507]    Fetching origin
DEBUG [349bd507]    fatal: The remote end hung up unexpectedly
DEBUG [349bd507]    Fetching origin
DEBUG [349bd507]    error: Could not fetch origin
cap aborted!

有什么想法吗?

谢谢,

安迪

2 个答案:

答案 0 :(得分:7)

我遇到了与当前部署相同的问题。我运行了命令cap staging deploy --trace并收到了以下输出:

Command: cd /var/www/deployment directory/repo && (GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/deployment directory/git-ssh.sh /usr/bin/env git remote update )    

DEBUG [520a3953]    Fetching origin   
DEBUG [520a3953]    Fetching origin  
DEBUG [520a3953]    ERROR: Repository not found.
DEBUG [520a3953]    Fetching origin
DEBUG [520a3953]    fatal: The remote end hung up unexpectedly  
DEBUG [520a3953]    Fetching origin
DEBUG [520a3953]    error: Could not fetch origin  
cap aborted!   
SSHKit::Command::Failed: git stdout: Nothing written 
git stderr: Nothing written   

我通过删除部署目录来修复错误

rm -rf /var/www/deployment directory

我再次运行cap staging deploy --trace并修复了无关的Rails错误,并且能够成功部署。

事后我可能只删除了/ var / www / deployment目录/ repo并获得了相同的结果。我确定我的问题是我更改了我的gitlab项目名称,并且它存储在部署服务器的repo目录中。命令: Command: cd /var/www/deployment directory/repo && ( GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/deployment directory/git-ssh.sh /usr/bin/env git remote update )可能正在使用存储在部署服务器中某个文件中的旧存储库URL。

答案 1 :(得分:1)

当我更改了repo位置时遇到了同样的问题,但是没有删除部署目录:

/www/deployment directory/repo

我在/ www / deployment目录/ repo:

中运行了这个命令
git remote show origin

这表明原点仍然设置为旧的仓库,我通过运行此命令改变了原因:

git remote set-url origin git://new.url.here

(我从这个问题中学到了:Change the URI (URL) for a remote Git repository