CI/CD 是什么這里就不多介紹了。下面是我個人使用 GItHub + Travis 集成的 CI/CD 過程總結(jié)。
開發(fā)語言:golang
開發(fā)環(huán)境:Ubuntu 18.04
1. 安裝 GitHub CI 平臺插件。
github 支持很多的CI應(yīng)用可以跳到 這里進行選擇 我這里選擇的是 Travis CI

點擊 Travis CI 選擇 下面的 Open Source (提供免費版),并點擊 Install it for free。 如下圖:

2. 選擇 github repositories
接著根據(jù)頁面的步驟接著執(zhí)行安裝插件操作即可。最后在GitHub的 settings->Applications 中找到 Travis CI 并添加你的項目到 Travis CI 中。

注意:上面步驟中會需要登錄 Travis CI ,選擇 github 賬號登錄即可。
3. 編寫基本Travis CI 集成文件
進入 Travis CI 平臺, 如果需要登錄,尋找右上角的 Sign in with github 。進入 Travis 中的 Documentation 查看有關(guān) Travis CI 的使用文檔。
集成文檔分為下面幾個步驟: travis.yml 文件是由這些步驟組合起來的。
- OPTIONAL Install
apt addons
- OPTIONAL Install
- OPTIONAL Install
cache components
- OPTIONAL Install
before_install
install
before_script
script
- OPTIONAL
before_cache(for cleaning up cache)
- OPTIONAL
-
after_successorafter_failure
-
- OPTIONAL
before_deploy
- OPTIONAL
- OPTIONAL
deploy
- OPTIONAL
- OPTIONAL
after_deploy
- OPTIONAL
after_script
我的項目中用到如下步驟:具體 yml 中用到指令查找 https://docs.travis-ci.com/user/job-lifecycle/
- 1、在github項目中加入
.travis.yml文件。 - 2、在文件中指定
language。 - 3、指定
golang的編譯版本。 - 4、編寫
before_install指令。 - 5、編寫 script
- 6、編寫 deploy
最后編寫出來的 .travis.yml 文件如下:
language: go
go:
- 1.12.x
before_install:
- mv ../$(basename $(pwd)) $GOPATH/src
- go get -v github.com/json-iterator/go
install: true
script:
- make test
- make build
- make docker-read
before_deploy:
- tar -zcf docker_build.tar.gz docker_build
deploy:
provider: releases
api_key: $GITHUB_TOKEN
file_glob: true
file:
- bin/*
- docker_build.tar.gz
skip_cleanup: true
on:
tags: true
注意:deploy 中的 $GITHUB_TOKEN 是在 Travis CI 平臺中設(shè)置的環(huán)境變量。
當github 有 commit 的時候會觸發(fā) deploay 之前的命令,如果在 github 中進行 release 操作就會 觸發(fā) deploy 操作,并且會把編譯好的 binary 文件上傳到 github release 中,下面這張圖中的文件就是 travis 上傳過來的。
