【问题标题】:Formatting curl to clone git repos using clone_url and user:token使用 clone_url 和 user:token 格式化 curl 以克隆 git repos
【发布时间】:2020-03-16 21:52:37
【问题描述】:

我目前有一个 shell 脚本,我想将它用作 Jenkins/Docker 作业的一部分,该作业会克隆我的组织中使用特定语言标记的所有存储库。

curl -s -H "Authorization: token "$token"" https://github.[company].com/api/v3/orgs/$org/repos\?page\=$i\&per_page\=100 |
jq '.[] | select(.language != null) | select(.language | contains("'$lang'")).ssh_url' | xargs -n 1 git clone

在大多数情况下它可以正常工作,但我意识到我需要在 Docker 容器中配置 SSH 才能正常工作。我发现我仍然可以使用带有 user:token 组合的 HTTPS 进行克隆,而不是尝试进行设置。

git clone https://user:token@github.[company].com/organization/repo.git

我的问题是我正在获取所有目标存储库的列表并克隆它们,因此我需要以某种方式为每个存储库传递 user:token。我知道我可以获得 clone_url 而不是 ssh_url 但我不知道如何插入凭据。我环顾四周,但没有发现任何可以帮助我解决这个问题的东西。有谁知道我是否以及如何格式化脚本,以便在 URL 中传递凭据?

谢谢

【问题讨论】:

    标签: git curl https clone


    【解决方案1】:

    您可以关注 Kayan Azimov 的“Dockerizing Jenkins: Securing Passwords With docker-compose, docker-secret and Jenkins Credentials Plugin

    它使用秘密来注册密码

    secrets:
      artifactoryPassword:
        file: ./secrets/artifactoryPassword
    

    它将绑定挂载的 docker 秘密文件,我们稍后可以通过 /run/secrets/secret_name_here 路径从容器中引用这些文件。

    如果没有,你也可以添加到你的脚本中

    git config --global url."https://github.com/github.[company].com".insteadOf "git@github.com/github.[company].com"
    

    没有更多 sed 可做。
    git config url.<base>.insteadOf

    【讨论】:

    • 我很欣赏这个建议,但在这种特殊情况下,它并不是我想要的。我真的想通了。当我开始更多地使用 Docker 时,我一定会牢记这一点。
    • @jb11 我添加了一个避免任何 sed 的替代方案。
    【解决方案2】:

    我想我正在查看脚本的错误部分以进行格式化。在使用 xargs 进行 git clone 之前,我最终在 curl/jq 输出上使用了 sed 来格式化 url。

    sed 's,https://,https://user:token@,g'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2023-03-06
      • 2018-06-15
      • 2016-04-19
      • 2016-02-14
      相关资源
      最近更新 更多