Quantcast
Channel: 小惡魔 – 電腦技術 – 工作筆記 – AppleBOY
Viewing all articles
Browse latest Browse all 325

在 Jenkins 跑 Golang 測試

$
0
0
Go-brown-side.sh 本篇會紀錄如何在 Jenkins 測試 Golang 專案,直接拿 go-hello 當作本篇範例。

安裝 Jenkins

請直接參考官網 Wiki 安裝方式,完成後可以發現預設跑在 8080 port,搭配 Nginx 設定就可以跑在 80 port。
server {

  listen 80;
  server_name your_host_name;

  location / {

    proxy_set_header        Host $host;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Proto $scheme;

    # Fix the "It appears that your reverse proxy set up is broken" error.
    proxy_pass          http://127.0.0.1:8081;
    proxy_read_timeout  90;
  }
}

安裝 Golang Plugin

請先到 Manage Jenkins > Manage Plugins > Available 找到 Go Plugin,安裝後,請到 Global Tool Configuration 內把要測試的版本安裝到 Jenkins 內 golang

設定 Jenkins 專案

要跑 golang 測試非常簡單,只需要兩個指令就可以測試
$ go get -t -d -v ./...
$ go test -v -cover ./...
設定完成後,跑測試,會出現底下錯誤訊息
package github.com/gin-gonic/gin: cannot download, $GOPATH not set. For more details see: go help gopath
可以發現 jenkins 預設不會將 $GOPATH 設定好,所以必須手動將 $GOPATH 相關目錄設定完成,請不要將 $WORKSPACE 設定成 $GOPATH,因為 $WORKSPACE 目錄底下包含 src, pkg, bin 三大目錄,請將底下 Script 設定到專案內
export GOPATH=$WORKSPACE/gopath
export PATH=$GOPATH/bin:$PATH
mkdir -p $GOPATH/bin
mkdir -p $GOPATH/src/github.com/appleboy/go-hello
rsync -az --exclude="gopath" ${WORKSPACE}/ $GOPATH/src/github.com/appleboy/go-hello
從上面可以發現,我們手動建立了 gopath 目錄來指定為 $GOPATH,並且將專案的目錄都設定好後,把 $WORKSPACE 內所有檔案透過 rsync 方式丟到 $GOPATH 內,這樣就可以進行測試了。export 可以用 EnvInject Plugin 取代。

結論

很多人會問為什麼 $GOPATH 是指定在 $WORKSPACE/gopath 而不是 $GOROOT/gopath,如果採用後者的話,這樣多個專案使用不同版本的第三方套件就會出問題,而且共用目錄會造成蠻多額外問題,也無法在測試後完整刪除相關目錄。將 $GOPATH 設定在專案內就可以透過 Workspace Cleanup Plugin 直接清除,讓每次測試環境都是非常乾淨。

Viewing all articles
Browse latest Browse all 325

Trending Articles


Girasoles para colorear


mayabang Quotes, Torpe Quotes, tanga Quotes


Tagalog Quotes About Crush – Tagalog Love Quotes


OFW quotes : Pinoy Tagalog Quotes


Long Distance Relationship Tagalog Love Quotes


Tagalog Quotes To Move on and More Love Love Love Quotes


5 Tagalog Relationship Rules


Best Crush Tagalog Quotes And Sayings 2017


Re:Mutton Pies (lleechef)


FORECLOSURE OF REAL ESTATE MORTGAGE


Sapos para colorear


tagalog love Quotes – Tiwala Quotes


Break up Quotes Tagalog Love Quote – Broken Hearted Quotes Tagalog


Patama Quotes : Tagalog Inspirational Quotes


Pamatay na Banat and Mga Patama Love Quotes


Tagalog Long Distance Relationship Love Quotes


BARKADA TAGALOG QUOTES


“BAHAY KUBO HUGOT”


Vimeo 10.7.0 by Vimeo.com, Inc.


Vimeo 10.7.1 by Vimeo.com, Inc.