有使用 Gitlab 的朋友對於 Gitlab-CI 應該都不陌生,透過 CI 可以幫助開發者進行測試或是部署,相當的方便,但往往在執行 CI 的時候出錯要 debug 都要一直 push code 很麻煩, Gitlab 有將他的 runner 開源並提供方法讓大家在自己的環境安裝 gitlab-runner
,下面就來跟大家說怎麼安裝。
如果你只是想要使用 gitlab-runner
的指令,可以直接透過這份文件進行安裝即可,但如果你會有需要使用不同版本的 gitlab-runner
的需求,就可以參考這份文件進行操作,如果此環境你沒有 sudo 權限或是想要看懶人版可以直接往下看 xD
gitlab-runner
是使用 Golang 寫的,所以需要先進行 Golang 相關配置與安裝。
更新你的 .profile
, .bash_profile
, .zshrc
看習慣放哪裡,我自己都會放在 .zshrc
中
> vim ~/.zshrc
...
export GOPATH=$HOME/Go
export PATH=$PATH:$GOPATH/bin:$HOME/local/go/bin
...
> exec zsh
> mkdir ~/Go
> mkdir ~/local
下載與安裝 Golang 。
> cd
> wget https://storage.googleapis.com/golang/go1.10.8.linux-amd64.tar.gz
> tar -C local -xzf go1.10.8.linux-amd64.tar.gz
完成之後你就會在你的 ~/local/go
這邊看到 go 的原始碼,也可以開始使用 go 了,接著來確認是否有成功安裝
> go version
go version go1.10.8 linux/amd64
看到上面訊息代表你的 go 語言有成功的安裝,接下來安裝 gitlab-runner
> go get gitlab.com/gitlab-org/gitlab-runner
執行完成後你可以看到 ~/Go/bin
裡面有個 gitlab-runner
的執行檔,就可以直接使用了,或是要使用 $GOPATH/src/gitlab.com/gitlab-org/gitlab-runner/.gopath/bin/gitlab-runner
也可以,至於為什麼要知道後面這個路徑呢?後面會提到,測試一下 gitlab-runner
是否安裝成功。
╭─hashmanlin@ubuntu ~/Go/bin
╰─➤ gitlab-runner -v
Version: development version
Git revision: HEAD
Git branch: HEAD
GO version: go1.10.8
Built: unknown
OS/Arch: linux/amd64
這邊可以看到安裝成功了,但會發現 Version 是 development version ,如果希望指定版本怎麼辦?舉例想要使用 11.11.4 的版本。
╭─hashmanlin@ubuntu ~/Go/src/gitlab.com/gitlab-org/gitlab-runner ‹master›
╰─➤ git checkout v11.11.4
Note: switching to 'v11.11.4'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at e828d3bc9 Update CHANGELOG for v11.11.4
╭─hashmanlin@ubuntu ~/Go/src/gitlab.com/gitlab-org/gitlab-runner ‹e828d3bc9›
╰─➤ make deps
mkdir -p /home/hashmanlin/Go/src/gitlab.com/gitlab-org/gitlab-runner/.gopath/bin
touch /home/hashmanlin/Go/src/gitlab.com/gitlab-org/gitlab-runner/.gopath/.ok
go get github.com/golang/dep/cmd/dep
go get github.com/mitchellh/gox
go get github.com/vektra/mockery/.../
╭─hashmanlin@ubuntu ~/Go/src/gitlab.com/gitlab-org/gitlab-runner ‹e828d3bc9›
╰─➤ make install
go install --ldflags="-X gitlab.com/gitlab-org/gitlab-runner/common.NAME=gitlab-runner -X gitlab.com/gitlab-org/gitlab-runner/common.VERSION=11.11.4 -X gitlab.com/gitlab-org/gitlab-runner/common.REVISION=e828d3bc -X gitlab.com/gitlab-org/gitlab-runner/common.BUILT=2019-12-02T12:42:34+0000 -X gitlab.com/gitlab-org/gitlab-runner/common.BRANCH= -s -w" gitlab.com/gitlab-org/gitlab-runner
╭─hashmanlin@ubuntu ~/Go/src/gitlab.com/gitlab-org/gitlab-runner ‹e828d3bc9›
╰─➤ ./.gopath/bin/gitlab-runner -v
Version: 11.11.4
Git revision: e828d3bc
Git branch:
GO version: go1.10.8
Built: 2019-12-02T12:42:34+0000
OS/Arch: linux/amd64
至於為什麼 $GOPATH/bin/gitlab-runner
沒有被置換成 11.11.4 的版本呢?看了一下 gitlab-runner
的 Makefile
會把 $GOPATH
改為 .gopath
這個路徑,如果希望 $GOPATH/bin/gitlab-runner
也跟著置換可以考慮用 ln
的方式來處理。
╭─hashmanlin@ubuntu ~/Go/bin
╰─➤ rm gitlab-runner
╭─hashmanlin@ubuntu ~/Go/bin
╰─➤ rm gitlab-runner
╭─hashmanlin@ubuntu ~/Go/bin
╰─➤ ln /home/hashmanlin/Go/src/gitlab.com/gitlab-org/gitlab-runner/.gopath/bin/gitlab-runner gitlab-runner
╭─hashmanlin@ubuntu ~/Go/bin
╰─➤ cd
╭─hashmanlin@ubuntu ~
╰─➤ gitlab-runner -v
Version: 11.11.4
Git revision: e828d3bc
Git branch:
GO version: go1.10.8
Built: 2019-12-02T12:42:34+0000
OS/Arch: linux/amd64
這樣就可以執行特定版本的 gitlab-runner
囉