mirror of
https://github.com/elastic/kibana.git
synced 2025-06-27 18:51:07 -04:00
[CI] Record Github commit statuses outside of PRs (#69432)
This commit is contained in:
parent
1cdeec07b2
commit
1ad18794ef
9 changed files with 229 additions and 5 deletions
|
@ -20,6 +20,7 @@ dependencies {
|
||||||
implementation 'org.jenkins-ci.plugins.workflow:workflow-step-api:2.19@jar'
|
implementation 'org.jenkins-ci.plugins.workflow:workflow-step-api:2.19@jar'
|
||||||
testImplementation 'com.lesfurets:jenkins-pipeline-unit:1.4'
|
testImplementation 'com.lesfurets:jenkins-pipeline-unit:1.4'
|
||||||
testImplementation 'junit:junit:4.12'
|
testImplementation 'junit:junit:4.12'
|
||||||
|
testImplementation 'org.mockito:mockito-core:2.+'
|
||||||
testImplementation 'org.assertj:assertj-core:3.15+' // Temporary https://github.com/jenkinsci/JenkinsPipelineUnit/issues/209
|
testImplementation 'org.assertj:assertj-core:3.15+' // Temporary https://github.com/jenkinsci/JenkinsPipelineUnit/issues/209
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,9 +19,9 @@ class KibanaBasePipelineTest extends BasePipelineTest {
|
||||||
env.BUILD_DISPLAY_NAME = "#${env.BUILD_ID}"
|
env.BUILD_DISPLAY_NAME = "#${env.BUILD_ID}"
|
||||||
|
|
||||||
env.JENKINS_URL = 'http://jenkins.localhost:8080'
|
env.JENKINS_URL = 'http://jenkins.localhost:8080'
|
||||||
env.BUILD_URL = "${env.JENKINS_URL}/job/elastic+kibana+${env.BRANCH_NAME}/${env.BUILD_ID}/"
|
env.BUILD_URL = "${env.JENKINS_URL}/job/elastic+kibana+${env.BRANCH_NAME}/${env.BUILD_ID}/".toString()
|
||||||
|
|
||||||
env.JOB_BASE_NAME = "elastic / kibana # ${env.BRANCH_NAME}"
|
env.JOB_BASE_NAME = "elastic / kibana # ${env.BRANCH_NAME}".toString()
|
||||||
env.JOB_NAME = env.JOB_BASE_NAME
|
env.JOB_NAME = env.JOB_BASE_NAME
|
||||||
|
|
||||||
env.WORKSPACE = 'WS'
|
env.WORKSPACE = 'WS'
|
||||||
|
@ -31,6 +31,9 @@ class KibanaBasePipelineTest extends BasePipelineTest {
|
||||||
getBuildStatus: { 'SUCCESS' },
|
getBuildStatus: { 'SUCCESS' },
|
||||||
printStacktrace: { ex -> print ex },
|
printStacktrace: { ex -> print ex },
|
||||||
],
|
],
|
||||||
|
githubPr: [
|
||||||
|
isPr: { false },
|
||||||
|
],
|
||||||
jenkinsApi: [ getFailedSteps: { [] } ],
|
jenkinsApi: [ getFailedSteps: { [] } ],
|
||||||
testUtils: [ getFailures: { [] } ],
|
testUtils: [ getFailures: { [] } ],
|
||||||
])
|
])
|
||||||
|
|
48
.ci/pipeline-library/src/test/buildState.groovy
Normal file
48
.ci/pipeline-library/src/test/buildState.groovy
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
import org.junit.*
|
||||||
|
import static groovy.test.GroovyAssert.*
|
||||||
|
|
||||||
|
class BuildStateTest extends KibanaBasePipelineTest {
|
||||||
|
def buildState
|
||||||
|
|
||||||
|
@Before
|
||||||
|
void setUp() {
|
||||||
|
super.setUp()
|
||||||
|
|
||||||
|
buildState = loadScript("vars/buildState.groovy")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'get() returns existing data'() {
|
||||||
|
buildState.add('test', 1)
|
||||||
|
def actual = buildState.get('test')
|
||||||
|
assertEquals(1, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'get() returns null for missing data'() {
|
||||||
|
def actual = buildState.get('missing_key')
|
||||||
|
assertEquals(null, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'add() does not overwrite existing keys'() {
|
||||||
|
assertTrue(buildState.add('test', 1))
|
||||||
|
assertFalse(buildState.add('test', 2))
|
||||||
|
|
||||||
|
def actual = buildState.get('test')
|
||||||
|
|
||||||
|
assertEquals(1, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'set() overwrites existing keys'() {
|
||||||
|
assertFalse(buildState.has('test'))
|
||||||
|
buildState.set('test', 1)
|
||||||
|
assertTrue(buildState.has('test'))
|
||||||
|
buildState.set('test', 2)
|
||||||
|
|
||||||
|
def actual = buildState.get('test')
|
||||||
|
|
||||||
|
assertEquals(2, actual)
|
||||||
|
}
|
||||||
|
}
|
85
.ci/pipeline-library/src/test/githubCommitStatus.groovy
Normal file
85
.ci/pipeline-library/src/test/githubCommitStatus.groovy
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
import org.junit.*
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
class GithubCommitStatusTest extends KibanaBasePipelineTest {
|
||||||
|
def githubCommitStatus
|
||||||
|
def githubApiMock
|
||||||
|
def buildStateMock
|
||||||
|
|
||||||
|
def EXPECTED_STATUS_URL = 'repos/elastic/kibana/statuses/COMMIT_HASH'
|
||||||
|
def EXPECTED_CONTEXT = 'kibana-ci'
|
||||||
|
def EXPECTED_BUILD_URL = 'http://jenkins.localhost:8080/job/elastic+kibana+master/1/'
|
||||||
|
|
||||||
|
interface BuildState {
|
||||||
|
Object get(String key)
|
||||||
|
}
|
||||||
|
|
||||||
|
interface GithubApi {
|
||||||
|
Object post(String url, Map data)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
void setUp() {
|
||||||
|
super.setUp()
|
||||||
|
|
||||||
|
buildStateMock = mock(BuildState)
|
||||||
|
githubApiMock = mock(GithubApi)
|
||||||
|
|
||||||
|
when(buildStateMock.get('checkoutInfo')).thenReturn([ commit: 'COMMIT_HASH', ])
|
||||||
|
when(githubApiMock.post(any(), any())).thenReturn(null)
|
||||||
|
|
||||||
|
props([
|
||||||
|
buildState: buildStateMock,
|
||||||
|
githubApi: githubApiMock,
|
||||||
|
])
|
||||||
|
|
||||||
|
githubCommitStatus = loadScript("vars/githubCommitStatus.groovy")
|
||||||
|
}
|
||||||
|
|
||||||
|
void verifyStatusCreate(String state, String description) {
|
||||||
|
verify(githubApiMock).post(
|
||||||
|
EXPECTED_STATUS_URL,
|
||||||
|
[
|
||||||
|
'state': state,
|
||||||
|
'description': description,
|
||||||
|
'context': EXPECTED_CONTEXT,
|
||||||
|
'target_url': EXPECTED_BUILD_URL,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'onStart() should create a pending status'() {
|
||||||
|
githubCommitStatus.onStart()
|
||||||
|
verifyStatusCreate('pending', 'Build started.')
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'onFinish() should create a success status'() {
|
||||||
|
githubCommitStatus.onFinish()
|
||||||
|
verifyStatusCreate('success', 'Build completed successfully.')
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'onFinish() should create an error status for failed builds'() {
|
||||||
|
mockFailureBuild()
|
||||||
|
githubCommitStatus.onFinish()
|
||||||
|
verifyStatusCreate('error', 'Build failed.')
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'onStart() should exit early for PRs'() {
|
||||||
|
prop('githubPr', [ isPr: { true } ])
|
||||||
|
|
||||||
|
githubCommitStatus.onStart()
|
||||||
|
verifyZeroInteractions(githubApiMock)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 'onFinish() should exit early for PRs'() {
|
||||||
|
prop('githubPr', [ isPr: { true } ])
|
||||||
|
|
||||||
|
githubCommitStatus.onFinish()
|
||||||
|
verifyZeroInteractions(githubApiMock)
|
||||||
|
}
|
||||||
|
}
|
2
Jenkinsfile
vendored
2
Jenkinsfile
vendored
|
@ -3,7 +3,7 @@
|
||||||
library 'kibana-pipeline-library'
|
library 'kibana-pipeline-library'
|
||||||
kibanaLibrary.load()
|
kibanaLibrary.load()
|
||||||
|
|
||||||
kibanaPipeline(timeoutMinutes: 155, checkPrChanges: true) {
|
kibanaPipeline(timeoutMinutes: 155, checkPrChanges: true, setCommitStatus: true) {
|
||||||
githubPr.withDefaultPrComments {
|
githubPr.withDefaultPrComments {
|
||||||
ciStats.trackBuild {
|
ciStats.trackBuild {
|
||||||
catchError {
|
catchError {
|
||||||
|
|
30
vars/buildState.groovy
Normal file
30
vars/buildState.groovy
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
import groovy.transform.Field
|
||||||
|
|
||||||
|
public static @Field JENKINS_BUILD_STATE = [:]
|
||||||
|
|
||||||
|
def add(key, value) {
|
||||||
|
if (!buildState.JENKINS_BUILD_STATE.containsKey(key)) {
|
||||||
|
buildState.JENKINS_BUILD_STATE[key] = value
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
def set(key, value) {
|
||||||
|
buildState.JENKINS_BUILD_STATE[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
def get(key) {
|
||||||
|
return buildState.JENKINS_BUILD_STATE[key]
|
||||||
|
}
|
||||||
|
|
||||||
|
def has(key) {
|
||||||
|
return buildState.JENKINS_BUILD_STATE.containsKey(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
def get() {
|
||||||
|
return buildState.JENKINS_BUILD_STATE
|
||||||
|
}
|
||||||
|
|
||||||
|
return this
|
42
vars/githubCommitStatus.groovy
Normal file
42
vars/githubCommitStatus.groovy
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
def shouldCreateStatuses() {
|
||||||
|
return !githubPr.isPr() && buildState.get('checkoutInfo')
|
||||||
|
}
|
||||||
|
|
||||||
|
def onStart() {
|
||||||
|
catchError {
|
||||||
|
if (!shouldCreateStatuses()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
def checkoutInfo = buildState.get('checkoutInfo')
|
||||||
|
create(checkoutInfo.commit, 'pending', 'Build started.')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def onFinish() {
|
||||||
|
catchError {
|
||||||
|
if (!shouldCreateStatuses()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
def checkoutInfo = buildState.get('checkoutInfo')
|
||||||
|
def status = buildUtils.getBuildStatus()
|
||||||
|
|
||||||
|
if (status == 'SUCCESS' || status == 'UNSTABLE') {
|
||||||
|
create(checkoutInfo.commit, 'success', 'Build completed successfully.')
|
||||||
|
} else if(status == 'ABORTED') {
|
||||||
|
create(checkoutInfo.commit, 'error', 'Build aborted or timed out.')
|
||||||
|
} else {
|
||||||
|
create(checkoutInfo.commit, 'error', 'Build failed.')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// state: error|failure|pending|success
|
||||||
|
def create(sha, state, description, context = 'kibana-ci') {
|
||||||
|
withGithubCredentials {
|
||||||
|
return githubApi.post("repos/elastic/kibana/statuses/${sha}", [ state: state, description: description, context: context, target_url: env.BUILD_URL ])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return this
|
|
@ -214,12 +214,15 @@ def runErrorReporter() {
|
||||||
}
|
}
|
||||||
|
|
||||||
def call(Map params = [:], Closure closure) {
|
def call(Map params = [:], Closure closure) {
|
||||||
def config = [timeoutMinutes: 135, checkPrChanges: false] + params
|
def config = [timeoutMinutes: 135, checkPrChanges: false, setCommitStatus: false] + params
|
||||||
|
|
||||||
stage("Kibana Pipeline") {
|
stage("Kibana Pipeline") {
|
||||||
timeout(time: config.timeoutMinutes, unit: 'MINUTES') {
|
timeout(time: config.timeoutMinutes, unit: 'MINUTES') {
|
||||||
timestamps {
|
timestamps {
|
||||||
ansiColor('xterm') {
|
ansiColor('xterm') {
|
||||||
|
if (config.setCommitStatus) {
|
||||||
|
buildState.set('shouldSetCommitStatus', true)
|
||||||
|
}
|
||||||
if (config.checkPrChanges && githubPr.isPr()) {
|
if (config.checkPrChanges && githubPr.isPr()) {
|
||||||
pipelineLibraryTests()
|
pipelineLibraryTests()
|
||||||
|
|
||||||
|
@ -230,7 +233,13 @@ def call(Map params = [:], Closure closure) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
closure()
|
try {
|
||||||
|
closure()
|
||||||
|
} finally {
|
||||||
|
if (config.setCommitStatus) {
|
||||||
|
githubCommitStatus.onFinish()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,6 +65,12 @@ def base(Map params, Closure closure) {
|
||||||
|
|
||||||
dir("kibana") {
|
dir("kibana") {
|
||||||
checkoutInfo = getCheckoutInfo()
|
checkoutInfo = getCheckoutInfo()
|
||||||
|
|
||||||
|
// use `checkoutInfo` as a flag to indicate that we've already reported the pending commit status
|
||||||
|
if (buildState.get('shouldSetCommitStatus') && !buildState.has('checkoutInfo')) {
|
||||||
|
buildState.set('checkoutInfo', checkoutInfo)
|
||||||
|
githubCommitStatus.onStart()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ciStats.reportGitInfo(
|
ciStats.reportGitInfo(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue