冒险村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


<<:  Day 03 Python 入门

>>:  资安认知-电子邮件钓鱼

Day 18 ATT&CK for ICS - Privilege Escalation

Privilege Escalation 攻击者尝试在工控环境内取得更高的权限。 攻击者进入工控环境...

【左京淳的JAVA学习笔记】第五章 class定义与物件生成

如果把程序当成是魔法,前面几章都是基本的咒文。 到这章开始需要用到想像力了。 class(类) cl...

Day 17 - SVG 使用

唷呼~各位看官们今天最後一天上班日,明天就要放中秋连假了,欢呼吧,各位!下班後要搭乘大众运输返乡的...

第39天-学习 od 指令

进度 : 鸟哥的 Linux 私房菜 -- 第六章、Linux 档案与目录管理 快速查询对应 ASC...

Unity自主学习(二):如何安装Unity(一)

昨天我们简单了解了Unity是什麽东西,而为了接下来的学习,便先从下载安装Unity引擎开始。 我们...