(注意:我知道个人访问令牌会起作用,但外部原因要求我通过SSH Deploy Key执行此操作。源repo和目标repo都是私有的。)
我需要使用CircleCI将每个提交从源repo推送到目标repo。假设回购被命名为source
和target
。我正在配置CircleCI来运行我的自定义推送脚本,但它说的是密钥是只读的。
我做了什么:
ssh-keygen
创建了一个新密钥对并压缩私钥。id_rsa.pub
作为Deploy Key上载到目标仓库,勾选“允许使用此密钥进行推送访问”。#!/bin/bash
set -e
if [ -z "$SSH_KEY_E" ]; then
echo "No SSH key found in environment, set it as \$SSH_KEY_E" >&2
exit 1
fi
echo "$SSH_KEY_E" |
base64 -d |
gunzip -c > ~/.ssh/m.id_rsa
set -x # debug
cat >> ~/.ssh/config << EOF
Host GHMirror
HostName github.com
User git
Port 22
IdentityFile ~/.ssh/m.id_rsa
EOF
git remote add mirror GHMirror:iBug/circleci-target.git
git push mirror +master
输出日志表明从环境恢复的密钥是有效的,但它似乎不像用于推送到GitHub。
我想指出一些观点:
~/.ssh/id_rsa
并直接使用git@github.com:iBug/target.git
作为mirror
的远程URL,但它没有用,说密钥是只读的GHMirror
并将此规则写入~/.ssh/config
,如shell脚本中所示。仍然抱怨密钥是只读的~/.ssh/m.id_rsa
,但没有运气。我已经通过在本地运行脚本验证了所有内容,并且它已成功推送到目标存储库,因此我必须在CircleCI上有一些我缺少的东西。
我添加了环境变量GIT_SSH_COMMAND="ssh -vv"
并得到了这个结果:
debug1: key_load_public: No such file or directory
debug1: identity file /home/circleci/.ssh/id_rsa type -1
...
debug2: key: (0xREDACTED), agent
debug2: key: /home/circleci/.ssh/id_rsa ((nil))
debug2: key: /home/circleci/.ssh/id_dsa ((nil))
debug2: key: /home/circleci/.ssh/id_ecdsa ((nil))
debug2: key: /home/circleci/.ssh/id_ed25519 ((nil))
但是,ls -l ~/.ssh/id_rsa
显示该文件存在,并允许0600。
我相信您遇到的问题是由于ssh-agent提供的CircleCI密钥是只读的。我过去也遇到过这个问题。要调试,您可以使用以下内容:
export GIT_SSH_COMMAND="ssh -vv"
这将打印出有关正在使用的密钥的详细信息。
我能够解决这个问题:
# Disable the ssh-agent
export SSH_AUTH_SOCK=none
# Tell ssh to use the specific SSH key
export GIT_SSH_COMMAND="ssh -i path/to/key"
还要确保你chmod 0600 path/to/key
。如果其他用户可以读取SSH,则不会使用密钥。