Skip to content

Commit

Permalink
feat: autoconfigure KNAPSACK_PRO_FIXED_QUEUE_SPLIT
Browse files Browse the repository at this point in the history
  • Loading branch information
3v0k4 committed Jun 7, 2023
1 parent f7756e8 commit 40076ee
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 42 deletions.
14 changes: 9 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

### Unreleased

* Set `RAILS_ENV=test` / `RACK_ENV=test` in Queue Mode
* __(breaking change)__ Use `KNAPSACK_PRO_FIXED_QUEUE_SPLIT=true` as default value in Queue Mode and use `false` for proper CI providers

https://github.com/KnapsackPro/knapsack_pro-ruby/pull/199

### Unreleased
https://github.com/KnapsackPro/knapsack_pro-ruby/pull/198

* Detect CI from environment and get the correct ENVs instead of trying all of them and risk conflicts

https://github.com/KnapsackPro/knapsack_pro-ruby/pull/201

* Set `RAILS_ENV=test` / `RACK_ENV=test` in Queue Mode

https://github.com/KnapsackPro/knapsack_pro-ruby/pull/199

https://github.com/KnapsackPro/knapsack_pro-ruby/compare/v4.1.0...v5.0.0

### 4.1.0

* Add support for CI node retry count on GitHub Actions
Expand All @@ -24,7 +28,7 @@ https://github.com/KnapsackPro/knapsack_pro-ruby/compare/v4.0.0...v4.1.0

### 4.0.0

* Raise when `KNAPSACK_PRO_CI_NODE_BUILD_ID` is missing
* __(breaking change)__ Raise when `KNAPSACK_PRO_CI_NODE_BUILD_ID` is missing

https://github.com/KnapsackPro/knapsack_pro-ruby/pull/195

Expand Down
4 changes: 4 additions & 0 deletions lib/knapsack_pro/config/ci/app_veyor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def project_dir
def detected
ENV.key?('APPVEYOR') ? self.class : nil
end

def fixed_queue_split
false
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/knapsack_pro/config/ci/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def user_seat

def detected
end

def fixed_queue_split
true
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/knapsack_pro/config/ci/buildkite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def user_seat
def detected
ENV.key?('BUILDKITE') ? self.class : nil
end

def fixed_queue_split
true
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/knapsack_pro/config/ci/circle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def user_seat
def detected
ENV.key?('CIRCLECI') ? self.class : nil
end

def fixed_queue_split
false
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/knapsack_pro/config/ci/cirrus_ci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def project_dir
def detected
ENV.key?('CIRRUS_CI') ? self.class : nil
end

def fixed_queue_split
false
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/knapsack_pro/config/ci/codefresh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def project_dir
def detected
ENV.key?('CF_BUILD_ID') ? self.class : nil
end

def fixed_queue_split
false
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/knapsack_pro/config/ci/codeship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def project_dir
def detected
ENV['CI_NAME'] == 'codeship' ? self.class : nil
end

def fixed_queue_split
true
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/knapsack_pro/config/ci/github_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def user_seat
def detected
ENV.key?('GITHUB_ACTIONS') ? self.class : nil
end

def fixed_queue_split
true
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/knapsack_pro/config/ci/gitlab_ci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def user_seat
def detected
ENV.key?('GITLAB_CI') ? self.class : nil
end

def fixed_queue_split
false
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/knapsack_pro/config/ci/heroku.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def project_dir
def detected
ENV.key?('HEROKU_TEST_RUN_ID') ? self.class : nil
end

def fixed_queue_split
false
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/knapsack_pro/config/ci/semaphore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def project_dir
def detected
ENV.key?('SEMAPHORE_BUILD_NUMBER') ? self.class : nil
end

def fixed_queue_split
false
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/knapsack_pro/config/ci/semaphore2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def detected
# check 2 keys to be sure we are using Semaphore 2.0
ENV.key?('SEMAPHORE') && ENV.key?('SEMAPHORE_WORKFLOW_ID') ? self.class : nil
end

def fixed_queue_split
false
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/knapsack_pro/config/ci/travis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def project_dir
def detected
ENV.key?('TRAVIS') ? self.class : nil
end

def fixed_queue_split
true
end
end
end
end
Expand Down
11 changes: 10 additions & 1 deletion lib/knapsack_pro/config/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,16 @@ def fixed_test_suite_split?
end

def fixed_queue_split
ENV.fetch('KNAPSACK_PRO_FIXED_QUEUE_SPLIT', false)
@fixed_queue_split ||= begin
env_name = 'KNAPSACK_PRO_FIXED_QUEUE_SPLIT'
computed = ENV.fetch(env_name, ci_env_for(:fixed_queue_split)).to_s

if !ENV.key?(env_name)
KnapsackPro.logger.info("#{env_name} is not set. Using default value: #{computed}. Learn more at #{KnapsackPro::Urls::FIXED_QUEUE_SPLIT}")
end

computed
end
end

def fixed_queue_split?
Expand Down
114 changes: 78 additions & 36 deletions spec/knapsack_pro/config/env_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -632,20 +632,6 @@
end
end

describe '.fixed_test_suite_split' do
subject { described_class.fixed_test_suite_split }

context 'when ENV exists' do
before { stub_const("ENV", { 'KNAPSACK_PRO_FIXED_TEST_SUITE_SPLIT' => false }) }
it { should eq false }
end

context "when ENV doesn't exist" do
before { stub_const("ENV", {}) }
it { should be true }
end
end

describe '.fixed_test_suite_split?' do
subject { described_class.fixed_test_suite_split? }

Expand All @@ -667,38 +653,94 @@
end
end

describe '.fixed_queue_split' do
subject { described_class.fixed_queue_split }

