搜索
您的当前位置:首页正文

Golang项目如何集成 circleci

来源:哗拓教育

转自

想写出好项目,肯定要在项目的初期就把架子搭好。所以我们的目标就是:如何在不花钱的前提下,给 Github Private 仓库的项目集成 CI, Code Review 等功能。

免费 APP

先去 Github Marketplace 中选购一番。

Dependabot

Every day, Dependabot checks your dependency files for outdated requirements and opens individual pull requests for any it finds. You review the PRs, merge them, and get to work on the latest, most secure releases.

监控项目的 go.mod, go.sum 中是否存在不安全的 package,并自动 PR 帮忙修正。Github 自己也在用的东西,免费当然要加上。

从 Github 的 Marketplace 找到这个服务,一路点下去,各种授权就行。选择要监控的项目和语言。我选择了 Go ModulesDocker

进入到 dependabot 的站点上可以进行一些更详细的设置,比如更改时间为每周检查一次。


CodeFactor

CodeFactor instantly performs Code Review with every GitHub Commit or PR.

它集成了 Yamllint, Hadolint, Vet, stylelint, Duplication checker 几项功能,并且免费版支持一个 Private 仓库。

安装方法和 Dependabot 一样,从 Github Marketplace 中一路点下去,最后去 codefactor 的网站进行一些设置。

添加徽章

进入 CodeFactor 网站,对应项目的右上角提供各种格式的徽章代码。

NewRelic

免费版的请求分析,数据事务,机器运行状况,错误报警等足够用了。

注册一个免费帐号,根据官网 Get Start 选 Go 一路走下去,拿到 LicenseKey。

// Middleware creates a Gin middleware that instruments requests by New Relic.
cfg := newrelic.NewConfig("Gin App", conf.NewRelicLicense)
// You can print log by: cfg.Logger = newrelic.NewDebugLogger(os.Stdout)
app, err := newrelic.NewApplication(cfg)
if err != nil {
  log.Printf("failed to make new_relic app: %v", err)
} else {
  router.Use(nrgin.Middleware(app))
}

第一次用 Go + Gin 安装 NewRelic 的探针,参考了一些文章:

  • - 过时了,不用看
  • - 社区的这个比较靠谱
  • - 最终还是用的官方的例子

CircleCI

基础配置

想找个免费好看的且跟 Golang 集成比较完美的 CI 不容易啊。在 Marketplace 中发现 CircleCI 的免费套餐支持一个 Private 仓库,每个月有 1000 分钟的时间。

# Golang CircleCI 2.0 configuration file
version: 2
jobs:
  build:
    working_directory: 

    docker:
      - image: circleci/golang:1.12
      - image: circleci/mysql:5.7
        environment:
          MYSQL_ROOT_PASSWORD: password
          MYSQL_DATABASE: test_db

    steps:
      - checkout

      - run:
          name: Wait for DB
          command: dockerize -wait tcp://127.0.0.1:3306 -timeout 1m

      - run: go get -v -t -d ./...

      - run:
          name: Unit test
          command: go test -v ./...

在配置 MySQL 时遇到了一些问题,在连接数据库时候报密码错误。

CircleCI + dredd

但全局安装仍然有问题,后来只能通过添加 package.json 的方案,不用全局安装的方式。

- run:
    name: npm install dredd
    command: npm install dredd@11.2.1

GolangCI-Lint

补充

  1. 参考 添加一些配置,可以测试 server 是否运行起来。
  2. 参考 区分多套环境。
- run:
    name: Validate service is working
    command: curl --retry 10 --retry-delay 1 --retry-connrefused http://localhost:8080/contacts/test

参考

需要手动安装 CI

Top