How do I configure Travis-CI to use the correct time zone for a rails app?

喜你入骨 提交于 2019-12-21 07:14:04

问题


In my application.rb, I have

config.time_zone = "Pacific Time (US & Canada)"

And this works correctly in development/test, and production servers. However when I push to Travis-CI, it appears to be localized to UTC for example the output of I18n.l Time.now.

Is there something different about the Travis-CI ruby/rails environment?


回答1:


The way I accomplish setting the timezone is in the before_script section of the travis.yml

They give you root access to the VM running your project, so you can simply set the OS timezone that ruby uses:

    before_script:
      - echo 'Canada/Pacific' | sudo tee /etc/timezone
      - sudo dpkg-reconfigure --frontend noninteractive tzdata

If you wish, you can also force an update by adding this below:

      - sudo ntpdate ntp.ubuntu.com



回答2:


This worked for me:

before_install:
- export TZ=Australia/Canberra

to check that is correct you can output the date with this:

- date



回答3:


I had a similar problem and solved it.

On development my time is +10 GMT.

On Travis my time is +11 GMT.

before_script:
  - export TZ=Australia/Melbourne

I used Time Cop gem to test time depended code. Here is how i changed what the time is set too on Travis.

Here is an example test:

require 'spec_helper'
context "during the week after 11:00am for Morning Follow Up Call" do
  before do
    valid_prospect_params["treatment_enquiry_form"]["contact_time"] = "10:00 am"
    freeze_time = Time.local(2015, 11, 18, 11, 01, 0)
    change_time(freeze_time)
    post :create_prospect, valid_prospect_params
    reset_time
  end

  it "creates follow up activities" do
    expect(prospect_followup_activities).to eq(3)
  end

  it "creates follow_up_call for the next day in the morning " do
    expect(first_call.scheduled_for.to_s).to  eq("2015-11-19 10:00:00 +1100")
  end
end

Here is is how i changed the time on Travis. I have 2 methods.

def change_time(freeze_time)
  if Time.now.strftime("%Z") == "AEDT"
    Timecop.freeze(freeze_time + 1.hours)
  else
    Timecop.freeze(freeze_time)
  end
end

def reset_time
  Timecop.return
end


来源:https://stackoverflow.com/questions/23371542/how-do-i-configure-travis-ci-to-use-the-correct-time-zone-for-a-rails-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!