2022-01-08 02:04:49 +08:00
|
|
|
#!/usr/bin/env groovy
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 - present Instructure, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of Canvas.
|
|
|
|
*
|
|
|
|
* Canvas is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
* Software Foundation, version 3 of the License.
|
|
|
|
*
|
|
|
|
* Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
library "canvas-builds-library@${env.CANVAS_BUILDS_REFSPEC}"
|
|
|
|
loadLocalLibrary('local-lib', 'build/new-jenkins/library')
|
|
|
|
|
2023-03-23 21:38:35 +08:00
|
|
|
commitMessageFlag.setDefaultValues(commitMessageFlagDefaults() + commitMessageFlagPrivateDefaults())
|
2023-03-16 03:49:44 +08:00
|
|
|
|
2022-01-08 02:04:49 +08:00
|
|
|
@groovy.transform.Field
|
2022-02-02 03:47:56 +08:00
|
|
|
def rspecqNodeTotal = 50
|
2022-01-26 00:27:20 +08:00
|
|
|
@groovy.transform.Field
|
2022-10-14 23:43:33 +08:00
|
|
|
def rspecqTargetTime = 180
|
|
|
|
@groovy.transform.Field
|
2022-01-28 01:46:18 +08:00
|
|
|
def summaryMessage = ''
|
2022-01-26 00:27:20 +08:00
|
|
|
@groovy.transform.Field
|
|
|
|
def crystalballColor = 'danger'
|
2022-06-23 00:31:52 +08:00
|
|
|
@groovy.transform.Field
|
|
|
|
def specUnique = []
|
2022-01-08 02:04:49 +08:00
|
|
|
|
2022-06-22 04:45:16 +08:00
|
|
|
def computeTestCountNecessary() {
|
|
|
|
copyArtifacts filter: 'tmp/crystalball_spec_list.txt', projectName: "${env.UPSTREAM}", selector: upstream()
|
|
|
|
copyArtifacts filter: 'tmp/crystalball_map_version.txt', projectName: "${env.UPSTREAM}", selector: upstream()
|
2022-01-08 02:04:49 +08:00
|
|
|
|
2022-01-13 03:01:15 +08:00
|
|
|
def specs = readFile(file: 'tmp/crystalball_spec_list.txt')
|
2022-01-25 04:18:53 +08:00
|
|
|
def mapVersion = readFile(file: 'tmp/crystalball_map_version.txt')
|
2022-06-23 00:31:52 +08:00
|
|
|
specUnique = specs.tokenize(',').unique()
|
2022-02-02 03:47:56 +08:00
|
|
|
env.CRYSTALBALL_SPEC = specUnique.join(' ')
|
2022-01-15 01:25:09 +08:00
|
|
|
|
2022-01-28 01:46:18 +08:00
|
|
|
summaryMessage = "Map: $mapVersion\n"
|
2022-02-18 05:23:42 +08:00
|
|
|
|
2023-03-16 00:13:47 +08:00
|
|
|
if (commitMessageFlag('skip-crystalball') as Boolean) {
|
2022-02-18 05:23:42 +08:00
|
|
|
summaryMessage += "Skip Crystalball Detected! - Running everything!"
|
|
|
|
crystalballColor = 'danger'
|
|
|
|
env.CRYSTALBALL_SPEC = '.'
|
2022-06-22 04:45:16 +08:00
|
|
|
return false
|
2022-02-18 05:23:42 +08:00
|
|
|
}
|
|
|
|
|
2022-01-15 03:36:30 +08:00
|
|
|
// Crystalball predictor returned empty text file
|
|
|
|
if (specUnique.size() == 0) {
|
2022-02-17 04:30:19 +08:00
|
|
|
summaryMessage += "Configuration Changes Detected! - Running everything!"
|
2022-01-28 01:46:18 +08:00
|
|
|
crystalballColor = 'danger'
|
2022-02-02 03:47:56 +08:00
|
|
|
env.CRYSTALBALL_SPEC = '.'
|
2022-06-22 04:45:16 +08:00
|
|
|
return false
|
2022-01-13 03:01:15 +08:00
|
|
|
}
|
2022-01-15 03:36:30 +08:00
|
|
|
// Crystalball predictor returned "."
|
|
|
|
if (specUnique.size() == 1 && specUnique[0] == '.') {
|
2022-01-28 01:46:18 +08:00
|
|
|
summaryMessage += "New File Detected! - Complete Suite Re-run!"
|
|
|
|
crystalballColor = 'danger'
|
2022-02-02 03:47:56 +08:00
|
|
|
env.CRYSTALBALL_SPEC = '.'
|
2022-06-22 04:45:16 +08:00
|
|
|
return false
|
2022-01-14 05:51:53 +08:00
|
|
|
}
|
2022-01-28 01:46:18 +08:00
|
|
|
|
2022-06-22 04:45:16 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
def computeTestCount() {
|
|
|
|
credentials.withStarlordCredentials { ->
|
|
|
|
sh(script: 'build/new-jenkins/docker-compose-pull.sh', label: 'Pull Images')
|
|
|
|
}
|
|
|
|
|
|
|
|
sh(script: 'build/new-jenkins/docker-compose-build-up.sh', label: 'Start Containers')
|
|
|
|
|
2022-06-02 22:10:46 +08:00
|
|
|
sh(script: "docker-compose exec -T canvas bundle exec rspec --dry-run \
|
|
|
|
--require './spec/formatters/node_count_formatter.rb' \
|
|
|
|
--format NodeCountRecorder \
|
|
|
|
--out formatter_out.txt --pattern ${specUnique.join(',')}", label: 'Get Node Count')
|
|
|
|
|
|
|
|
def formatterResult = sh(script: 'docker-compose exec -T canvas cat formatter_out.txt', returnStdout: true).trim().split(" ")
|
2022-01-26 00:27:20 +08:00
|
|
|
|
2022-06-03 06:22:56 +08:00
|
|
|
def nodeTotal = formatterResult[0].toInteger()
|
|
|
|
def specTotal = formatterResult[1].toInteger()
|
2022-01-28 01:46:18 +08:00
|
|
|
|
2022-06-02 22:10:46 +08:00
|
|
|
summaryMessage += "${specTotal} specs across ${specUnique.size()} files using ${nodeTotal} nodes"
|
2022-01-14 23:08:01 +08:00
|
|
|
|
2022-06-02 22:10:46 +08:00
|
|
|
echo "=== Prediction Summary ===\n${summaryMessage}\n${specUnique.join('\n')}"
|
2022-01-14 05:51:53 +08:00
|
|
|
|
2022-06-02 22:10:46 +08:00
|
|
|
crystalballColor = nodeTotal > rspecqNodeTotal ? 'warning' : 'good'
|
|
|
|
rspecqNodeTotal = nodeTotal > rspecqNodeTotal ? rspecqNodeTotal : nodeTotal
|
2022-10-14 23:43:33 +08:00
|
|
|
|
|
|
|
def jqAddCommand = []
|
|
|
|
|
|
|
|
specUnique.each { x ->
|
2022-10-19 18:39:02 +08:00
|
|
|
def globMatcher = x =~ /^(.+)\*spec\.rb$/
|
|
|
|
if (globMatcher.matches()) {
|
|
|
|
jqAddCommand.add("([with_entries(select(.key | match(\"${globMatcher.group(1)}\"))) | to_entries[] | .value] | add)")
|
|
|
|
} else {
|
|
|
|
jqAddCommand.add(".[\"./${x}\"]")
|
|
|
|
}
|
2022-10-14 23:43:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
def specTimings = sh(script: """#!/usr/bin/env bash
|
|
|
|
set -ex
|
|
|
|
|
|
|
|
docker run \
|
|
|
|
-e TEST_QUEUE_HOST \
|
|
|
|
-v \$(pwd)/build/new-jenkins/iterscores.lua:/iterscores.lua \
|
|
|
|
-t \
|
|
|
|
--rm \
|
|
|
|
$REGISTRY_BASE/redis:alpine /bin/sh -c 'redis-cli -h $TEST_QUEUE_HOST -p 6379 --eval /iterscores.lua' | jq 'fromjson | ${jqAddCommand.join(' + ')}'
|
|
|
|
""", returnStdout: true)
|
|
|
|
|
2023-06-14 07:37:44 +08:00
|
|
|
if (specTimings && specTimings.isNumber()) {
|
2023-06-14 05:10:03 +08:00
|
|
|
echo "=== Total Spec Time $specTimings"
|
2022-10-14 23:43:33 +08:00
|
|
|
|
2023-06-14 05:10:03 +08:00
|
|
|
def newNodeTotal = Math.ceil(specTimings.toFloat() / (rspecqTargetTime * RSPEC_PROCESSES.toInteger()))
|
2022-10-14 23:43:33 +08:00
|
|
|
|
2023-06-14 05:10:03 +08:00
|
|
|
summaryMessage += " (new: ${newNodeTotal})"
|
2023-06-14 06:08:20 +08:00
|
|
|
} else {
|
|
|
|
echo "=== No Spec Timings Found! ==="
|
2023-06-14 05:10:03 +08:00
|
|
|
}
|
2022-01-13 03:01:15 +08:00
|
|
|
}
|
|
|
|
|
2022-01-26 00:27:20 +08:00
|
|
|
def sendCrystalballSlack(summary, color, status) {
|
|
|
|
def jobInfo = "<https://gerrit.instructure.com/$env.GERRIT_CHANGE_NUMBER|Gerrit> | <$env.BUILD_URL|Jenkins>: **$status!**"
|
2022-01-14 05:51:53 +08:00
|
|
|
def message = "$jobInfo\n$summary"
|
|
|
|
slackSend channel: '#crystalball-noisy', color: color, message: message
|
2022-01-14 00:47:10 +08:00
|
|
|
}
|
|
|
|
|
2022-02-02 03:47:56 +08:00
|
|
|
def sendCrystalballMetrics() {
|
2022-02-02 22:42:25 +08:00
|
|
|
def queueInfo = sh(script: "docker run -e TEST_QUEUE_HOST -t --rm $REGISTRY_BASE/redis:alpine /bin/sh -c '\
|
2022-02-02 03:47:56 +08:00
|
|
|
redis-cli -h $TEST_QUEUE_HOST -p 6379 get ${JOB_NAME}_build${BUILD_NUMBER}:example_count;\
|
|
|
|
redis-cli -h $TEST_QUEUE_HOST -p 6379 get ${JOB_NAME}_build${BUILD_NUMBER}:build_time'", returnStdout: true).split('\n')
|
2022-02-02 22:42:25 +08:00
|
|
|
def exampleCount = queueInfo[0].replaceAll('"', '').trim()
|
|
|
|
def buildTime = queueInfo[1].replaceAll('"', '').trim()
|
2022-01-22 03:53:07 +08:00
|
|
|
|
2022-02-02 03:47:56 +08:00
|
|
|
reportToSplunk('rspecq_crystalball_data', [
|
|
|
|
'node_count': rspecqNodeTotal,
|
2022-01-22 03:53:07 +08:00
|
|
|
'example_count': exampleCount.toInteger(),
|
2022-02-02 22:42:25 +08:00
|
|
|
'execution_time': buildTime.toInteger(),
|
2022-01-22 03:53:07 +08:00
|
|
|
'result': currentBuild.currentResult,
|
|
|
|
'upstream_tag': "${env.UPSTREAM_TAG}"])
|
|
|
|
}
|
|
|
|
|
2022-01-08 02:04:49 +08:00
|
|
|
def redisUrl() {
|
|
|
|
return "redis://${TEST_QUEUE_HOST}:6379"
|
|
|
|
}
|
|
|
|
|
|
|
|
pipeline {
|
|
|
|
agent none
|
|
|
|
options {
|
|
|
|
ansiColor('xterm')
|
|
|
|
timeout(60)
|
|
|
|
timestamps()
|
|
|
|
}
|
|
|
|
|
|
|
|
environment {
|
2022-01-28 01:46:18 +08:00
|
|
|
REGISTRY_BASE = 'starlord.inscloudgate.net/jenkins'
|
2022-01-08 02:04:49 +08:00
|
|
|
COMPOSE_FILE = 'docker-compose.new-jenkins.yml:docker-compose.new-jenkins-selenium.yml'
|
2022-06-08 04:48:32 +08:00
|
|
|
COMPOSE_PROJECT_NAME = 'test-queue'
|
2023-03-17 02:00:28 +08:00
|
|
|
FORCE_FAILURE = commitMessageFlag("force-failure-rspec").asBooleanInteger()
|
2023-03-16 03:49:44 +08:00
|
|
|
RERUNS_RETRY = commitMessageFlag('rspecq-max-requeues').asType(Integer)
|
|
|
|
RSPEC_PROCESSES = commitMessageFlag('rspecq-processes').asType(Integer)
|
|
|
|
RSPECQ_FILE_SPLIT_THRESHOLD = commitMessageFlag('rspecq-file-split-threshold').asType(Integer)
|
|
|
|
RSPECQ_MAX_REQUEUES = commitMessageFlag('rspecq-max-requeues').asType(Integer)
|
2022-01-08 02:04:49 +08:00
|
|
|
TEST_PATTERN = '^./(spec|gems/plugins/.*/spec_canvas)/'
|
2022-02-02 03:47:56 +08:00
|
|
|
EXCLUDE_TESTS = '.*/(selenium/performance|instfs/selenium|contracts)'
|
2022-01-08 02:04:49 +08:00
|
|
|
RSPECQ_UPDATE_TIMINGS = "${env.GERRIT_EVENT_TYPE == 'change-merged' ? '1' : '0'}"
|
|
|
|
ENABLE_AXE_SELENIUM = "${env.ENABLE_AXE_SELENIUM}"
|
|
|
|
POSTGRES_PASSWORD = 'sekret'
|
|
|
|
RSPECQ_REDIS_URL = redisUrl()
|
|
|
|
}
|
|
|
|
|
|
|
|
stages {
|
|
|
|
stage('Environment') {
|
|
|
|
steps {
|
|
|
|
script {
|
2023-03-17 02:46:17 +08:00
|
|
|
def rspecNodeRequirements = [label: nodeLabel()]
|
2022-02-02 03:47:56 +08:00
|
|
|
env.CRYSTALBALL_SPEC = '.'
|
2022-01-08 02:04:49 +08:00
|
|
|
|
2022-01-22 03:53:07 +08:00
|
|
|
def postRunnerHandler = [
|
2022-01-08 02:04:49 +08:00
|
|
|
onStageEnded: { stageName, stageConfig, result ->
|
|
|
|
node('master') {
|
2022-03-18 02:00:16 +08:00
|
|
|
if (!configuration.isChangeMerged() && env.GERRIT_REFSPEC != "refs/heads/master" && env.ENABLE_CRYSTALBALL == '1') {
|
2022-01-28 01:46:18 +08:00
|
|
|
sendCrystalballSlack(summaryMessage, crystalballColor, stageConfig.status())
|
2022-01-26 00:27:20 +08:00
|
|
|
}
|
2022-01-08 02:04:49 +08:00
|
|
|
buildSummaryReport.saveRunManifest()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2022-01-22 03:53:07 +08:00
|
|
|
def postBuildHandler = [
|
2022-01-28 01:46:18 +08:00
|
|
|
onNodeReleasing: {
|
2022-03-18 02:00:16 +08:00
|
|
|
if (!configuration.isChangeMerged() && env.GERRIT_REFSPEC != "refs/heads/master" && env.ENABLE_CRYSTALBALL == '1') {
|
2022-02-02 03:47:56 +08:00
|
|
|
sendCrystalballMetrics()
|
2022-01-28 01:46:18 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-22 03:53:07 +08:00
|
|
|
]
|
|
|
|
|
2022-01-08 02:04:49 +08:00
|
|
|
def postStageHandler = [
|
|
|
|
onStageEnded: { stageName, stageConfig, result ->
|
|
|
|
buildSummaryReport.setStageTimings(stageName, stageConfig.timingValues())
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2022-01-22 03:53:07 +08:00
|
|
|
extendedStage('Runner').hooks(postRunnerHandler).obeysAllowStages(false).execute {
|
2022-06-01 09:27:33 +08:00
|
|
|
extendedStage('Builder').hooks(postStageHandler + postBuildHandler).obeysAllowStages(false).nodeRequirements(rspecNodeRequirements).execute {
|
2022-06-01 03:43:44 +08:00
|
|
|
extendedStage('RSpecQ Setup').hooks(postStageHandler).obeysAllowStages(false).execute {
|
2022-06-22 04:45:16 +08:00
|
|
|
sh 'rm -vrf ./tmp'
|
|
|
|
|
|
|
|
checkout scm
|
|
|
|
|
|
|
|
distribution.stashBuildScripts()
|
2022-01-08 02:04:49 +08:00
|
|
|
}
|
|
|
|
|
2022-06-22 04:45:16 +08:00
|
|
|
def testCountNecessary = !configuration.isChangeMerged() && env.GERRIT_REFSPEC != "refs/heads/master" && env.ENABLE_CRYSTALBALL == '1' && computeTestCountNecessary()
|
|
|
|
|
|
|
|
if (testCountNecessary) {
|
2022-06-01 03:43:44 +08:00
|
|
|
extendedStage('RSpecQ Compute Build Distribution').hooks(postStageHandler).obeysAllowStages(false).execute {
|
2022-06-22 04:45:16 +08:00
|
|
|
|
2022-01-13 03:01:15 +08:00
|
|
|
computeTestCount()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 21:50:07 +08:00
|
|
|
extendedStage('Parallel Run Tests').obeysAllowStages(false).execute { stageConfig, buildConfig ->
|
2022-01-08 02:04:49 +08:00
|
|
|
def rspecqStages = [:]
|
|
|
|
|
2022-06-22 04:45:16 +08:00
|
|
|
def initialNodeHooks = testCountNecessary ?
|
|
|
|
postStageHandler + [onNodeReleasing: { rspecStage.tearDownNode() }] :
|
|
|
|
postStageHandler + [onNodeAcquired: { rspecStage.setupNode() }, onNodeReleasing: { rspecStage.tearDownNode() }]
|
2022-01-08 02:04:49 +08:00
|
|
|
|
2022-06-22 04:45:16 +08:00
|
|
|
extendedStage('RSpecQ Initial Node')
|
2022-01-08 02:04:49 +08:00
|
|
|
.envVars(['CI_NODE_INDEX=0',
|
2022-02-02 03:47:56 +08:00
|
|
|
"CRYSTAL_BALL_SPECS=${env.CRYSTALBALL_SPEC}",
|
|
|
|
"BUILD_NAME=${env.JOB_NAME}_build${env.BUILD_NUMBER}"])
|
2022-06-22 04:45:16 +08:00
|
|
|
.hooks(initialNodeHooks)
|
2024-08-08 07:00:02 +08:00
|
|
|
.timeout(env.NODE_TIMEOUT.toInteger())
|
2022-06-22 04:45:16 +08:00
|
|
|
.queue(rspecqStages) {
|
|
|
|
def initialNodeStages = [:]
|
|
|
|
|
|
|
|
extendedStage('RSpecQ Reporter').queue(initialNodeStages) {
|
|
|
|
try {
|
|
|
|
sh(script: "docker run -e SENTRY_DSN -e RSPECQ_REDIS_URL -t $PATCHSET_TAG bundle exec rspecq \
|
|
|
|
--build=${JOB_NAME}_build${BUILD_NUMBER} \
|
|
|
|
--queue-wait-timeout 120 \
|
|
|
|
--redis-url $RSPECQ_REDIS_URL \
|
|
|
|
--report", label: 'Reporter')
|
|
|
|
} catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) {
|
|
|
|
if (e.causes[0] instanceof org.jenkinsci.plugins.workflow.steps.TimeoutStepExecution.ExceededTimeout) {
|
|
|
|
/* groovylint-disable-next-line GStringExpressionWithinString, SpaceInsideParentheses */
|
|
|
|
sh '''#!/bin/bash
|
|
|
|
ids=($(docker ps -aq --filter "name=canvas_"))
|
|
|
|
for i in "${ids[@]}"
|
|
|
|
do
|
|
|
|
docker exec $i bash -c "cat /usr/src/app/log/cmd_output/*.log"
|
|
|
|
done
|
|
|
|
'''
|
|
|
|
}
|
|
|
|
throw e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extendedStage("RSpecQ Set 00").queue(initialNodeStages) {
|
|
|
|
rspecStage.runRspecqSuite()
|
|
|
|
}
|
|
|
|
|
|
|
|
parallel(initialNodeStages)
|
|
|
|
}
|
2022-01-08 02:04:49 +08:00
|
|
|
|
2022-02-02 03:47:56 +08:00
|
|
|
for (int i = 1; i < rspecqNodeTotal; i++) {
|
2022-01-08 02:04:49 +08:00
|
|
|
def index = i
|
2022-02-02 03:47:56 +08:00
|
|
|
extendedStage("RSpecQ Set ${(index).toString().padLeft(2, '0')}")
|
2022-01-08 02:04:49 +08:00
|
|
|
.envVars(["CI_NODE_INDEX=$index",
|
2022-02-02 03:47:56 +08:00
|
|
|
"CRYSTAL_BALL_SPECS=${env.CRYSTALBALL_SPEC}",
|
|
|
|
"BUILD_NAME=${env.JOB_NAME}_build${env.BUILD_NUMBER}"])
|
2022-06-08 04:48:32 +08:00
|
|
|
.hooks(postStageHandler + [onNodeAcquired: { rspecStage.setupNode() }, onNodeReleasing: { rspecStage.tearDownNode() }])
|
2022-01-08 02:04:49 +08:00
|
|
|
.nodeRequirements(rspecNodeRequirements)
|
2024-08-08 07:00:02 +08:00
|
|
|
.timeout(env.NODE_TIMEOUT.toInteger())
|
2022-01-08 02:04:49 +08:00
|
|
|
.queue(rspecqStages) { rspecStage.runRspecqSuite() }
|
|
|
|
}
|
|
|
|
|
|
|
|
parallel(rspecqStages)
|
|
|
|
} //rspecQ
|
|
|
|
} //builder
|
|
|
|
} //runner
|
|
|
|
} //script
|
|
|
|
} //steps
|
|
|
|
} //environment
|
|
|
|
} //stages
|
|
|
|
} //pipeline
|