rsa - 如何使用私钥从 Gitlab 下载私有(private)项目

标签 rsa gitlab ssh-keys

我在 Gitlab 中创建了一个私有(private)项目,我试图写下一个简单的部署脚本以在从存储库下载项目后启动该项目,我正在使用 Ubuntu 的 bash 环境来执行该脚本。

我使用一个简单的 curl 命令对一个公共(public)项目做了同样的事情:

curl -fSL "https://gitlab.com/MyUsername/MyProject/repository/archive.zip?ref=master" -o project.zip

answers那里展示了如何使用 Gitlab 的用户名+密码或 private_token 和 curl 命令对私有(private)项目执行相同的操作。但我更喜欢使用 Gitlab 提供的“部署 key ”功能。到目前为止,我已经按照 Gitlab's documentation 的指示完成了:

ssh-keygen -t rsa -C "$your_email"

然后上传公钥文件以在 Gitlab 中部署我的项目的 key 。我的问题是:如何使用我必须下载项目最新版本的私钥?我应该使用 curlscp 还是其他什么?还请包括项目属于某个组的示例。

最佳答案

SSH 私钥不能用于 HTTPS 连接——因此,您需要使用通过 SSH 公开的服务。在这种情况下,这意味着执行 git clone(或者,理想情况下,如果您有一个现有的克隆,则执行 git fetch 以增量更新它),然后执行 git 存档

#!/bin/bash
#      ^^^^-- /bin/bash, not /bin/sh, or the [[ ]] syntax isn't guaranteed to work

# change or parameterize these
repo_path=git@gitlabhost.com:group/repo.git
repo_dir=/path/to/local-dir
output_path=/path/to/archive.zip

if [[ -d $repo_dir ]]; then
  (cd "$repo_dir" && exec git fetch) || exit
else
  git clone "$repo_path" "$repo_dir" || exit
fi

# and to create the archive
cd "$repo_dir" && git archive --format=zip -o "$output_path" origin/master 

创建初始克隆后,执行 git fetch 和本地 git archive 实际上比下载整个 zip 文件更节省带宽,因为它仅提取自上次更新以来的更改。


如果您的私钥保存为~/.ssh/id_rsa,它将被自动使用。否则,您需要像这样创建一个 ~/.ssh/config 文件条目:

Host gitlabhost.com # substituting the real hostname
    User git # use the username "git" connecting to this host by default
    IdentityFile /path/to/your/id_rsa # if not in the default location

关于rsa - 如何使用私钥从 Gitlab 下载私有(private)项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38294483/

相关文章:

dns - GitLab Pages 自定义域允许 www

仅使用 SSH key 和 dockerfile 进行部署

rsa - 如何组合 n 和 e 来创建 RSA 中的公钥?

gitlab - 使用 GitLab 私有(private) token 调用 GitLab 端点时出现未经授权的错误

encryption - 在 Go 中使用 RSA 进行 key 交换的 AES 加密通信

git - SmartGit RPC 失败;结果=22,HTTP 代码=500

scp - 将公钥添加到 SCP 服务器

linux - 如何验证两个字符串是否是使用ssh-keygen生成的 key 对?

Java RSA如何生成不同的公钥?

java - RSA 身份验证问题