gpt4 book ai didi

docker - 无法在 Github Actions 中的 docker 中向 gcloud 进行身份验证

转载 作者:行者123 更新时间:2023-12-03 08:04:26 24 4
gpt4 key购买 nike

我正在 Github Actions 中运行一个工作流程,该工作流程构建一个 docker 镜像,然后在容器内运行一些 gsutil 命令。

我在身份验证方面遇到问题,gsutil 命令需要访问我在 GCP 上的存储桶,但我收到错误:

Run docker run ltr:latest /bin/sh -c "gsutil cat gs://test-bucket/test_file.txt"
ServiceException: 401 Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object.
Error: Process completed with exit code 1.

我无法提供完全可重现的代码,因为它需要将 GCP 项目和服务帐户凭据保存在 github 的 secret 中,但这是我可以分享的最多内容:

Github Actions 工作流程定义:

name: test

on: push

jobs:

test-gsutil-command:
runs-on: [self-hosted, ubuntu-latest]
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Authenticate to Google Cloud
id: auth
uses: google-github-actions/auth@v0
with:
credentials_json: '${{ secrets.GCP_CREDENTIALS }}'
- name: Set up Google Cloud SDK
uses: google-github-actions/setup-gcloud@v0
- name: Register gcloud as a Docker credential helper
run: gcloud auth configure-docker
- name: build
run: docker build ltr:latest .
- name: Run gcloud command in docker
run: docker run ltr:latest /bin/sh -c "gsutil cat gs://test-bucket/test_file.txt" # this line is giving me the above error

Dockerfile

FROM gcr.io/deeplearning-platform-release/tf-gpu.2-8
RUN mkdir -p /ltr
COPY requirements /ltr/requirements
COPY Makefile /ltr
COPY setup.py /ltr

WORKDIR /ltr

RUN python -m pip --no-cache-dir install -r requirements/base.txt

另一件重要的事情是,我确信我使用的服务帐户可以访问我想要从中读取文件的存储桶 - 我可以在本地运行该 gsutil 命令。

您能否告诉我,除了工作流程中的 gcloud auth configure-docker 之外,我还需要做什么才能在 docker 容器中运行该 gsutil 命令?

最佳答案

默认情况下,setup-gcloud 操作会设置指向凭证文件的环境变量。因此,您可以将此文件安装到 Docker 容器中,并将环境变量指向已安装的文件,如下所示:

docker run --rm 
--volume $GITHUB_WORKSPACE:/workspace
--mount type=bind,source=$GOOGLE_APPLICATION_CREDENTIALS,target=/workspace/creds.json
--env GOOGLE_APPLICATION_CREDENTIALS=/workspace/creds.json
--env CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=/workspace/creds.json
--env GOOGLE_GHA_CREDS_PATH=/workspace/creds.json

完成此操作后,gcloud cli 自动检测到 creds 文件,我可以进行经过身份验证的调用。希望这有帮助!

关于docker - 无法在 Github Actions 中的 docker 中向 gcloud 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72855303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com