Skip to content

Commit

Permalink
Introduce suspenders:ci generator
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stevepolitodesign committed Mar 22, 2024
1 parent 52e99e9 commit 002dc86
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Unreleased
* Introduce `suspenders:email` generator
* Introduce `suspenders:testing` generator
* Introduce `suspenders:prerequisites` generator
* Introduce `suspenders:ci` generator

20230113.0 (January, 13, 2023)

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ Configures prerequisites. Currently Node.
bin/rails g suspenders:prerequisites
```

### CI

CI

```
bin/rails g suspenders:ci
```

## Contributing

See the [CONTRIBUTING] document.
Expand Down
12 changes: 12 additions & 0 deletions lib/generators/suspenders/ci_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Suspenders
module Generators
class CiGenerator < Rails::Generators::Base
source_root File.expand_path("../../templates/ci", __FILE__)

def cifiles
empty_directory ".github/workflows"
template "ci.yml", ".github/workflows/ci.yml"
end
end
end
end
146 changes: 146 additions & 0 deletions lib/generators/templates/ci/ci.yml.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
name: CI

on:
pull_request:
push:
branches: [main]

jobs:
<%- if Bundler.rubygems.find_name("bundler-audit").any? -%>
scan_ruby:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: .ruby-version
bundler-cache: true

- name: Scan for security vulnerabilities in Ruby dependencies
run: |
bin/rails bundle:audit:update
bin/rails bundle:audit
<% end -%>

<%- if File.exist?("bin/importmap") && File.exist?("package.json") -%>
scan_js:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: .ruby-version
bundler-cache: true

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version-file: .node-version

- name: Install modules
run: yarn install

- name: Scan for security vulnerabilities in JavaScript dependencies
run: |
bin/importmap audit
yarn audit
<% end -%>

<%- if Bundler.rubygems.find_name("standard").any? && File.exist?(".prettierrc") -%>
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: .ruby-version
bundler-cache: true

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version-file: .node-version

- name: Install modules
run: yarn install

- name: Lint Ruby code for consistent style
run: bin/rails standard

- name: Lint front-end code for consistent style
run: yarn lint
<% end -%>

test:
runs-on: ubuntu-latest

services:
postgres:
image: postgres
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
options: --health-cmd="pg_isready" --health-interval=10s --health-timeout=5s --health-retries=3

# redis:
# image: redis
# ports:
# - 6379:6379
# options: --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5

steps:
- name: Install packages
run: sudo apt-get update && sudo apt-get install --no-install-recommends -y google-chrome-stable curl libjemalloc2 libvips postgresql-client libpq-dev

- name: Checkout code
uses: actions/checkout@v4

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: .ruby-version
bundler-cache: true

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version-file: .node-version

- name: Install modules
run: yarn install

- name: Run tests
env:
RAILS_ENV: test
DATABASE_URL: postgres://postgres:postgres@localhost:5432
# REDIS_URL: redis://localhost:6379/0
<%- if File.exist? "spec" -%>
run: bin/rails db:setup spec
<%- else -%>
run: bin/rails db:setup test test:system
<%- end -%>

- name: Keep screenshots from failed system tests
uses: actions/upload-artifact@v4
if: failure()
with:
name: screenshots
<%- if File.exist? "spec" -%>
path: ${{ github.workspace }}/tmp/capybara
<%- else -%>
path: ${{ github.workspace }}/tmp/screenshots
<%- end -%>
if-no-files-found: ignore
26 changes: 26 additions & 0 deletions test/generators/suspenders/ci_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "test_helper"
require "generators/suspenders/ci_generator"

module Suspenders
module Generators
class CiGeneratorTest < Rails::Generators::TestCase
include Suspenders::TestHelpers

tests Suspenders::Generators::CiGenerator
destination Rails.root
teardown :restore_destination

test "generates CI template" do
run_generator

assert_file app_root(".github/workflows/ci.yml")
end

private

def restore_destination
remove_dir_if_exists ".github"
end
end
end
end

0 comments on commit 002dc86

Please sign in to comment.