context 'when ENV exists' do
before { stub_const("ENV", { 'KNAPSACK_PRO_FIXED_QUEUE_SPLIT' => true }) }
it { should eq true }
end

context "when ENV doesn't exist" do
before { stub_const("ENV", {}) }
it { should be false }
end
end

describe '.fixed_queue_split?' do
subject { described_class.fixed_queue_split? }
after(:each) { described_class.remove_instance_variable(:@fixed_queue_split) }

context 'when ENV exists' do
context 'when KNAPSACK_PRO_FIXED_QUEUE_SPLIT=true' do
before { stub_const("ENV", { 'KNAPSACK_PRO_FIXED_QUEUE_SPLIT' => 'true' }) }
it { should be true }
context 'when KNAPSACK_PRO_FIXED_QUEUE_SPLIT=false' do
[
['AppVeyor', { 'APPVEYOR' => '123' }],
['Buildkite', { 'BUILDKITE' => 'true' }],
['CircleCI', { 'CIRCLECI' => 'true' }],
['Cirrus CI', { 'CIRRUS_CI' => 'true' }],
['Codefresh', { 'CF_BUILD_ID' => '123' }],
['Codeship', { 'CI_NAME' => 'codeship' }],
['GitHub Actions', { 'GITHUB_ACTIONS' => 'true' }],
['GitLab CI', { 'GITLAB_CI' => 'true' }],
['Heroku CI', { 'HEROKU_TEST_RUN_ID' => '123' }],
['Semaphore CI 1.0', { 'SEMAPHORE_BUILD_NUMBER' => '123' }],
['Semaphore CI 2.0', { 'SEMAPHORE' => 'true', 'SEMAPHORE_WORKFLOW_ID' => '123' }],
['Travis CI', { 'TRAVIS' => 'true' }],
['Unsupported', {}],
].each do |ci, env|
it "on #{ci} it is false" do
logger = instance_double(Logger)
allow(KnapsackPro).to receive(:logger).and_return(logger)
expect(logger).not_to receive(:info)

stub_const("ENV", env.merge({ 'KNAPSACK_PRO_FIXED_QUEUE_SPLIT' => 'false' }))

expect(subject).to eq(false)
end
end
end

context 'when KNAPSACK_PRO_FIXED_QUEUE_SPLIT=false' do
before { stub_const("ENV", { 'KNAPSACK_PRO_FIXED_QUEUE_SPLIT' => 'false' }) }
it { should be false }
context 'when KNAPSACK_PRO_FIXED_QUEUE_SPLIT=true' do
[
['AppVeyor', { 'APPVEYOR' => '123' }],
['Buildkite', { 'BUILDKITE' => 'true' }],
['CircleCI', { 'CIRCLECI' => 'true' }],
['Cirrus CI', { 'CIRRUS_CI' => 'true' }],
['Codefresh', { 'CF_BUILD_ID' => '123' }],
['Codeship', { 'CI_NAME' => 'codeship' }],
['GitHub Actions', { 'GITHUB_ACTIONS' => 'true' }],
['GitLab CI', { 'GITLAB_CI' => 'true' }],
['Heroku CI', { 'HEROKU_TEST_RUN_ID' => '123' }],
['Semaphore CI 1.0', { 'SEMAPHORE_BUILD_NUMBER' => '123' }],
['Semaphore CI 2.0', { 'SEMAPHORE' => 'true', 'SEMAPHORE_WORKFLOW_ID' => '123' }],
['Travis CI', { 'TRAVIS' => 'true' }],
['Unsupported', {}],
].each do |ci, env|
it "on #{ci} it is true" do
logger = instance_double(Logger)
allow(KnapsackPro).to receive(:logger).and_return(logger)
expect(logger).not_to receive(:info)

stub_const("ENV", env.merge({ 'KNAPSACK_PRO_FIXED_QUEUE_SPLIT' => 'true' }))

expect(subject).to eq(true)
end
end
end
end

context "when ENV doesn't exist" do
before { stub_const("ENV", {}) }
it { should be false }
[
['AppVeyor', { 'APPVEYOR' => '123' }, false],
['Buildkite', { 'BUILDKITE' => 'true' }, true],
['CircleCI', { 'CIRCLECI' => 'true' }, false],
['Cirrus CI', { 'CIRRUS_CI' => 'true' }, false],
['Codefresh', { 'CF_BUILD_ID' => '123' }, false],
['Codeship', { 'CI_NAME' => 'codeship' }, true],
['GitHub Actions', { 'GITHUB_ACTIONS' => 'true' }, true],
['GitLab CI', { 'GITLAB_CI' => 'true' }, false],
['Heroku CI', { 'HEROKU_TEST_RUN_ID' => '123' }, false],
['Semaphore CI 1.0', { 'SEMAPHORE_BUILD_NUMBER' => '123' }, false],
['Semaphore CI 2.0', { 'SEMAPHORE' => 'true', 'SEMAPHORE_WORKFLOW_ID' => '123' }, false],
['Travis CI', { 'TRAVIS' => 'true' }, true],
['Unsupported', {}, true],
].each do |ci, env, expected|
it "on #{ci} it is #{expected}" do
logger = instance_double(Logger)
expect(KnapsackPro).to receive(:logger).and_return(logger)
expect(logger).to receive(:info).with("KNAPSACK_PRO_FIXED_QUEUE_SPLIT is not set. Using default value: #{expected}. Learn more at #{KnapsackPro::Urls::FIXED_QUEUE_SPLIT}")

stub_const("ENV", env)

expect(subject).to eq(expected)
end
end
end
end

Expand Down

0 comments on commit 40076ee

Please sign in to comment.