Module: SchemaDev::GithubActions

Extended by:
GithubActions
Included in:
GithubActions
Defined in:
lib/schema_dev/github_actions.rb

Constant Summary collapse

WORKFLOW_FILE =
'.github/workflows/prs.yml'
HEADER =
"# This file was auto-generated by the schema_dev tool, based on the data in\n#                 ./schema_dev.yml\n# Please do not edit this file; any changes will be overwritten next time\n# schema_dev gets run.\n"
BASIC_WORKFLOW =
{
  name:        'CI PR Builds',
  on:          {
    push:         {
      branches: %w[master],
    },
    pull_request: nil,
  },
  concurrency: {
    group:                '${{ github.head_ref }}',
    'cancel-in-progress': true,
  }
}.freeze
BASIC_JOB =
{ 'runs-on': 'ubuntu-latest' }.freeze
BASIC_ENV =
{
  BUNDLE_GEMFILE: '${{ github.workspace }}/gemfiles/activerecord-${{ matrix.activerecord }}/Gemfile.${{ matrix.db }}'
}.freeze
FINISH_STEPS =
[
  {
    name: 'Coveralls Finished',
    if:   '${{ !env.ACT }}',
    uses: 'coverallsapp/github-action@master',
    with: {
      'github-token':      '${{ secrets.GITHUB_TOKEN }}',
      'parallel-finished': true,
    }
  }
].freeze
STEPS =
{
  start:  [
            {
              uses: 'actions/checkout@v2',
            },
            {
              name: 'Set up Ruby',
              uses: 'ruby/setup-ruby@v1',
              with: {
                'ruby-version':  '${{ matrix.ruby }}',
                'bundler-cache': true,
              },
            },
            {
              name: 'Run bundle update',
              run:  'bundle update',
            },
          ],
  test:   [
            {
              name: 'Run tests',
              run:  'bundle exec rake spec',
            }
          ],
  finish: [
            {
              name: 'Coveralls Parallel',
              if:   '${{ !env.ACT }}',
              uses: 'coverallsapp/github-action@master',
              with: {
                'github-token': '${{ secrets.GITHUB_TOKEN }}',
                'flag-name':    'run-${{ matrix.ruby }}-${{ matrix.activerecord }}-${{ matrix.db }}-${{ matrix.dbversion }}',
                parallel:       true,
              }
            }
          ],
}.freeze
DB_ENV =
{
  postgresql: {
    POSTGRESQL_DB_HOST: '127.0.0.1',
    POSTGRESQL_DB_USER: 'schema_plus_test',
    POSTGRESQL_DB_PASS: 'database',
  },
  mysql2:     {
    MYSQL_DB_HOST: '127.0.0.1',
    MYSQL_DB_USER: 'root',
    MYSQL_DB_PASS: 'database',
  },
}.freeze
DB_STARTUP =
{
  postgresql: [
                {
                  name: 'Start Postgresql',
                  if:   "matrix.db == 'postgresql'",
                  run:  "                    docker run --rm --detach \\\\\n                      -e POSTGRES_USER=$POSTGRESQL_DB_USER \\\\\n                      -e POSTGRES_PASSWORD=$POSTGRESQL_DB_PASS \\\\\n                      -p 5432:5432 \\\\\n                      --health-cmd \"pg_isready -q\" \\\\\n                      --health-interval 5s \\\\\n                      --health-timeout 5s \\\\\n                      --health-retries 5 \\\\\n                      --name database postgres:${{ matrix.dbversion }}\n                  BASH\n                },\n              ],\n  mysql2:     [\n                {\n                  name: 'Start Mysql',\n                  if:   \"matrix.db == 'mysql2'\",\n                  run:  <<~BASH\n                    docker run --rm --detach \\\\\n                      -e MYSQL_ROOT_PASSWORD=$MYSQL_DB_PASS \\\\\n                      -p 3306:3306 \\\\\n                      --health-cmd \"mysqladmin ping --host=127.0.0.1 --password=$MYSQL_DB_PASS --silent\" \\\\\n                      --health-interval 5s \\\\\n                      --health-timeout 5s \\\\\n                      --health-retries 5 \\\\\n                      --name database mysql:5.6\n                  BASH\n                }\n              ],\n}.freeze\n"
DB_SETUP_NEEDED =
%w[postgresql mysql2].freeze
DB_SETUP =
[
  {
    name: 'Wait for database to start',
    if:   "(matrix.db == 'postgresql' || matrix.db == 'mysql2')",
    run:  "      COUNT=0\n      ATTEMPTS=20\n      until [[ $COUNT -eq $ATTEMPTS ]]; do\n        [ \"$(docker inspect -f {{.State.Health.Status}} database)\" == \"healthy\" ] && break\n        echo $(( COUNT++ )) > /dev/null\n        sleep 2\n      done\n    BASH\n  },\n  {\n    name: 'Create testing database',\n    if:   \"(matrix.db == 'postgresql' || matrix.db == 'mysql2')\",\n    run:  'bundle exec rake create_ci_database',\n  },\n].freeze\n"
DB_TEARDOWN_NEEDED =
%w[postgresql mysql2].freeze
DB_TEARDOWN =
[
  {
    name: 'Shutdown database',
    if:   "always() && (matrix.db == 'postgresql' || matrix.db == 'mysql2')",
    run:  'docker stop database',
  }
].freeze

Instance Method Summary collapse

Instance Method Details

#build(config) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# 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) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# 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) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
# 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