Skip to content

Commit de7749c

Browse files
committed
ci(workflow): 添加 RuboCop 检查并优化 CI 流程
添加 RuboCop 及相关 gem 用于代码风格检查 在测试流程前添加 lint 步骤 优化测试步骤的分组和日志输出 添加 CI 健康检查任务
1 parent c9bbc1d commit de7749c

5 files changed

Lines changed: 279 additions & 37 deletions

File tree

.github/workflows/ruby.yml

Lines changed: 70 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,82 @@ name: Ruby
22
on: [push, pull_request]
33
permissions:
44
contents: read
5+
56
jobs:
7+
lint:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v4.1.0
11+
12+
- name: Set up Ruby
13+
uses: ruby/setup-ruby@v1
14+
with:
15+
ruby-version: 3.4
16+
bundler-cache: true
17+
18+
- name: Run RuboCop
19+
run: |
20+
echo "::group::RuboCop"
21+
bundle exec rubocop --format progress --format clang
22+
echo "::endgroup::"
23+
624
test:
725
runs-on: ubuntu-latest
26+
needs: lint
827
steps:
9-
- uses: actions/checkout@v4.1.0
10-
- name: Set up Ruby
11-
uses: ruby/setup-ruby@v1
12-
with:
13-
ruby-version: 3.4
14-
bundler-cache: true
15-
- name: Install dependencies
16-
run: |
17-
gem install rails
18-
bundle install
19-
- name: Check Ruby Syntax in .rb files
20-
run: find ./app/ | grep ".*\.rb$" | xargs -L 1 ruby -c
21-
- name: Setup database
22-
run: rails db:migrate RAILS_ENV=test
23-
- name: Run tests
24-
run: bundle exec rake
28+
- uses: actions/checkout@v4.1.0
29+
30+
- name: Set up Ruby
31+
uses: ruby/setup-ruby@v1
32+
with:
33+
ruby-version: 3.4
34+
bundler-cache: true
35+
36+
- name: Database migrations
37+
run: |
38+
echo "::group::Database Migrations"
39+
bundle exec rails db:migrate RAILS_ENV=test
40+
echo "::endgroup::"
41+
42+
- name: CI health check
43+
run: |
44+
echo "::group::CI Health Check"
45+
bundle exec rails ci:health RAILS_ENV=test
46+
echo "::endgroup::"
47+
48+
- name: Run tests
49+
run: |
50+
echo "::group::Running Tests"
51+
bundle exec rails test RAILS_ENV=test --verbose
52+
echo "::endgroup::"
2553
2654
smoke-test:
2755
runs-on: ubuntu-latest
2856
needs: test
2957
steps:
30-
- uses: actions/checkout@v4.1.0
31-
- name: Set up Ruby
32-
uses: ruby/setup-ruby@v1
33-
with:
34-
ruby-version: 3.4
35-
bundler-cache: true
36-
- name: Install dependencies
37-
run: |
38-
gem install rails
39-
bundle install
40-
- name: Make scripts executable
41-
run: |
42-
chmod +x start-app.sh
43-
chmod +x stop-app.sh
44-
chmod +x status.sh
45-
chmod +x smoke-ci.sh
46-
- name: Run smoke CI test
47-
run: ./smoke-ci.sh
58+
- uses: actions/checkout@v4.1.0
59+
60+
- name: Set up Ruby
61+
uses: ruby/setup-ruby@v1
62+
with:
63+
ruby-version: 3.4
64+
bundler-cache: true
65+
66+
- name: Database migrations
67+
run: |
68+
echo "::group::Database Migrations"
69+
bundle exec rails db:migrate RAILS_ENV=test
70+
echo "::endgroup::"
71+
72+
- name: Make scripts executable
73+
run: |
74+
chmod +x start-app.sh
75+
chmod +x stop-app.sh
76+
chmod +x status.sh
77+
chmod +x smoke-ci.sh
78+
79+
- name: Run smoke CI test
80+
run: |
81+
echo "::group::Smoke Test"
82+
./smoke-ci.sh
83+
echo "::endgroup::"

.rubocop.yml

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
require:
2+
- rubocop-rails
3+
- rubocop-performance
4+
5+
AllCops:
6+
TargetRubyVersion: 3.1
7+
NewCops: enable
8+
Exclude:
9+
- 'vendor/**/*'
10+
- 'db/schema.rb'
11+
- 'db/migrate/*.rb'
12+
- 'node_modules/**/*'
13+
- 'tmp/**/*'
14+
- 'log/**/*'
15+
16+
Layout/LineLength:
17+
Max: 120
18+
19+
Style/Documentation:
20+
Enabled: false
21+
22+
Style/FrozenStringLiteralComment:
23+
Enabled: false
24+
25+
Style/MethodCallWithoutArgsParentheses:
26+
Enabled: false
27+
28+
Style/BlockDelimiters:
29+
Enabled: false
30+
31+
Metrics/MethodLength:
32+
Max: 30
33+
34+
Metrics/ClassLength:
35+
Max: 150
36+
37+
Metrics/AbcSize:
38+
Max: 20
39+
40+
Metrics/CyclomaticComplexity:
41+
Max: 10
42+
43+
Rails:
44+
Enabled: true
45+
46+
Rails/HasManyOrHasOneDependent:
47+
Enabled: true
48+
49+
Rails/InverseOf:
50+
Enabled: true
51+
52+
Rails/UnknownEnv:
53+
Enabled: true
54+
55+
Rails/RedundantReceiverInWithOptions:
56+
Enabled: false
57+
58+
Performance:
59+
Enabled: true
60+
61+
Performance/Casecmp:
62+
Enabled: true
63+
64+
Performance/RedundantMatch:
65+
Enabled: true
66+
67+
Performance/ReverseEach:
68+
Enabled: true
69+
70+
Naming/PredicateName:
71+
Enabled: true
72+
ForbiddenPrefixes:
73+
- is_
74+
- are_
75+
AllowedMethods:
76+
- is_a?
77+
- kind_of?
78+
- instance_of?
79+
80+
Naming/FileName:
81+
Exclude:
82+
- 'config/initializers/*'
83+
84+
Style/HashEachMethods:
85+
Enabled: true
86+
87+
Style/HashTransformKeys:
88+
Enabled: true
89+
90+
Style/HashTransformValues:
91+
Enabled: true
92+
93+
Style/ExplicitBlockArgument:
94+
Enabled: true
95+
96+
Style/CollectionCompact:
97+
Enabled: true
98+
99+
Style/DocumentDynamicEvalDefinition:
100+
Enabled: true
101+
102+
Style/NilLambda:
103+
Enabled: true
104+
105+
Style/RedundantArgument:
106+
Enabled: true
107+
108+
Style/StringChars:
109+
Enabled: true
110+
111+
Style/SwapValue:
112+
Enabled: true
113+
114+
Lint/RaiseException:
115+
Enabled: true
116+
117+
Lint/StructNewOverride:
118+
Enabled: true
119+
120+
Lint/NumberConversion:
121+
Enabled: true

Gemfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ gem 'rack-cors'
3636
gem 'lockbox', '=1.3.3'
3737

3838
group :development, :test do
39-
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
4039
gem 'byebug'
40+
gem 'rubocop', require: false
41+
gem 'rubocop-rails', require: false
42+
gem 'rubocop-performance', require: false
4143
end
4244

4345
group :development do

lib/tasks/ci.rake

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
namespace :ci do
2+
desc 'CI health check - verify application loads correctly'
3+
task health: :environment do
4+
puts '=' * 60
5+
puts 'CI Health Check'
6+
puts '=' * 60
7+
puts
8+
9+
checks = []
10+
11+
checks << {
12+
name: 'Database Connection',
13+
check: -> {
14+
ActiveRecord::Base.connection.execute('SELECT 1')
15+
true
16+
}
17+
}
18+
19+
checks << {
20+
name: 'User Model',
21+
check: -> {
22+
User.column_names.include?('email') && User.column_names.include?('name')
23+
}
24+
}
25+
26+
checks << {
27+
name: 'Document Model',
28+
check: -> {
29+
Document.column_names.include?('title') && Document.column_names.include?('user_id')
30+
}
31+
}
32+
33+
checks << {
34+
name: 'Routes Loaded',
35+
check: -> {
36+
Rails.application.routes.routes.any?
37+
}
38+
}
39+
40+
checks << {
41+
name: 'JsonWebToken Available',
42+
check: -> {
43+
defined?(JsonWebToken) && JsonWebToken.respond_to?(:encode)
44+
}
45+
}
46+
47+
checks << {
48+
name: 'Lockbox Available',
49+
check: -> {
50+
defined?(Lockbox)
51+
}
52+
}
53+
54+
all_passed = true
55+
56+
checks.each do |check|
57+
begin
58+
passed = check[:check].call
59+
if passed
60+
puts " ✓ #{check[:name]}"
61+
else
62+
puts " ✗ #{check[:name]} - FAILED"
63+
all_passed = false
64+
end
65+
rescue => e
66+
puts " ✗ #{check[:name]} - ERROR: #{e.message}"
67+
all_passed = false
68+
end
69+
end
70+
71+
puts
72+
puts '=' * 60
73+
74+
if all_passed
75+
puts 'Health Check: PASSED ✓'
76+
puts '=' * 60
77+
exit 0
78+
else
79+
puts 'Health Check: FAILED ✗'
80+
puts '=' * 60
81+
exit 1
82+
end
83+
end
84+
end

test/test_helper.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
require 'rails/test_help'
44

55
class ActiveSupport::TestCase
6-
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
76
fixtures :all
87

9-
# Add more helper methods to be used by all tests here...
8+
parallelize(workers: :number_of_processors)
109
end

0 commit comments

Comments
 (0)