-
-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
suspenders:ci
generator (#1172)
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. Also create a [Dependabot][dependabot] file based off the [the upcoming release][ci template]. Raises if the application is not using PostgreSQL, since our CI template assumes that adapter. 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 [dependabot]: https://docs.github.com/en/code-security/dependabot/working-with-dependabot
- Loading branch information
1 parent
52e99e9
commit 5f90074
Showing
9 changed files
with
304 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module Suspenders | ||
module Generators | ||
class CiGenerator < Rails::Generators::Base | ||
include Suspenders::Generators::DatabaseUnsupported | ||
include Suspenders::Generators::Helpers | ||
|
||
source_root File.expand_path("../../templates/ci", __FILE__) | ||
desc "Creates CI files for GitHub Actions" | ||
|
||
def ci_files | ||
empty_directory ".github/workflows" | ||
template "ci.yml", ".github/workflows/ci.yaml" | ||
template "dependabot.yml", ".github/dependabot.yaml" | ||
end | ||
|
||
private | ||
|
||
def scan_ruby? | ||
has_gem? "bundler-audit" | ||
end | ||
|
||
def scan_js? | ||
File.exist?("bin/importmap") && using_node? | ||
end | ||
|
||
def lint? | ||
using_node? && has_gem?("standard") && has_yarn_script?("lint") | ||
end | ||
|
||
def using_node? | ||
File.exist? "package.json" | ||
end | ||
|
||
def has_gem?(name) | ||
Bundler.rubygems.find_name(name).any? | ||
end | ||
|
||
def using_rspec? | ||
File.exist? "spec" | ||
end | ||
|
||
def has_yarn_script?(name) | ||
return false if !using_node? | ||
|
||
content = File.read("package.json") | ||
json = JSON.parse(content) | ||
|
||
json.dig("scripts", name) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
name: CI | ||
|
||
on: | ||
pull_request: | ||
push: | ||
branches: [main] | ||
|
||
jobs: | ||
<%- if scan_ruby? -%> | ||
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 scan_js? -%> | ||
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 lint? -%> | ||
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 | ||
|
||
<%- if using_node? -%> | ||
- name: Setup Node | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version-file: .node-version | ||
|
||
- name: Install modules | ||
run: yarn install | ||
<%- end -%> | ||
|
||
- name: Run tests | ||
env: | ||
RAILS_ENV: test | ||
DATABASE_URL: postgres://postgres:postgres@localhost:5432 | ||
# REDIS_URL: redis://localhost:6379/0 | ||
<%- if using_rspec? -%> | ||
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 using_rspec? -%> | ||
path: ${{ github.workspace }}/tmp/capybara | ||
<%- else -%> | ||
path: ${{ github.workspace }}/tmp/screenshots | ||
<%- end -%> | ||
if-no-files-found: ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
version: 2 | ||
updates: | ||
- package-ecosystem: bundler | ||
directory: "/" | ||
schedule: | ||
interval: daily | ||
open-pull-requests-limit: 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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 files" do | ||
with_database "postgresql" do | ||
run_generator | ||
|
||
assert_file app_root(".github/workflows/ci.yaml") | ||
assert_file app_root(".github/dependabot.yaml") | ||
end | ||
end | ||
|
||
test "raises if PostgreSQL is not the adapter" do | ||
with_database "unsupported" do | ||
assert_raises Suspenders::Generators::DatabaseUnsupported::Error, match: "This generator requires PostgreSQL" do | ||
run_generator | ||
|
||
assert_no_file app_root(".github/workflows/ci.yaml") | ||
assert_no_file app_root(".github/dependabot.yaml") | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def restore_destination | ||
remove_dir_if_exists ".github" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters