【问题标题】:Cloning a Gitlab project to a Google Colab instance using SSH or HTTPS使用 SSH 或 HTTPS 将 Gitlab 项目克隆到 Google Colab 实例
【发布时间】:2020-03-23 07:49:29
【问题描述】:

我的问题是我想将 Google Colab 实例与 Gitlab 项目连接起来,但 SSH 和 HTTPS 似乎都不起作用。从错误消息中,我怀疑 Colab 中存在与设置相关的问题。也许我必须允许 Colab 连接到 Gitlab 并将其放在某处的白名单中?

在位于“/content”目录中时,从 Colab 中的 Notebook 运行以下 shell 命令

git config --global user.name "mr_bla"
git config --global user.email "bla@wbla.bla"
git clone https://gitlab.com/mr_bla/mr_blas_project.git

导致以下错误消息:

Cloning into 'mr_blas_project'...
fatal: could not read Username for 'https://gitlab.com': No such device or address

我已经按照习惯生成了 SSH 密钥,但是 SSH 检查

ssh -vvvT git@gitlab.com:mr_bla/mr_blas_project.git

失败,导致以下错误:

OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "gitlab.com:mr_bla/mr_blas_project.git" port 22
ssh: Could not resolve hostname gitlab.com:mr_bla/mr_blas_project.git: Name or service not known

尝试使用 SSH 方式克隆项目也不起作用:

git clone git@gitlab.com:mr_bla/mr_blas_project.git

结果:

Cloning into 'mr_blas_project'...
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Google Colab 实例正在运行以下操作系统:

cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.3 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.3 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

我已经检查了以下问题,但没有成功:

【问题讨论】:

  • 嗨,你能把你的 colab 和 gitlab 连接起来吗?我想在 gitlab 上上传我的 colab 文件!谢谢
  • 嗨@Chris_007,不抱歉,我最终将项目写在一个普通的 .py 文件中并在本地运行所有内容。如果您的存储库不需要是私有的,也许您可​​以尝试 mitra 的答案并将您的 GitLab 存储库公开。并考虑在 SSH 和 HTTPS 之间切换。

标签: git gitlab google-colaboratory


【解决方案1】:

这是我使用 GitLab 我的 Google Colab 笔记本 持久 版本控制所遵循的工作流程(我想使用 GitHub 会非常相似)。

我使用来自 GitLab 的 个人访问令牌,以便能够在 私有存储库

中使用它们

工作流程

  • 在 GitLab 中创建一个个人访问令牌

    • 编辑个人资料/用户设置转到访问令牌
      • 然后输入令牌的名称(稍后您将不得不使用它)和可选的到期日期
      • 选择所需范围
        • read_repository:通过 git clone 对存储库进行只读(拉取)
        • write_repository:存储库的读写(拉取、推送)。
      • 创建个人访问令牌
      • 将个人访问令牌保存在安全的地方。离开页面后,您将无法再访问令牌。
  • 然后,为了让 Colab 与 GitLab 交互,您必须将存储库的 .git 文件夹 存储在 Google Drive 文件夹 中,以便它持久 Colab 会话

    之间
    • 假设您在 Gdrive 中有一个文件夹,其中包含一些您想使用 Git 进行版本控制的文件:

      • /RootGDrive/Folder1/Folder2
    • 在 GColab 容器文件系统中装载 GoogleDrive。假设您将它安装在 Colab 容器文件系统内的 /content/myfiles 上。您必须在笔记本中执行此行(这会输出一个 URL,您必须通过 OAuth2 访问您的 Google Drive 到 Colab 实例)。在单元格中运行:

      from google.colab import drive 
      drive.mount(/content/myfiles)
      
      • 这会在容器文件系统上挂载您的 Google Drive 的根文件夹在 /content/myfiles/MyDrive
    • 一旦安装 change directory 执行 ma​​gic 命令,使用 %cd(使用 !cd 将不起作用,每个 shell 命令都在临时子 shell 中执行,因此它不是持久的)

      %cd "/content/myfiles/MyDrive/Folder1/Folder2"
      !pwd
      
    • 在那里,您初始化 git 存储库(这只是第一次,因为所有这些都在您的 Google Drive 中完成,这意味着它是一个将在会话之间持续存在的存储库,如果不是在您离开 Google Colab 会话后,它将被删除)。

       !git init
      
      • 这会在您的 Google Drive 文件夹中创建 .git 文件夹
    • 现在您必须在本地配置典型的 git 参数(因此它存储在 .git 文件夹中)在推/拉时需要(同样这必须在第一次完成):

      !git config --local user.email your_gitlab_mail@your_domain.com 
      !git config --local user.name your_gitlab_name
      
    • 现在使用 之前创建的 PAT 添加遥控器(这也是第一次完成):

      • 关键点:远程 URL 格式(必须通过 HTTPs)取决于 Gitlab 项目(repo)是否在组/子组下的天气:

        • 在一个组下(可能有 /group/subgroup1/subgroup2/.../project.git 或只是 /group/projec.git)

          !git remote add origin https://<pat_name>:pat_code>@gitlab.com/group_name/subgroup1/project_name.git
          
        • 不属于一个组

          !git remote add origin https://<pat_name>:pat_code>@gitlab.com/your_gitlab_username/project_name.git
          
    • 现在 git 存储库配置在 Google Drive 文件夹 中,而不仅仅是在文件系统容器中,因此除了所有常用的 git 命令外,您还可以拉/推

      !git add .
      !git commit -m"First commit"
      !git push -u origin master
      

