From 8211590b398dc11ea02822dfbddc6a5850a62d16 Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Mon, 6 Jan 2025 18:20:40 +0100 Subject: [PATCH] Rewrite Foreman's testing to a matrix This makes it easier to change the Ruby versions and also test on multiple at the same time. --- theforeman.org/pipelines/lib/foreman.groovy | 13 ++ .../pipelines/release/source/foreman.groovy | 126 +++++++----------- 2 files changed, 59 insertions(+), 80 deletions(-) diff --git a/theforeman.org/pipelines/lib/foreman.groovy b/theforeman.org/pipelines/lib/foreman.groovy index 97242bdc..f9970ed5 100644 --- a/theforeman.org/pipelines/lib/foreman.groovy +++ b/theforeman.org/pipelines/lib/foreman.groovy @@ -1,3 +1,16 @@ +def databaseUrlForTask(task) { + if (task == 'assets:precompile') { + return 'nulldb://nohost' + } else { + database = UUID.randomUUID().toString() + return "postgresql://foreman:foreman@localhost/foreman-${database}" + } +} + +def railsEnvForTask(task) { + return task == 'assets:precompile' ? 'production' : 'test' +} + def databaseFile(id) { text = postgresqlTemplate(id) writeFile(file: 'config/database.yml', text: text) diff --git a/theforeman.org/pipelines/release/source/foreman.groovy b/theforeman.org/pipelines/release/source/foreman.groovy index 9943a0bc..2aee1cb7 100644 --- a/theforeman.org/pipelines/release/source/foreman.groovy +++ b/theforeman.org/pipelines/release/source/foreman.groovy @@ -8,98 +8,64 @@ pipeline { buildDiscarder(logRotator(daysToKeepStr: '7')) } + environment { + RUBY_VERSION = '2.7.6' + BUNDLE_WITHOUT = 'development' + RAILS_ENV = railsEnvForTask(RAKE_TASK) + DATABASE_URL = databaseUrlForTask(RAKE_TASK) + TESTOPTS = '-v' + } + stages { stage('Test Matrix') { - parallel { - stage('ruby-2.7-postgres') { - agent { label 'fast' } - environment { - RUBY_VER = '2.7.6' - } - stages { - stage("setup-2.7-postgres") { - steps { - git url: git_url, branch: git_ref - script { - archive_git_hash() - } - databaseFile("${env.JOB_NAME}-${env.BUILD_ID}") - configureDatabase(env.RUBY_VER) - } - } - stage("unit-tests-2.7-postgres") { - steps { - bundleExec(env.RUBY_VER, 'rake jenkins:unit TESTOPTS="-v" --trace') - } - } - } - post { - always { - junit(testResults: 'jenkins/reports/unit/*.xml') - } - cleanup { - cleanup(env.RUBY_VER) - deleteDir() - } + matrix { + agent { label 'fast' } + axes { + axis { + name 'RAKE_TASK' + values 'jenkins:unit', 'jenkins:integration', 'assets:precompile' } } - stage('ruby-2.7-postgres-integrations') { - agent { label 'fast' } - environment { - RUBY_VER = '2.7.6' - } - stages { - stage("setup-2.7-postgres-ui") { - steps { - git url: git_url, branch: git_ref - databaseFile("${env.JOB_NAME}-${env.BUILD_ID}-ui") - configureDatabase(env.RUBY_VER) - withRuby(env.RUBY_VER, 'npm install --no-audit --legacy-peer-deps') - archiveArtifacts(artifacts: 'package-lock.json') + stages { + stage('setup') { + steps { + git url: git_url, branch: git_ref + script { + archive_git_hash() } - } - stage("integration-tests-2.7-postgres-ui") { - steps { - bundleExec(env.RUBY_VER, 'rake jenkins:integration TESTOPTS="-v" --trace') + bundleInstall(RUBY_VERSION) + archiveArtifacts(artifacts: 'Gemfile.lock') + script { + if (RAKE_TASK == 'assets:precompile') { + sh "cp db/schema.rb.nulldb db/schema.rb" + filter_package_json(RUBY_VERSION) + } + if (RAKE_TASK == 'jenkins:integration' || RAKE_TASK == 'assets:precompile' ){ + withRuby(RUBY_VERSION, 'npm install --no-audit --legacy-peer-deps') + archiveArtifacts(artifacts: 'package-lock.json') + } } } } - post { - always { - junit(testResults: 'jenkins/reports/unit/*.xml') + stage('database') { + steps { + bundleExec(RUBY_VERSION, "rake db:create --trace") + bundleExec(RUBY_VERSION, "rake db:migrate --trace") } - cleanup { - cleanup(env.RUBY_VER) - deleteDir() + } + stage('rake task') { + steps { + bundleExec(RUBY_VERSION, "rake ${RAKE_TASK} --trace") } } } - stage('ruby-2.7-nulldb-assets') { - agent { label 'fast' } - environment { - RUBY_VER = '2.7.6' + post { + always { + junit(testResults: 'jenkins/reports/*/*.xml', allowEmptyResults: RAKE_TASK == 'assets:precompile') } - stages { - stage("setup-2.7-nulldb") { - steps { - git url: git_url, branch: git_ref - bundleInstall(env.RUBY_VER, '--without=development') - sh "cp db/schema.rb.nulldb db/schema.rb" - filter_package_json(env.RUBY_VER) - withRuby(env.RUBY_VER, 'npm install --no-audit --legacy-peer-deps') - } - } - stage("assets-precompile-2.7-nulldb") { - steps { - bundleExec(env.RUBY_VER, 'rake assets:precompile RAILS_ENV=production DATABASE_URL=nulldb://nohost') - } - } - } - post { - cleanup { - cleanup(env.RUBY_VER) - deleteDir() - } + cleanup { + bundleExec(RUBY_VERSION, 'rake db:drop DISABLE_DATABASE_ENVIRONMENT_CHECK=true') + deleteDir() } } } @@ -110,7 +76,7 @@ pipeline { git url: git_url, branch: git_ref } script { - sourcefile_paths = generate_sourcefiles(project_name: project_name, source_type: source_type) + sourcefile_paths = generate_sourcefiles(project_name: project_name, source_type: source_type, ruby_version: RUBY_VERSION) } } }