【问题标题】:Pull deploy, github actions and ssh keys拉动部署、github 操作和 ssh 密钥
【发布时间】:2022-02-02 01:01:28
【问题描述】:

假设我想使用 GitHub 操作设置我的部署流程,并使用拉取策略。

所以我有一个 Ubuntu 服务器,我复制服务器的公共 ssh 密钥,将它添加到我的 GitHub 帐户,然后我可以从 Ubuntu 服务器克隆,构建和运行应用程序。

很好,但我觉得这里是个小陷阱。
密钥被添加到帐户,而不是存储库。

如果我离开作为仓库所有者的组织会怎样?
服务器将失去执行正确 CI 的能力,对吧?

组织所有者可以创建作为 SSH 密钥持有者的帐户并且永远不会离开组织,但是如果存储库所有权被转移怎么办?
我可能在这里错过了一些东西,但为什么不允许直接将密钥添加到存储库,而不是用户帐户?
还是有这个选项,但我以某种方式错过了它?

【问题讨论】:

    标签: github github-actions


    【解决方案1】:

    密钥被添加到帐户,而不是存储库。

    这就是为什么您有 deploy keysper 存储库。

    例如,像 webfactory/ssh-agent 这样的 GitHub Action 确实支持 Deploy 密钥。

    为了支持在此用例中选择正确的密钥,此操作会扫描密钥 cmets 并将设置额外的 Git 和 SSH 配置以使事情正常进行。

    • git@github.com:owner/repo.githttps://github.com/owner/repo 等存储库创建部署密钥时,将该URL 放入密钥注释中。 (提示:试试ssh-keygen ... -C "git@github.com:owner/repo.git"。)
    • 将密钥添加到代理后,此操作将扫描密钥 cmets。
    • 对于包含此类 URL 的关键 cmets,将编写使用 url.<base>.insteadof 的 Git 配置设置。它将 git 请求重定向到以 https://github.com/owner/repo 或 git@github.com:owner/repo 开头的 URL 到一个虚假的主机名/URL,如 git@...some.hash...:owner/repo
    • 会生成适用于假主机名的 SSH 配置部分。它将 SSH 连接映射回 github.com,同时将 SSH 指向包含适当密钥的公共部分的文件。这将使 SSH 在连接到 GitHub.com 时使用正确的密钥。

    然后你会得到一个像this example这样的GitHub Action配置:

    name: Deploy
    on:
    push:
     tags:
       - 'GA*'
    # ...
    - name: Install SSH Client ?
      uses: webfactory/ssh-agent@v0.4.0
      with:
        ssh-private-key: ${{ secrets.DEPLOY_KEY }}
    
    - name: Deploy ?
      uses: JamesIves/github-pages-deploy-action@3.5.9
      with:
        BASE_BRANCH: master
        BRANCH: gh-pages
        CLEAN: true
        FOLDER: .
        SSH: true
    # ...
    

    在命令行中,由于GitHub CLI gh 2.5.0(2022 年 2 月):gh repo deploy_key

    gh repo deploy-key add <key-file> [flags]
    
    # generate a passwordless SSH key and add it as a deploy key to a repository
    $ ssh-keygen -t ed25519 -C "my description" -N "" -f ~/.ssh/gh-test
    $ gh repo deploy-key add ~/.ssh/gh-test.pub
    

    从上下文中查看issue 4242

    【讨论】:

      猜你喜欢
      • 2012-05-14
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      • 1970-01-01
      • 2016-01-19
      • 2018-07-14
      • 1970-01-01
      • 2012-06-19
      相关资源
      最近更新 更多