【问题标题】:Git Permission denied (publickey) when accessing server through Github Actions CI/CD通过 Github Actions CI/CD 访问服务器时 Git Permission denied (publickey)
【发布时间】:2020-11-16 03:41:27
【问题描述】:

当我通过本地计算机连接到服务器时,我可以使用 ssh 成功连接到 Github。

我使用this 教程来设置 ssh 密钥。

但是,在使用 Github 操作时出现此错误:

err: git@github.com: Permission denied (publickey).
err: fatal: Could not read from remote repository.
err: 
err: Please make sure you have the correct access rights
err: and the repository exists.

这是我的 Github 操作 YML:

name: CI App to DO

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  deploy-do:
    runs-on: ubuntu-latest
    steps:
      - name: SSH to server and Deploy App
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USERNAME }}
          key: ${{ secrets.SSH_KEY }}
          port: ${{ secrets.SSH_PORT }}
          script: |
            cd ~/app
            git pull origin master
            npm run build
            pm2 restart next

当通过我的本地机器在服务器上运行 ssh-add -l 时,我得到了我的密钥,但是当通过 Github 操作工作流执行相同操作时,我得到:

The agent has no identities.

我的服务器托管在使用 Ubuntu 20.04 的 Digital Ocean Droplet 上。 如前所述,当通过我的本地机器连接到我的服务器并在那里执行 git pull 时,这非常有用。我使用 MobaXterm 连接到我的 droplet。


编辑:我可以在不使用密码时完成这项工作。

在我的本地机器上,我使用的是 MobaXterm

【问题讨论】:

  • this 有帮助吗?
  • @ParthShah 我已经尝试了所有这些,但仍然不能只在 de CI 工作流程上工作。
  • 我认为,您在没有密码的情况下工作的编辑评论是这里的关键。您是在谈论 SSH 密钥上的密码吗?如果是这样,您使用的是哪个工具 - Putty 或 OpenSSH?如果使用 WINdows,您是否使用凭据管理器?请在编辑中添加该信息...

标签: git ubuntu github github-actions


【解决方案1】:

由于密码似乎是问题所在,您可能需要在 GitHub 操作工作流程中将密钥添加到 ssh 代理。
参见Matthias Pigulla 中的示例“Using a SSH deploy key in GitHub Actions to access private repositories”,它建议:

# .github/workflows/my-workflow.yml
# ... other config here
jobs:
    build:
        runs-on: ubuntu-18.04
        steps:
            -   uses: actions/checkout@v1

            -   name: Setup SSH Keys and known_hosts
                env:
                    SSH_AUTH_SOCK: /tmp/ssh_agent.sock
                run: |
                    mkdir -p ~/.ssh
                    ssh-keyscan github.com >> ~/.ssh/known_hosts
                    ssh-agent -a $SSH_AUTH_SOCK > /dev/null
                    ssh-add - <<< "${{ secrets.SSH_PRIVATE_KEY }}"

            -   name: Some task that fetches dependencies
                env:
                    SSH_AUTH_SOCK: /tmp/ssh_agent.sock
                run: ./fetch-deps.sh

但从那以后他也定义了actions/webfactory-ssh-agent

这个动作

  • 启动 ssh 代理,
  • 导出 SSH_AUTH_SOCK 环境变量,
  • 将私有 SSH 密钥加载到代理中并
  • 为 GitHub.com 配置 known_hosts。

【讨论】:

    【解决方案2】:

    为此,您可以在结帐步骤之后在您的 eas-pipeline.yml 文件中添加一个额外的步骤。

     - name: Checkout
            uses: actions/checkout@v2
            with:
              persist-credentials: false
    
    *******************************************************************************
    
          - name: Reconfigure git to use HTTP authentication
            run: >
              git config --global url."https://github.com/".insteadOf
              ssh://git@github.com/
        
    
    *******************************************************************************
    

    这是原始答案:https://github.com/actions/setup-node/issues/214

    【讨论】:

      猜你喜欢
      • 2019-12-29
      • 2012-05-03
      • 2018-06-01
      • 2018-04-22
      • 2013-10-13
      • 2019-01-25
      • 2016-10-04
      • 2016-01-05
      • 2021-11-22
      相关资源
      最近更新 更多