在 Docker 中使用私有 gitlab 模块构建 Go 应用程序
时间:2023-04-13
浏览:232
作者:佚名
今日不肯埋头,明日何以抬头!每日一句努力自己的话哈哈~哈喽,今天我将给大家带来一篇《在 Docker 中使用私有 gitlab 模块构建 Go 应用程序》,主要内容是讲解golang等等,感兴趣的朋友可以收藏或者有更好的建议在评论提出,我都会认真看的!大家一起进步,一起学习!
问题内容
我正在尝试在 docker 文件上构建我的 go 应用程序。在我的 go.mod 中有一个需要身份验证/ssh 的私有包。这个问题类似于[在 Docker
中使用私有模块构建 Go 应用程序](https://stackoverflow.com/questions/62960934/building-go-
apps-with-private-modules-in-docker),但在我的情况下,我必须从gitlabnot
from中提取包github。这是我的码头文件:
# builder image FROM golang:1.14.11-alpine AS builder # specific directory for build process WORKDIR /usr/src/build # copying the source code # to the current working directory COPY . . RUN apk add --no-cache openssh-client RUN apk add --no-cache git # create ssh directory RUN mkdir ~/.ssh RUN touch ~/.ssh/known_hosts RUN ssh-keyscan -t rsa gitlab.com >> ~/.ssh/known_hosts # allow private repo pull RUN git config --global url."https://my-personal-access-token:token@gitlab.com/".insteadOf "https://gitlab.com/" ADD . /go/src/gitlab.com/my-repo/backends/backend-structs CMD cd /go/src/gitlab.com/my-repo/backends/backend-structs; go get /go/src/gitlab.com/my-repo/backends/backend-structs && go build -o /go/bin/backend-structs # executing build process RUN GOOS=linux go build -ldflags="-s -w" -o app # runtime image FROM golang:1.14.11-alpine AS runtime # create and use non-root user # to increase container security # ref https://pythonspeed.com/articles/root-capabilities-docker-security/ RUN adduser myuser --disabled-password USER myuser WORKDIR /home/myuser # copy the executable binary file from builder directory # to the current working directory COPY --from=builder /usr/src/build/app . # exposing port EXPOSE 8080 # run the application CMD ["./app"]
我试图按照本教程https://divan.dev/posts/go_get_private/更改github.com为gitlab.com仍然失败。
以下是错误详情:
#17 5.830 remote: HTTP Basic: Access denied #17 5.830 fatal: Authentication failed for 'https://gitlab.com/my-repo/backends.git/' ------ executor failed running [/bin/sh -c GOOS=linux go build -ldflags="-s -w" -o app]: exit code: 1
这里的任何人都知道如何使用 golang 私有包创建 dockerfile(repo 托管在 gitlab.com 中)?
正确答案
根据我的经验,不要使用 git configs 来解决这个问题。仅使用~/.netrc. 这是专门为此制作的指南:https
://gist.github.com/MicahParks/1ba2b19c39d1e5fccc3e892837b10e21
我也会在下面粘贴它的内容。
问题
go命令行工具需要能够从您的私有 GitLab 中获取依赖项,但需要进行身份验证。
这假设您的私人 GitLab 托管在privategitlab.company.com.
环境变量
推荐使用以下环境变量:
export GO111MODULE=on export GOPRIVATE=privategitlab.company.com
以上几行可能最适合您的 shell 启动,例如~/.bashrc.
解释
GO111MODULE=on告诉 Golang 命令行工具你正在使用模块。我没有在私有 GitLab 上使用不使用 Golang
模块的项目对此进行测试。
GOPRIVATE=privategitlab.company.com告诉 Golang
命令行工具不要为列出的主机名使用公共互联网资源(如公共模块代理)。
从您的私人 GitLab 获取个人访问令牌
为了将来证明这些说明,请遵循[GitLab
文档中的本指南](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html#creating-
a-personal-access-token)。我知道read_apiGolang
命令行工具需要范围才能工作,我也可能怀疑read_repository,但尚未证实这一点。
设置~/.netrc
为了让 Golang 命令行工具向 GitLab 进行身份验证,~/.netrc最好使用文件。
如果文件不存在,要创建该文件,请运行以下命令:
touch ~/.netrc chmod 600 ~/.netrc
现在编辑文件的内容以匹配以下内容:
machine privategitlab.company.com login USERNAME_HERE password TOKEN_HERE
WhereUSERNAME_HERE替换为您的 GitLab 用户名,并TOKEN_HERE替换为上一节中获取的访问令牌。
常见错误
不要 使用 以下内容设置全局 git 配置:
git config --global url."git@privategitlab.company.com:".insteadOf "https://privategitlab.company.com"
我相信在撰写本文时,Golang 命令行工具不完全支持 SSH git,这可能会导致与~/.netrc.
奖励:SSH 配置文件
对于该git工具的常规使用,而不是 Golang 命令行工具,最好~/.ssh/config设置一个文件。为此,请运行以下命令:
mkdir ~/.ssh chmod 700 ~/.ssh touch ~/.ssh/config chmod 600 ~/.ssh/config
请注意,以上文件和目录的权限是 SSH 在大多数 Linux 系统上以默认配置工作的必要条件。
然后,编辑~/.ssh/config文件以匹配以下内容:
Host privategitlab.company.com Hostname privategitlab.company.com User USERNAME_HERE IdentityFile ~/.ssh/id_rsa
请注意上述文件中的间距很重要,如果不正确将使文件无效。
USERNAME_HERE您的 GitLab 用户名在哪里,并且是文件系统中SSH 私钥~/.ssh/id_rsa的路径。您已经将其公钥上传
到 GitLab。这里有[一些说明](https://docs.gitlab.com/ee/ssh/README.html#adding-an-ssh-
key-to-your-gitlab-account)。 __
__[](https://docs.gitlab.com/ee/ssh/README.html#adding-an-ssh-key-to-your-
gitlab-account)
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。
本文内容来源于互联网,如有侵权请联系删除。
近期热门
站内热门
最新文章
热门专题
