通过Github Actions CI / CD访问服务器时,Git权限被拒绝(公钥)

时间:2020-07-26 19:21:02

标签: git ubuntu github github-actions

当我通过本地计算机连接到服务器时,可以使用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 Actions工作流进行此操作时,我得到了:

The agent has no identities.

我的服务器使用Ubuntu 20.04托管在Digital Ocean Droplet上。 如前所述,当通过本地计算机连接到我的服务器并在其中执行git pull时,此方法非常有用。我使用MobaXterm连接到我的液滴。


编辑:在不使用密码短语的情况下,我可以使其工作。

在我的本地计算机上,我正在使用MobaXterm

1 个答案:

答案 0 :(得分:1)

由于密码短语似乎是问题所在,因此您可能需要将密钥添加到GitHub Action工作流程中的ssh代理中。 以Using a SSH deploy key in GitHub Actions to access private repositories中的示例“ {Matthias Pigulla”为例,该提案建议:

# .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-agent,
  • 导出SSH_AUTH_SOCK环境变量,
  • 将私有SSH密钥加载到代理中,并
  • 为GitHub.com配置known_hosts。
相关问题