day 14 - drone 的go-test & golangci-lint

自己的产出自己负责!每次交付专案之前我都会想到这句话, 是不是该再检查一下/images/emoticon/emoticon37.gif

程序码已经成功的build完之後, 为了确保程序码有经过基本的检查, 可以导入golangci-lint, 它会针对程序码做基本的品质把关, 像是参数没有接、function或参数未被使用...等都可以帮忙检查出来!详细检查项目可以参考golangci-lint

我们团队有自建的drone可以使用, 每次commit都会透过drone自动跑golangci-lint, 本机要跑的话只要在专案底下执行指令就可以透过docker run image 检查:

  • 指令
docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:v1.30.0-alpine \
sh -c "golangci-lint run" -v
  • 执行结果
-> % docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:v1.30.0-alpine \
sh -c "golangci-lint run" -v
helper.go:33:6: `HandlerPanicRecover` is unused (deadcode)
func HandlerPanicRecover(err *error) {
     ^
model/limit.go:11:32: S1019: should use make(map[string]int) instead (gosimple)
        result = make(map[string]int, 0)
                                      ^

不过我没有自己架设过drone, 就来练习一下架设drone的过程吧
要使用到的工具有github帐号, ngrok, drone

  1. 先注册 ngrok
    https://i.imgur.com/JNRQ6Oc.png
  • install ngrok

    -> % brew install ngrok
    Updating Homebrew...
    ==> Auto-updated Homebrew!
    Updated 1 tap (homebrew/core).
    ==> New Formulae
    (... 省略)
    ######################################################################## 100.0%
    Warning: No checksum defined for cask 'ngrok', skipping verification.
    ==> Installing Cask ngrok
    ==> Linking Binary 'ngrok' to '/usr/local/bin/ngrok'
    ?  ngrok was successfully installed!
    
  • 使用 ngrok 启动一个外部可以使用的网址

    ngrok http 80
    

    启动後的Forwarding就是之後设定的drone网址
    https://i.imgur.com/IqtVm4M.png

  1. 按照官网安装 drone
  • 可以直接执行 pull image

    docker pull drone/drone:2
    
  • github 到 Developer Settings 绑定Oauth App(使用ngrok产生的网址)
    https://i.imgur.com/3wTaCR7.png

  • 透过 docker-compose 启动 drone server, 参数要带入对应的github资料

    version: '2'
    
    services:
      drone:
        image: drone/drone:2
        container_name: drone
        ports:
          - 80:80
        volumes:
          - /var/lib/drone:/var/lib/drone/
        restart: always
        environment:
          - DRONE_SERVER_HOST={{DRONE_SERVER_HOST}}
          - DRONE_SERVER_PROTO=http
          - DRONE_RPC_SECRET={{DRONE_RPC_SECRET}}
          - DRONE_GITHUB_CLIENT_ID={{DRONE_GITHUB_CLIENT_ID}}
          - DRONE_GITHUB_CLIENT_SECRET={{DRONE_GITHUB_CLIENT_SECRET}}
    
      drone-runner:
        image: drone/drone-runner-docker:1
        container_name: drone-runner
        restart: always
        depends_on:
          - drone
        volumes:
          - /var/run/docker.sock:/var/run/docker.sock
        environment:
          - DRONE_RPC_HOST=drone
          - DRONE_RPC_PROTO=http
          - DRONE_RPC_SECRET={{DRONE_RPC_SECRET}}
          - DRONE_RUNNER_CAPACITY=2
    
    
  1. 连到ngrok网址, 确认绑定状况:
    https://i.imgur.com/9Uf7Xas.png

  2. github repo 绑定 Webhooks(使用ngrok产生的网址)
    https://i.imgur.com/PmvQvxU.png

  3. 都设定好之後, 推一个commit上去测试drone build
    https://i.imgur.com/jrWnstm.png

  • drone上面会显示执行结果
    https://i.imgur.com/xt4XG2f.png

  • lint 也会提示要调整的部分
    https://i.imgur.com/70l3JSe.png

绑定在drone上面做检查可以确保每个commit都有经过基本的品质验证,
很方便又能避免忘记跑golangci-lint~

参考资料


<<:  鬼故事 - 天才小钓手

>>:  Day14-Go函式function

[Day22] 发送验证信API – views

嗨嗨~~ 夥伴们,大家好,今天我们要来说明的是,发送验证信API的逻辑,以下是我的程序码 程序码 f...

[Android Studio] 每日小技巧 - 在 Project 中定位目前开启的 Class

常常有时候在阅读较大的专案时 没有定位档案位置的功能的话很难找到该 Class 的位置 大家可以找到...

[Angular] Day30. Angular Module(一)

在 Angular 中应用程序是模块化的,Angular 有自己的模块化系统称为 NgModule,...

第三十天:完赛心得

终於啊啊啊啊啊啊啊啊啊! 嗨大家好我是Andy,今天铁人赛终於完赛了,先谢谢大家30天以来的观看,从...

Day 5 hook的前奏 useState

这篇是在讲React的测试,所以就拿个几天篇幅来讲hooks,React在v16.8之後开始支援ho...