我有一个Git存储库,这是一个私有存储库,我需要使用它进行身份验证的能力,并能够在运行时在container build透视图中看到它.对于一些背景信息,我有一个GitHub工作流,它构建一个容器映像并将其发布到ghcr.io注册表.然而,因为我的包所依赖的存储库是私有的,所以它不能工作.现在它在本地运行,我已经考虑过改变我存储GitHub身份验证的方式,以便我可以访问它,但我想知道是否有人知道更好的方法让我访问私有存储库.

以下是GitHub操作发布到ghcr.io注册表:

name: Docker dataeng_github_metrics

# Run workflow on tags starting with v (eg. v2, v1.2.0)
on:
  push:
    branches: [ "master" ]
    paths:
      - ./data_pipelines/dataeng_github_metrics/*
  pull_request:
    branches: [ "master" ]

jobs:
  Deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v1
        
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GHCR_REGISTRY_TOKEN }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Build and Push Docker Image
        uses: docker/build-push-action@v3
        with:
          context: ./data_pipelines/dataeng_github_metrics/
          file: ./data_pipelines/dataeng_github_metrics/Dockerfile
          push: true # Will only build if this is not here
          tags: |
            ghcr.io/mirantis/dataeng_github_metrics:latest
          # TODO: I CANNOT USE DATAENG AS PUBLIC AND NEED TO CHANGE THE WAY GITCONFIG IS USED IN THE DOCKERFILE FOR AUTHENTICATION
          secrets: |
            TOKEN=${{ secrets.AUTOMATION_PAT}}

以下是前Dockerfile名:

###############
# CACHE IMAGE #
###############
ARG GO_IMAGE=golang:1.17.3-alpine3.14
ARG BASE_IMAGE=alpine:3.14.2

FROM ${GO_IMAGE} AS cache
# Add the keys
ARG GITHUB_ID
ENV GITHUB_ID=$GITHUB_ID
ARG GITHUB_TOKEN
ENV GITHUB_TOKEN=$GITHUB_TOKEN

# Install Git
RUN apk add git

# TODO: ENCRYPT THE GITHUB_ID AND GITHUB_TOKEN
# Make Git Configuration
RUN git config \
    --global \
    url."https://${GITHUB_ID}:${GITHUB_TOKEN}@github.com/".insteadOf \
    "https://github.com/"

WORKDIR /src
COPY go.mod go.sum /src/
RUN go mod download

##############
# BASE IMAGE #
##############
FROM cache AS dataeng_github_metrics
COPY . /bin
WORKDIR /bin

# Setup Git Terminal Prompt & Go Build
RUN go build .

###############
# FINAL IMAGE #
###############
FROM ${BASE_IMAGE}
COPY --from=dataeng_github_metrics /bin/dataeng_github_metrics bin/
ENTRYPOINT [ "bin/dataeng_github_metrics" ]

我认为让我感到困惑的重要部分是这一点,但我想知道是否有更好的方法来实现它:

# Make Git Configuration
RUN git config \
    --global \
    url."https://${GITHUB_ID}:${GITHUB_TOKEN}@github.com/".insteadOf \
    "https://github.com/"

如何访问私有存储库并避免工作流中的以下错误:

#14 9.438   remote: Repository not found.
#14 9.438   fatal: Authentication failed for 'https://github.com/Mirantis/dataeng/'
------
Dockerfile:26
--------------------
  24 |     WORKDIR /src
  25 |     COPY go.mod go.sum /src/
  26 | >>> RUN go mod download
  27 |     
  28 |     ##############
--------------------
ERROR: failed to solve: process "/bin/sh -c go mod download" did not complete successfully: exit code: 1
Error: buildx failed with: ERROR: failed to solve: process "/bin/sh -c go mod download" did not complete successfully: exit code: 1

推荐答案

Dockerfile中,为了使用操作传递的密码(称为TOKEN),您应该按如下方式运行:

RUN --mount=type=secret,id=TOKEN \
    echo "machine github.com login x password $(head -n 1 /run/secrets/TOKEN)" > ~/.netrc && \
git config \
    --global \
    url."https://${GITHUB_ID}:${TOKEN}@github.com/".insteadOf \
    "https://github.com/"

记住也要将GITHUB_ID传递给dockerfile

Go相关问答推荐

读取JSON数据并在网页上显示

Go SQLCMD比Windows本机版本慢吗?

Go中的Slice[:1][0]与Slice[0]

";无效的复制因子;融合Kafka Go客户端

如何在 Chi Router 的受保护路由下提供静态文件(尤其是图像)?

该文件位于模块.内,该模块不包含在您的工作区中

在本地 go 应用程序上获取秘密的正确策略

缺少签名帮助文档

如何使用 go-git 将特定分支推送到远程

helm :将 YAML 转换为 JSON 时出错:yaml:第 xx 行:未找到预期的密钥

无法使用 gocsv 读取引用字段

在 Golang 模板中计算时间/持续时间

如何编写一个以字符串或错误为参数的通用函数?

github.com/rs/zerolog 字段的延迟判断

在 GORM 中,如何在特定时区配置 autoCreateTime 和 autoUpdateTime?

将接口方法的参数限制为几个允许的 struct ?

如何从 tinygo webassembly 目标返回对象

实现接口的指针的泛型类型是什么?

泛型:对具有返回自身的函数的类型的约束

Go 1.18 泛型如何使用接口定义新的类型参数