iT邦幫忙

2021 iThome 鐵人賽

DAY 3
0
自我挑戰組

冒險村-30 Day Ruby on Rails Tips Challenge系列 第 3

冒險村03 - Travis CI cookbook

03 - Travis CI cookbook

既然都處理好 linter 來檢測我們的專案是否有符合規範,來把 Rails 放到 Travis CI 給它跑起來吧!

首先到 Travis CI 首頁,透過 Github 或者其他方式登入(注意如果進入的是首頁是 https://travis-ci.org/ 可以點選上方黃色的 link 連到 .com 的網址,以前有分企業、免費版)

登入完成後,點選 my account > repositories > activate 並選擇要加入的 Repo 然後 「Approve and install」加入,這樣子 Repo 有更新 code 推上去 CI 就會 trigger 到。

註: 如下述設定檔 .travis.yml 已經完成,還是沒有觸發 CI 的話,須先到 https://travis-ci.com/account/plan/usage 設定 Plan usage,需要點選其中一種的 Plan 後,才會正常的執行。(第一次用應該都會碰到)

  Plan information

  FreePlan Activation date: August 17, 2021
  Plan Add-ons list:
    - Private: Free 10 000 credits
    - User license: Unlimited users

Setting .travis.yml

建立設定檔案於專案根目錄下,告訴 travis ci 我們要使用的環境設定。

舉例來說:

  # .travis.yml
  language: ruby

  # 專案 ruby 版本
  rvm:
    - 3.0.0

  # 專案資料庫
  services:
    - postgresql

  install:
    - bundle install

  script:
    - RAILS_ENV=test bundle exec rake db:migrate:reset
    # Rspec + 前兩篇的 linter
    - bundle exec rspec
    - bundle exec rubocop
    - bundle exec rails_best_practices

再推一次 code 上 Repo 就會在 dashboard 看到 Build 了!如果成功跑過會顯示綠色 #branch passed,有問題則會是紅色 #branch failed

註: 有可能會發生 local 驗證正確,但在 CI 上跑沒過,這時候除了可以 Restart build 外(有問題重開機的概念(誤)),也可以點選 Debug build 後給的 ssh link,再透過 local ssh 進去測試。

Setting ENV

專案時常會用到 settings 的 ENV,像是 api 的 key 或其他的 token,可以想像在 local 的時候需要另外設定,而推上 CI 上如果測試中也會使用到,則需要到 More options > Settins > Enviroment Variables 中設定

Setting time_zone config

如專案有改變 Rails timezone

  # application.rb
  config.time_zone = "Tokyo"

並在測試項目中也有寫關於時間的測試於 CI 跑,也記得要將 CI 的 config 修改

  # .travis.yml
  language: ruby

  rvm:
    - 3.0.0

  services:
    - postgresql

  install:
    - bundle install

  script:
    - RAILS_ENV=test bundle exec rake db:migrate:reset
    - bundle exec rspec
    - bundle exec rubocop
    - bundle exec rails_best_practices

  before_install:
    # setting config
    - export TZ=Asia/Tokyo
    # to check that is correct you can output the date
    - date

更新: rvm 3.0.0 version 以上在 install 會出現問題,目前還未修正,詳細可以參考我在 rvm repo 開的 issue Could not download rvm-installer #5133

參考來源

My blog


上一篇
冒險村02 - Begin from linter(2)
下一篇
冒險村04 - Create PR with default template
系列文
冒險村-30 Day Ruby on Rails Tips Challenge30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言