如何使用私钥从 Gitlab 下载私有项目
How to download a private project from Gitlab using private keys
我在 Gitlab 中创建了一个私有项目,我正在尝试写下一个简单的部署脚本以在从存储库下载项目后启动该项目,我正在使用 Ubuntu 的 bash 执行脚本的环境。
我用一个简单的 curl 命令对 public 项目做了同样的事情:
curl -fSL "https://gitlab.com/MyUsername/MyProject/repository/archive.zip?ref=master" -o project.zip
有answers out there showing how to do the same for a private project using Gitlab's username+password or private_token
and the curl command. But I prefer to use the "deploy keys" features provided with Gitlab. So far I've done as it is instructed by Gitlab's documentation:
ssh-keygen -t rsa -C "$your_email"
然后上传 public 密钥文件以在 Gitlab 中部署我的项目的密钥。我的问题是:如何使用我必须下载项目最新版本的私钥?我应该使用 curl
或 scp
还是其他什么?还请包括项目属于某个组的示例。
SSH 私钥不能用于 HTTPS 连接——因此,您需要使用通过 SSH 公开的服务。在这种情况下,这意味着执行 git clone
(或者,理想情况下,如果您有现有克隆,则执行 git fetch
以增量更新它),然后执行 git archive
。
#!/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
我在 Gitlab 中创建了一个私有项目,我正在尝试写下一个简单的部署脚本以在从存储库下载项目后启动该项目,我正在使用 Ubuntu 的 bash 执行脚本的环境。
我用一个简单的 curl 命令对 public 项目做了同样的事情:
curl -fSL "https://gitlab.com/MyUsername/MyProject/repository/archive.zip?ref=master" -o project.zip
有answers out there showing how to do the same for a private project using Gitlab's username+password or private_token
and the curl command. But I prefer to use the "deploy keys" features provided with Gitlab. So far I've done as it is instructed by Gitlab's documentation:
ssh-keygen -t rsa -C "$your_email"
然后上传 public 密钥文件以在 Gitlab 中部署我的项目的密钥。我的问题是:如何使用我必须下载项目最新版本的私钥?我应该使用 curl
或 scp
还是其他什么?还请包括项目属于某个组的示例。
SSH 私钥不能用于 HTTPS 连接——因此,您需要使用通过 SSH 公开的服务。在这种情况下,这意味着执行 git clone
(或者,理想情况下,如果您有现有克隆,则执行 git fetch
以增量更新它),然后执行 git archive
。
#!/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