• 企业400电话
  • 网络优化推广
  • AI电话机器人
  • 呼叫中心
  • 全 部 栏 目

    网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    go mod 使用私有gitlab群组的解决方案
    POST TIME:2021-10-18 17:05

    由于go对私有gitlab的仓库支持不好,得使用下面这些步骤

    设置git使用 ssh协议

    git config --global url."git@gitlab.com:".insteadOf https://gitlab.com/

    添加ssh key 到gitlab

    ssh-keygen 会生成 id_rsa.pub

    cat ~/.ssh/id_rsa.pub 粘贴到gitlab 右上角头像 Setting -> SSH keys,或者打开链接https://gitlab.com/profile/keys

    修改 go.mod 添加

    replace gitlab.com/YourGroup/SubGroup/Project => gitlab.com/YourGroup/SubGroup/Project.git master

    设置noproxy域名

    go env -w GONOPROXY=\*\*.gitlab.com\*\*

    设置private域名

    go env -w GOPRIVATE=\*\*.gitlab.com\*\*

    自己搭建的gitlab也是如此!

    补充:Go Module访问私有Git仓库

    Go Module 极大地改进了Go中依赖的管理过程。如果您是Go模块的新手,希望阅读更多关于如何入门Go module内容,请查看官方文档

    一旦配置正确,就可以很容易地从公共仓库引入特定版本的Go包。一个典型的例子如下所示:

    module github.com/samplerepo/sampleproject
    go 1.12
    require (
        github.com/pkg/errors v0.8.0
        github.com/spf13/cobra v0.0.4
        github.com/spf13/viper v1.3.2
    )
    

    如果想扩展包的引入范围到私有代码库中,该如何处理呢?实际上也很简单,确保您的Go安装能够访问私有Git仓库即可。但是具体怎么做呢?

    私有仓库

    在底层,Go使用Git来获取指定版本的依赖模块。因此,无论Go运行在哪里(Docker容器或者笔记本电脑中)必须有权限访问私有存储库。

    幸运的是,有一个Git命令可以解决这个问题。下面的命令将在.gitconfig文件中添加一个条目,告诉Git使用带有凭证格式的URL来访问标准的URL。使用私有token代替密码,是因为需要在纯文本当中存储的。关于这方面的讨论可以查看Stack Overflow。

    导入须知:

    认证token必须是URL编码的

    以下gits使用反斜杠处理,在不同行中显示:

    BitBucket

    git config \
    
      --global \
    
      url."https://${bitbucket_id}:${bitbucket_token}@privatebitbucket.com".insteadOf \
    
      https://privatebitbucket.com

    GitHub

    git config \
    
      --global \
    
      url."https://${user}:${personal_access_token}@github.com".insteadOf \
    
      https://github.com

    Gitlab

    git config \
    
      --global \
    
      url."https://oauth2:${personal_access_token}@privategitlab.com".insteadOf \
    
      "https://privategitlab.com"
    #or 
    git config \
    
      --global \
    
      url."https://${user}:${personal_access_token}@privategitlab.com".insteadOf \
    
      https://privategitlab.com

    同样需要使用私有Gitlab服务器替换URL中privategitlab.com。

    这种配置方式对于本地开发很好用,但是在CI/CD流水线上面会怎么样呢?如下是一个Dockerfile的例子允许在构建时注入凭证:

    # ---------------------------------------------------------------------
    #  The first stage container, for building the application
    # ---------------------------------------------------------------------
    FROM golang:1.12.1-stretch as builder
    COPY . /app
    # Add the keys
    ARG bitbucket_id
    ENV bitbucket_id=$bitbucket_id
    ARG bitbucket_token
    ENV bitbucket_token=$bitbucket_token
    WORKDIR /app/cmd/webapp
    RUN git config \
    
        --global \
    
        url."https://${bitbucket_id}:${bitbucket_token}@privatebitbucket.com/".insteadOf \
    
        "https://privatebitbucket.com/"
    RUN GIT_TERMINAL_PROMPT=1 \
    
        GOARCH=amd64 \
    
        GOOS=linux \
    
        CGO_ENABLED=0 \
    
        go build -v --installsuffix cgo --ldflags="-s" -o myapp
    # ---------------------------------------------------------------------
    #  The second stage container, for running the application
    # ---------------------------------------------------------------------
    FROM alpine:3.8
    COPY --from=builder /app/cmd/webapp/myapp /app/myapp
    WORKDIR /app
    ENTRYPOINT ["/myapp"]
    

    我喜欢使用docker compose,所以这里有一个例子,将使用它来运行Dockerfile:

    version: '3.0'
    services:
      app:
        container_name: my_go_app_container
        build:
          # context can/may/will be different per-project setup
          context: ../
          dockerfile: GitDockerfile
          args:
            - bitbucket_id=private_user
            - bitbucket_token=private_token
        image: my_go_app_image
        # other configs...

    当然,Jenkins或Travis或者其他任何方式只要在构建Docker镜像时可提供build参数,这样Go模块就可以在不被讨厌的身份验证阻塞情况下完成其工作。

    另一种选择:SSH

    另一种设置方法是使用你的SSH密匙连接,并像下面这样设置你的.gitconfig确保每次引入包使用SSH:

    git config \
    
      --global \
    
      url."git@github.com".insteadOf \
    
      https://github.com

    我个人发现,当遇到问题时,这种设置调试很困难,因此我更偏向使用auth Token URL。

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

    您可能感兴趣的文章:
    • Golang 编译成DLL文件的操作
    • golang调用c实现的dll接口细节分享
    • Golang如何调用windows下的dll动态库中的函数
    • django将图片保存到mysql数据库并展示在前端页面的实现
    • golang实践-第三方包为私有库的配置方案
    • 解决go mod私有仓库拉取的问题
    • go语言中fallthrough的用法说明
    上一篇:golang实践-第三方包为私有库的配置方案
    下一篇:Golang如何调用windows下的dll动态库中的函数
  • 相关文章
  • 

    关于我们 | 付款方式 | 荣誉资质 | 业务提交 | 代理合作


    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    X

    截屏,微信识别二维码

    微信号:veteran88

    (点击微信号复制,添加好友)

     打开微信