如何在使用 GitHub Actions 构建的 Dockerfile 中使用 GitHub token 并尝试克隆私有仓库?

11
这是我的GitHub Action步骤。已经创建了PRIVATE_REQUIREMENT_OWNER_TOKEN密码,其中包含具有完整repo权限的GitHub令牌。
  - name: Build docker image
    id: docker_build
    uses: docker/build-push-action@v2
    with:
      push: false
      context: .
      tags: 'username/image:latest'
      secrets: |
        "github_token=${{ secrets.PRIVATE_REQUIREMENT_OWNER_TOKEN }}"

这是一个在requirements.txt中包含指向私有仓库的链接,在构建Docker镜像时正在尝试安装的一行代码。
git+ssh://git@github.com/username/private-repository

该行已经添加到 Dockerfile 中。
RUN --mount=type=secret,id=github_token pip install https://$(cat /run/secrets/github_token)@github.com/username/private-repository.git

在 GitHub Actions 中出现以下错误:

#11 [ 6/12] RUN --mount=type=secret,id=PRIVATE_REQUIREMENT_OWNER_TOKEN_SECRET pip install https://$(cat /run/secrets/PRIVATE_REQUIREMENT_OWNER_TOKEN_SECRET)@github.com/username/private-repository.git
#11 sha256:b3d88dd9813db3257b15f53f1eb5a4c593c69ff98ec03cc4d70d564df1a1f7f6
#11 0.697 Collecting https://****@github.com/vassilyvv/django-sinbad.git
#11 0.790   ERROR: HTTP error 404 while getting https://****@github.com/username/private-repository
.git
#11 0.791 ERROR: Could not install requirement https://****@github.com/username/private-repository
.git because of HTTP error 404 Client Error: Not Found for url: https://github.com/username/private-repository
 for URL https://****@github.com/username/private-repository.git

但是当我尝试使用同一令牌在本地机器上克隆存储库时,它能够成功:

git clone https://<token>@github.com/username/private-repository.git

我完全不知道如何使用这个github_token来成功克隆上面提到的私有仓库。

我的目标是在构建GitHub Actions中的Dockerfile镜像时克隆私有GitHub存储库。 我几乎确定我已经执行了一些错误的步骤。 请帮忙!


嗨。这个帖子可能会对你有所帮助:从Github Action传递变量到Docker镜像构建 - GuiFalourd
@Vas /run/secrets/github_token 是base64编码还是包含预期值的实际值? - VonC
它被输入到秘密部分而没有编码。我不确定它是如何存储在/run/secrets/github_token中的,因为我没有成功在GH Actions日志中显示它。 - Vassily
1个回答

5

我认为这是由于提供给pip的git URL有问题。如果您需要从私有git存储库安装Python包,则可以使用以下格式。

pip install git+https://<PERSONAL_ACCESS_TOKEN>@github.com/username/private-repo.git

因此,在您的情况下,应该是:

pip install git+https://$(cat /run/secrets/github_token)@github.com/username/private-repository.git

参见: https://pip.pypa.io/en/stable/cli/pip_install/#git


你太棒了! - Vassily

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接