亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

go mod 使用私有g(shù)itlab群組的解決方案

 更新時(shí)間:2021年05月06日 09:35:24   作者:半山無(wú)極  
這篇文章主要介紹了go mod 使用私有g(shù)itlab群組的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

由于go對(duì)私有g(shù)itlab的倉(cāng)庫(kù)支持不好,得使用下面這些步驟

設(shè)置git使用 ssh協(xié)議

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

添加ssh key 到gitlab

ssh-keygen 會(huì)生成 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

設(shè)置noproxy域名

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

設(shè)置private域名

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

自己搭建的gitlab也是如此!

補(bǔ)充:Go Module訪問(wèn)私有Git倉(cāng)庫(kù)

Go Module 極大地改進(jìn)了Go中依賴的管理過(guò)程。如果您是Go模塊的新手,希望閱讀更多關(guān)于如何入門Go module內(nèi)容,請(qǐng)查看官方文檔

一旦配置正確,就可以很容易地從公共倉(cāng)庫(kù)引入特定版本的Go包。一個(gè)典型的例子如下所示:

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
)

如果想擴(kuò)展包的引入范圍到私有代碼庫(kù)中,該如何處理呢?實(shí)際上也很簡(jiǎn)單,確保您的Go安裝能夠訪問(wèn)私有Git倉(cāng)庫(kù)即可。但是具體怎么做呢?

私有倉(cāng)庫(kù)

在底層,Go使用Git來(lái)獲取指定版本的依賴模塊。因此,無(wú)論Go運(yùn)行在哪里(Docker容器或者筆記本電腦中)必須有權(quán)限訪問(wèn)私有存儲(chǔ)庫(kù)。

幸運(yùn)的是,有一個(gè)Git命令可以解決這個(gè)問(wèn)題。下面的命令將在.gitconfig文件中添加一個(gè)條目,告訴Git使用帶有憑證格式的URL來(lái)訪問(wèn)標(biāo)準(zhǔn)的URL。使用私有token代替密碼,是因?yàn)樾枰诩兾谋井?dāng)中存儲(chǔ)的。關(guān)于這方面的討論可以查看Stack Overflow。

導(dǎo)入須知:

認(rèn)證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服務(wù)器替換URL中privategitlab.com。

這種配置方式對(duì)于本地開發(fā)很好用,但是在CI/CD流水線上面會(huì)怎么樣呢?如下是一個(gè)Dockerfile的例子允許在構(gòu)建時(shí)注入憑證:

# ---------------------------------------------------------------------
#  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,所以這里有一個(gè)例子,將使用它來(lái)運(yùn)行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...

當(dāng)然,Jenkins或Travis或者其他任何方式只要在構(gòu)建Docker鏡像時(shí)可提供build參數(shù),這樣Go模塊就可以在不被討厭的身份驗(yàn)證阻塞情況下完成其工作。

另一種選擇:SSH

另一種設(shè)置方法是使用你的SSH密匙連接,并像下面這樣設(shè)置你的.gitconfig確保每次引入包使用SSH:

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

我個(gè)人發(fā)現(xiàn),當(dāng)遇到問(wèn)題時(shí),這種設(shè)置調(diào)試很困難,因此我更偏向使用auth Token URL。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

最新評(píng)論