教程-如何用travis-ci實(shí)現(xiàn)自動(dòng)化部署前端項(xiàng)目

背景:UI組件庫(kù)做好了,但是要將UI文檔自動(dòng)發(fā)布,之前都是每次提交代碼的時(shí)候去手動(dòng)構(gòu)建后再發(fā)布文檔,但是這樣太麻煩,所以想用travis-ci自動(dòng)化構(gòu)建一個(gè)VUE UI框架的官方文檔。組件庫(kù)源碼地址
https://github.com/ninecat-ui/ninecat-ui

實(shí)現(xiàn)的方法

  • 在項(xiàng)目里面建一個(gè)docs文件夾,然后commit后再構(gòu)建文檔,然后push到原代碼倉(cāng)庫(kù),通過(guò)gh-page關(guān)聯(lián)docs文件實(shí)現(xiàn)官方文檔的預(yù)覽。

  • 官方文檔為另外的項(xiàng)目,以 ninecat-ui.github.io這種主頁(yè)名命名項(xiàng)目,然后commit后構(gòu)建文檔,再clone項(xiàng)目=》commit項(xiàng)目=》提交項(xiàng)目。

兩種方法原理都可行,但是第一種方式會(huì)發(fā)現(xiàn)在travis-ci第二次提交的時(shí)候,git的記錄是沒(méi)有的,所以放棄了第一種,然后用第二種方法實(shí)現(xiàn)的,第二種方法有個(gè)好處就是不用擔(dān)心你的操作會(huì)影響到原項(xiàng)目,因?yàn)闆](méi)有push操作,所以下面就講解一下第二種方法實(shí)現(xiàn)的步驟。

實(shí)現(xiàn)步驟

1.Github 賬號(hào)關(guān)聯(lián) TravisCI

用 Github 賬號(hào)登錄https://travis-ci.com/,你的所有 Repo 都會(huì)列出來(lái),選擇激活你想要 build 的 Repo,這一步基本都是傻瓜操作,很簡(jiǎn)單,無(wú)需贅述。

2.配置環(huán)境變量

環(huán)境變量有兩種,一種是配置在.travis.yml文件里面的,

.travis.yml文件

另一種是配置在travis-ci的后臺(tái)系統(tǒng)中進(jìn)行,在setting里的Environment Variables里面。


travis-ci后臺(tái)配置

第二種方式的環(huán)境變量主要是用于第三方系統(tǒng)的權(quán)限校驗(yàn),當(dāng)然再本次實(shí)踐中也需要進(jìn)行配置,配置一個(gè)github的token以便后面的提交操作。

按照下面的步驟配置GitHub的token
(1)進(jìn)入GitHub的個(gè)人設(shè)置,Settings=> Developer settings=>Personal access tokens=>Generate new token
進(jìn)行如下勾選就行

Generate new token

(2)進(jìn)入travis-ci的后臺(tái)系統(tǒng)中配置環(huán)境變量Environment Variables如travis-ci后臺(tái)配置圖所示,新增一個(gè)環(huán)境變量,然后將GitHub建立的token填入其中。DISPLAY VALUE IN BUILD LOG 選項(xiàng)不要勾選,否則環(huán)境變量會(huì)在你執(zhí)行build腳本的時(shí)候顯示出來(lái)。

3.配置腳本構(gòu)建


# Designated language. https://docs.travis-ci.com/user/languages/javascript-with-nodejs/
language: node_js

# Environment variables
env:
  global:
    - GitHub_REF: github.com/ninecat-ui/ninecat-ui.github.io.git

# Install dependence
install:
  - npm install
  - npm install -g codecov

# Cache the node_modules folder and don't need to download and install all npm packages every time.
cache:
  directories:
    - node_modules
# Specify the node version.
node_js:
  - "11.1.0"
# Perform builds only on the specified branch.  https://docs.travis-ci.com/user/customizing-the-build/#building-specific-branches
branches:
  only:
    - master
# The script to execute.
script:
  - npm test
  - codecov
  - npm run docs:build
  - cd docs
  - git init
  - git add -A
  - git commit -m "docs:update docs"
  - git push --force --quiet "https://${GitHub_TOKEN}@${GitHub_REF}" master:master
# Configure to send notifications when the build fails. https://docs.travis-ci.com/user/notifications
notifications:
  webhooks:
    urls:
      - https://www.travisbuddy.com/
    on_success: never # Successful build does not send mail.
    on_failure: always # Build failure always sends a message.

上面的腳本可以根據(jù)官方文檔https://docs.travis-ci.com/user/speeding-up-the-build/自行定義和使用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容