【问题标题】:Access GitHub repo from CloudBuild从 CloudBuild 访问 GitHub 存储库
【发布时间】:2020-12-21 19:01:49
【问题描述】:

我使用 Google CloudBuild 作为 CI 服务器。它使用我的 GitHub 存储库的镜像存储库来检测/提取更改,效果很好。

我的问题是,一旦 CloudBuild 成功运行了我的所有测试,我希望它在我的存储库中添加一个标签。我正在使用gcr.io/cloud-builders/git 容器,但我不能直接推送标签,因为它只连接到镜像仓库。因此,为了解决这个问题,我使用 shell 脚本直接从 Github 克隆 repo 以实际创建标签。

当我尝试从 Cloudbuild 访问 Github 时,我收到以下错误消息:

Step #3: debug1: read_passphrase: can't open /dev/tty: No such device or address
Step #3: Host key verification failed.
Step #3: fatal: Could not read from remote repository.
Step #3: 
Step #3: Please make sure you have the correct access rights
Step #3: and the repository exists.

这是我正在使用的脚本(它只是测试 github 访问):

#!bin/sh

# Copy private key to ~/.ssh directory
cp ./path/to/my/key ~/.ssh/github
chmod 600 ~/.ssh/github

# Create SSH config file
cat >~/.ssh/config <<EOL
Host github.com
  HostName github.com
  AddKeysToAgent yes
  IdentityFile ~/.ssh/github
EOL
chmod 600 ~/.ssh/config

# Add key to agent
eval "$(ssh-agent -s)"
ssh-keyscan github.com >> ~/.ssh/known_hosts
ssh-add -k ~/.ssh/github

# Set GIT config
git config --global user.name "myusername"
git config --global user.email "myusername@users.noreply.github.com"

# Test authentication with Github
ssh -T git@github.com

调试日志的另一个相关部分是:

Step #3: debug1: Reading configuration data /etc/ssh/ssh_config
Step #3: debug1: /etc/ssh/ssh_config line 1: Applying options for github.com

它似乎是从/etc/ssh/ssh_config 读取而不是我创建的~/.ssh/config 文件。

我做错了什么?

【问题讨论】:

    标签: git github ssh google-cloud-build


    【解决方案1】:

    首先,最好的做法是:

    • 将您的主机条目命名为githubgh,而不是github.com,以确保使用配置文件(因为任何DNS 都无法解析gh,而github.com 可以)

    • 在配置文件中也包含User git:这样,要使用的 SSH URL 变为:'gh':

      ssh -Tvv gh
      

    其次,如果 SSH 只读取 /etc/ssh/ssh_config 而不是 ~/.ssh/config,这意味着脚本以某种方式以 root 而非用户身份启动。

    【讨论】:

      猜你喜欢
      • 2017-04-21
      • 2018-02-04
      • 2018-11-27
      • 1970-01-01
      • 2013-03-23
      • 2022-12-04
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      相关资源
      最近更新 更多