go mod 使用私有g(shù)itlab群組的解決方案
由于go對私有g(shù)itlab的倉庫支持不好,得使用下面這些步驟
設(shè)置git使用 ssh協(xié)議
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
設(shè)置noproxy域名
go env -w GONOPROXY=\*\*.gitlab.com\*\*
設(shè)置private域名
go env -w GOPRIVATE=\*\*.gitlab.com\*\*
自己搭建的gitlab也是如此!
補(bǔ)充:Go Module訪問私有Git倉庫
Go Module 極大地改進(jìn)了Go中依賴的管理過程。如果您是Go模塊的新手,希望閱讀更多關(guān)于如何入門Go module內(nèi)容,請查看官方文檔
一旦配置正確,就可以很容易地從公共倉庫引入特定版本的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
)
如果想擴(kuò)展包的引入范圍到私有代碼庫中,該如何處理呢?實(shí)際上也很簡單,確保您的Go安裝能夠訪問私有Git倉庫即可。但是具體怎么做呢?
私有倉庫
在底層,Go使用Git來獲取指定版本的依賴模塊。因此,無論Go運(yùn)行在哪里(Docker容器或者筆記本電腦中)必須有權(quán)限訪問私有存儲庫。
幸運(yùn)的是,有一個Git命令可以解決這個問題。下面的命令將在.gitconfig文件中添加一個條目,告訴Git使用帶有憑證格式的URL來訪問標(biāo)準(zhǔn)的URL。使用私有token代替密碼,是因?yàn)樾枰诩兾谋井?dāng)中存儲的。關(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。
這種配置方式對于本地開發(fā)很好用,但是在CI/CD流水線上面會怎么樣呢?如下是一個Dockerfile的例子允許在構(gòu)建時注入憑證:
# ---------------------------------------------------------------------
# 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,所以這里有一個例子,將使用它來運(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鏡像時可提供build參數(shù),這樣Go模塊就可以在不被討厭的身份驗(yàn)證阻塞情況下完成其工作。
另一種選擇:SSH
另一種設(shè)置方法是使用你的SSH密匙連接,并像下面這樣設(shè)置你的.gitconfig確保每次引入包使用SSH:
git config \ --global \ url."git@github.com".insteadOf \ https://github.com
我個人發(fā)現(xiàn),當(dāng)遇到問題時,這種設(shè)置調(diào)試很困難,因此我更偏向使用auth Token URL。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
go語言中數(shù)據(jù)接口set集合的實(shí)現(xiàn)
set集合是一種常見的數(shù)據(jù)結(jié)構(gòu),它代表了一個唯一元素的集合,本文主要介紹了set的基本特性,包括唯一性、無序性、可變性和集合運(yùn)算,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10
Go-家庭收支記賬軟件項(xiàng)目實(shí)現(xiàn)
這篇文章主要介紹了Go-家庭收支記賬軟件項(xiàng)目實(shí)現(xiàn),本文章內(nèi)容詳細(xì),具有很好的參考價值,希望對大家有所幫助,需要的朋友可以參考下2023-01-01
Golang多線程下載器實(shí)現(xiàn)高效快速地下載大文件
Golang多線程下載器是一種高效、快速地下載大文件的方法。Golang語言天生支持并發(fā)和多線程,可以輕松實(shí)現(xiàn)多線程下載器的開發(fā)。通過使用Golang的協(xié)程和通道,可以將下載任務(wù)分配到多個線程中并行處理,提高了下載的效率和速度2023-05-05

