module SchemaDev::GithubActions

Constants

BASIC_ENV
BASIC_JOB
BASIC_WORKFLOW
DB_ENV
DB_SETUP
DB_SETUP_NEEDED
DB_STARTUP
DB_TEARDOWN
DB_TEARDOWN_NEEDED
FINISH_STEPS
STEPS
WORKFLOW_FILE

Public Instance Methods

build(config) click to toggle source
# File lib/schema_dev/github_actions.rb, line 170
def build(config)
  matrix, env = build_matrices(config)
  db_setup    = []
  db_teardown = []
  config.db.each do |db|
    db_setup.concat DB_STARTUP.fetch(db.to_sym, [])
  end
  db_setup.concat(DB_SETUP) if config.db.any? { |e| DB_SETUP_NEEDED.include?(e) }
  db_teardown.concat(DB_TEARDOWN) if config.db.any? { |e| DB_TEARDOWN_NEEDED.include?(e) }

  strategy = {
    'fail-fast': false,
    matrix:      matrix,
  }

  steps = [
    *STEPS[:start],
    *db_setup,
    *STEPS[:test],
    *db_teardown,
    *STEPS[:finish],
  ]

  {}.tap do |workflow|
    workflow.merge!(BASIC_WORKFLOW)
    workflow[:jobs] = {
      test:   {}.merge(BASIC_JOB)
                .merge({
                         strategy: strategy,
                         env:      env,
                         steps:    steps
                       }.compact),
      finish: {
                needs: 'test'
              }.merge(BASIC_JOB)
               .merge(steps: FINISH_STEPS)
    }
  end.deep_stringify_keys
end
build_matrices(config) click to toggle source
# File lib/schema_dev/github_actions.rb, line 210
def build_matrices(config)
  include_skip = false

  matrix = {
    ruby:         config.ruby,
    activerecord: config.activerecord,
    db:           [*config.db],
    dbversion:    [],
    exclude:      [],
    include:      [],
  }

  if config.exclude.any?
    matrix[:exclude] = config.exclude.map(&:to_hash).reject { |e| e.key?(:dbversion) }
  end

  env = {}.merge(BASIC_ENV)
  config.db.each do |db|
    env.merge!(DB_ENV.fetch(db.to_sym, {}))
  end

  if config.db.include?('postgresql')
    include_skip = true
    matrix[:db].delete('postgresql')
    config.matrix(db: 'postgresql', with_dbversion: true).map do |entry|
      matrix[:include] << entry
    end
  end

  if include_skip
    matrix[:db] << 'skip'
    matrix[:dbversion] << 'skip'
    matrix[:exclude] << { db: 'skip', dbversion: 'skip' }
  end

  [
    matrix.reject { |_, val| val.empty? },
    env
  ]
end
update(config) click to toggle source
# File lib/schema_dev/github_actions.rb, line 251
def update(config)
  filepath = Pathname.new(WORKFLOW_FILE)
  filepath.dirname.mkpath
  newworkflow = build(config)
  oldworkflow = YAML.safe_load(filepath.read) rescue nil
  if oldworkflow != newworkflow
    yaml_output = newworkflow.to_yaml(line_width: -1)
    # fix for some broken libyaml implementations (< 0.2.5)
    yaml_output.gsub!('pull_request: ', 'pull_request:')
    filepath.write HEADER + yaml_output
    return true
  end
  false
end