Skip to content

Commit 002dc86

Browse files
Introduce suspenders:ci generator
Creates CI template to be run via [GitHub Actions][ga] based on a [similar template][ci template] that will be generated in an upcoming Release of Rails. Because this generator can be run in an existing application, we add conditional checks for some jobs. However, this generator is intended to be run as part of our holistic `suspenders:install:web` which will be introduced in #1152. Once Rails is released to contain a CI template, we will need to consider how we want to handle conflicts between its file and ours, but for now, we do not need to worry about that. [ga]: https://docs.github.com/en/actions [ci template]: rails/rails#50508 --- **To Do** - [ ] Consider returning early and raising if the application is not using PostgreSQL.
1 parent 52e99e9 commit 002dc86

File tree

5 files changed

+193
-0
lines changed

5 files changed

+193
-0
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Unreleased
1616
* Introduce `suspenders:email` generator
1717
* Introduce `suspenders:testing` generator
1818
* Introduce `suspenders:prerequisites` generator
19+
* Introduce `suspenders:ci` generator
1920

2021
20230113.0 (January, 13, 2023)
2122

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ Configures prerequisites. Currently Node.
188188
bin/rails g suspenders:prerequisites
189189
```
190190

191+
### CI
192+
193+
CI
194+
195+
```
196+
bin/rails g suspenders:ci
197+
```
198+
191199
## Contributing
192200

193201
See the [CONTRIBUTING] document.
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Suspenders
2+
module Generators
3+
class CiGenerator < Rails::Generators::Base
4+
source_root File.expand_path("../../templates/ci", __FILE__)
5+
6+
def cifiles
7+
empty_directory ".github/workflows"
8+
template "ci.yml", ".github/workflows/ci.yml"
9+
end
10+
end
11+
end
12+
end

lib/generators/templates/ci/ci.yml.tt

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [main]
7+
8+
jobs:
9+
<%- if Bundler.rubygems.find_name("bundler-audit").any? -%>
10+
scan_ruby:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
17+
- name: Set up Ruby
18+
uses: ruby/setup-ruby@v1
19+
with:
20+
ruby-version: .ruby-version
21+
bundler-cache: true
22+
23+
- name: Scan for security vulnerabilities in Ruby dependencies
24+
run: |
25+
bin/rails bundle:audit:update
26+
bin/rails bundle:audit
27+
<% end -%>
28+
29+
<%- if File.exist?("bin/importmap") && File.exist?("package.json") -%>
30+
scan_js:
31+
runs-on: ubuntu-latest
32+
33+
steps:
34+
- name: Checkout code
35+
uses: actions/checkout@v4
36+
37+
- name: Set up Ruby
38+
uses: ruby/setup-ruby@v1
39+
with:
40+
ruby-version: .ruby-version
41+
bundler-cache: true
42+
43+
- name: Setup Node
44+
uses: actions/setup-node@v4
45+
with:
46+
node-version-file: .node-version
47+
48+
- name: Install modules
49+
run: yarn install
50+
51+
- name: Scan for security vulnerabilities in JavaScript dependencies
52+
run: |
53+
bin/importmap audit
54+
yarn audit
55+
<% end -%>
56+
57+
<%- if Bundler.rubygems.find_name("standard").any? && File.exist?(".prettierrc") -%>
58+
lint:
59+
runs-on: ubuntu-latest
60+
steps:
61+
- name: Checkout code
62+
uses: actions/checkout@v4
63+
64+
- name: Set up Ruby
65+
uses: ruby/setup-ruby@v1
66+
with:
67+
ruby-version: .ruby-version
68+
bundler-cache: true
69+
70+
- name: Setup Node
71+
uses: actions/setup-node@v4
72+
with:
73+
node-version-file: .node-version
74+
75+
- name: Install modules
76+
run: yarn install
77+
78+
- name: Lint Ruby code for consistent style
79+
run: bin/rails standard
80+
81+
- name: Lint front-end code for consistent style
82+
run: yarn lint
83+
<% end -%>
84+
85+
test:
86+
runs-on: ubuntu-latest
87+
88+
services:
89+
postgres:
90+
image: postgres
91+
env:
92+
POSTGRES_USER: postgres
93+
POSTGRES_PASSWORD: postgres
94+
ports:
95+
- 5432:5432
96+
options: --health-cmd="pg_isready" --health-interval=10s --health-timeout=5s --health-retries=3
97+
98+
# redis:
99+
# image: redis
100+
# ports:
101+
# - 6379:6379
102+
# options: --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5
103+
104+
steps:
105+
- name: Install packages
106+
run: sudo apt-get update && sudo apt-get install --no-install-recommends -y google-chrome-stable curl libjemalloc2 libvips postgresql-client libpq-dev
107+
108+
- name: Checkout code
109+
uses: actions/checkout@v4
110+
111+
- name: Set up Ruby
112+
uses: ruby/setup-ruby@v1
113+
with:
114+
ruby-version: .ruby-version
115+
bundler-cache: true
116+
117+
- name: Setup Node
118+
uses: actions/setup-node@v4
119+
with:
120+
node-version-file: .node-version
121+
122+
- name: Install modules
123+
run: yarn install
124+
125+
- name: Run tests
126+
env:
127+
RAILS_ENV: test
128+
DATABASE_URL: postgres://postgres:postgres@localhost:5432
129+
# REDIS_URL: redis://localhost:6379/0
130+
<%- if File.exist? "spec" -%>
131+
run: bin/rails db:setup spec
132+
<%- else -%>
133+
run: bin/rails db:setup test test:system
134+
<%- end -%>
135+
136+
- name: Keep screenshots from failed system tests
137+
uses: actions/upload-artifact@v4
138+
if: failure()
139+
with:
140+
name: screenshots
141+
<%- if File.exist? "spec" -%>
142+
path: ${{ github.workspace }}/tmp/capybara
143+
<%- else -%>
144+
path: ${{ github.workspace }}/tmp/screenshots
145+
<%- end -%>
146+
if-no-files-found: ignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require "test_helper"
2+
require "generators/suspenders/ci_generator"
3+
4+
module Suspenders
5+
module Generators
6+
class CiGeneratorTest < Rails::Generators::TestCase
7+
include Suspenders::TestHelpers
8+
9+
tests Suspenders::Generators::CiGenerator
10+
destination Rails.root
11+
teardown :restore_destination
12+
13+
test "generates CI template" do
14+
run_generator
15+
16+
assert_file app_root(".github/workflows/ci.yml")
17+
end
18+
19+
private
20+
21+
def restore_destination
22+
remove_dir_if_exists ".github"
23+
end
24+
end
25+
end
26+
end

0 commit comments

Comments
 (0)