其他分享
首页 > 其他分享> > golang 官方依赖管理工具 dep 使用和持续集成

golang 官方依赖管理工具 dep 使用和持续集成

作者:互联网

介绍

go dep 依赖管理工具是为应用管理代码的,go get是为GOPATH管理代码的

官方地址

官方说明为啥要统一依赖管理

dep 需要在Go 1.7及更高的版本中使用

安装

本文使用 golang 版本是 go1.9.3 需要自己安装 dep

go get -v -u github.com/golang/dep/cmd/dep

基础帮助参数

dep
Dep is a tool for managing dependencies for Go projects
Usage: "dep [command]"
Commands:
  init Set up a new Go project, or migrate an existing one
  status Report the status of the project's dependencies
  ensure Ensure a dependency is safely vendored in the project
  prune Pruning is now performed automatically by dep ensure.
  version Show the dep version information
Examples:
  dep init set up a new project
  dep ensure install the project's dependencies
  dep ensure -update update the locked versions of all dependencies
  dep ensure -add github.com/pkg/errors add a dependency to the project
Use "dep help [command]" for more information about a command.

ensure 确保,确保所有本地状态-代码树、依赖列表、锁、远端oss彼此同步

使用

初始化

dep init -v # 更建议使用,因为会很漫长

会生成两个文件 Gopkg.lockGopkg.toml 和一个目录 vendor

如果报错 Gopkg.toml and Gopkg.lock are out of sync 需要执行一下 dep ensure -v

其中

依赖管理

# 依赖管理帮助
dep help ensure
# 添加一条依赖
dep ensure -add github.com/bitly/go-simplejson
# 这里 @= 参数指定的是 某个 tag
dep ensure -add github.com/bitly/go-simplejson@=0.4.3
# 添加后一定记住执行 确保 同步
dep ensure
# 同理建议使用
dep ensure -v
# 更新依赖
dep ensure -update -v
#  删除没有用到的 package
dep prune -v

依赖更新

编辑 Gopkg.toml 文件,配置新的依赖,然后执行

dep ensure -update -v && dep ensure -v

注意,这个和更新依赖是完全不同的概念,因为 dep 依赖 vendor 目录,所以会出现一种奇怪的情况
toml 文件修改了,但是依赖没有在 vendor 目录出现,这点你需要学习 node_module 修复大法
删除 vendor 目录中对应的内容,或者干掉整个 vendor 目录

dep 管理存在的问题

临时解决方法是修改 Gopkg.toml 将有问题的仓库 version 注释掉,branch 切换到可以的分支

[[constraint]]
  branch = "master"
  name = "github.com/xx/xxx"
  #version = ""

最好的方法还是给依赖仓库打 tag

持续集成使用 dep 管理依赖

这里就不介绍一个 dep 管理实例了,因为环境不一样,使用的CI CD链不一样,除了脚本可以通用,其他内容都超过了本文的范围,所以主要讲些原则上的事情

持续集成时,使用 dep 的基本原则

代码提交版本管理

我倾向于选择提交全部 vendor, 原因是远程仓库的依赖拉取非常慢,容易拖慢构建
如果git 仓库压力很大,可以做缓存仓库迁移分离压力,当然得写脚本批量修改 golang 代码的引用

当然你还有一个选择,将 vendor 做成一个共享目录,编译时挂载到编译工程,这样既让 git 仓库的压力变小,也不被远程仓库网速问题困扰,缺点就是得额外维护脚本

构建过程的控制

docker容器服务构建样例

标签:依赖,vendor,dep,管理工具,仓库,golang,Gopkg,ensure
来源: https://blog.csdn.net/cpongo6/article/details/89249341