在第一次完成此操作后,为了保持 Git 和 GitLab 的“版本控制”(再次我猜它与 GitHub 非常相似,因为 GitLab 的 Groups 功能对我来说非常有价值)MyDrive 中的文件/Folder1/Folder2 您应该创建一个笔记本,在您编辑文件夹中的其他文件时安装 Google Drive 和所需的 git 命令。

我想说最好的方法是有一个参数化的笔记本来检查这是否是第一次进行 git 初始化等等,如果不是只是添加/提交/推送到 GitLab 存储库。

克隆

如果只是克隆到 Container FS(或者如果它已经挂载到 Google Drive),只需使用上面解释的相同远程 git clone:

  • 在一个组下

      !git clone https://<pat_name>:<pat_code>@gitlab.com/group_name/project_name.git
    
  • 不属于一个组

      !git clone https://<pat_name>:<pat_code>@gitlab.com/gitlab_user_name/project_name.git
    

编辑:我正在添加我创建的笔记本,以便您可以使用它在 Colab 和 GitLab 之间进行交互,名为 Gitlab_Colab_Interaction.ipynb,因此您可以直接从 Colab 使用它:

进口

import os
from pathlib import Path

参数

# Paths
container_folder_abspath = Path('/content/myfiles')
gdrive_subfolder_relpath = Path('MyDrive/Colab Notebooks/PathTo/FolderYouWant') # No need to scape the space with pathlib Paths
gitlab_project_relpath = Path('/group_name/subgroup1/YourProject.git')
# Personal Access Token
PAT_name = 'my_pat_name'
PAT_code = 'XXXX_PAT_CODE_XXXXX'

安装驱动器

from google.colab import drive
drive.mount(str(container_folder_abspath))


fullpath = container_folder_abspath / gdrive_subfolder_relpath # Path objects with the operator /
%cd $fullpath
!pwd

初始化(或不初始化)

initialization = True
for element in fullpath.iterdir():
    if element.is_dir():
        if element.name == '.git':
            initialization = False
            print('Folder already initialized as a git repository!')
    

gitlab_url = 'https://' + PAT_name + ':' + PAT_code + '@gitlab.com/' + str(gitlab_project_relpath)
if initialization:
    !git init
    !git config --local user.email your_gitlab_mail@yourmail.com
    !git config --local user.name your_gitlab_user
    !git remote add origin $gitlab_url # Check that PATs are still valid
    !echo "GitLab_Colab_Interaction.ipynb" >> ".gitignore" # To ignore this file itself if it is included in the folder

else:
    print("### Current Status ###")
    !git status
    print("\n\n### Git log ###")
    !git log

Git 命令

# Git Add
!git add *.ipynb # For example to add just the modified notebooks

# Git Commit
!git commit -m "My commit message"

# Git Push
!git push -u origin master # As of now Gitlab keeps using the name master 

【讨论】:

    【解决方案2】:

    请务必添加“!”作为您在 Google Colab 工作区上的命令的前缀,如下所示:!git clone https://gitlab.com/mr_bla/mr_blas_project.git

    【讨论】:

    • 感谢您的提示。我已经这样做了。正在执行 git 命令,问题是它们在稍后阶段失败。
    【解决方案3】:

    如果是私人回购。 您可以使用GitLab deploy token,也可以使用GitLab personal access token。 然后你就会

    git clone https://<deploy_username>:<deploy_token>@gitlab.example.com/tanuki/awesome_project.git
    

    请注意,您可能不希望上面的代码与此敏感的 &lt;deploy_token&gt; 暴露在您的笔记本中,例如,您可以通过将其放入安装在驱动器上的可执行脚本中来隐藏它,或者我认为您可以隐藏代码。

    【讨论】:

      【解决方案4】:

      在 gitlab 中将 repo 可见性更改为公共(从私有?)。我知道这可能并不总是可能的,但这样做解决了我的问题。

      【讨论】:

        【解决方案5】:

        在直接联系了部分 Google 员工后:上述功能尚不适用于 Google Colab。在此期间我尝试了几件事,但没有任何效果。

        如果有人知道是否以及何时添加此功能,请告诉我。

        【讨论】:

        • 我不确定,但是使用个人访问令牌,我认为您可以按照您所说的做,我已经在这方面添加了答案
        • @GonzaloPolo 谢谢!我已取消选中我的答案并接受了您的答案。我没有尝试您的答案,但它看起来非常全面,因此可能会起作用。
        猜你喜欢
        • 1970-01-01
        • 2021-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-28
        • 1970-01-01
        • 1970-01-01
        • 2021-10-17
        相关资源
        最近更新 更多