-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enable opbeans pipeline #230
Changes from all commits
d0a8c5c
4378c70
015337c
a732b57
409e44e
3a9a196
463fb6e
ba1b9b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import com.lesfurets.jenkins.unit.BasePipelineTest | ||
|
||
class ApmBasePipelineTest extends BasePipelineTest { | ||
Map env = [:] | ||
|
||
@Override | ||
void setUp() { | ||
super.setUp() | ||
env.BRANCH_NAME = 'master' | ||
binding.setVariable('env', env) | ||
|
||
registerDeclarativeMethods() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Loved this separation of concerns!! |
||
registerScriptedMethods() | ||
registerSharedLibraryMethods() | ||
} | ||
|
||
void registerDeclarativeMethods() { | ||
helper.registerAllowedMethod('always', [Closure.class], null) | ||
helper.registerAllowedMethod('ansiColor', [String.class], null) | ||
helper.registerAllowedMethod('agent', [Closure.class], null) | ||
helper.registerAllowedMethod('beforeAgent', [Boolean.class], { true }) | ||
helper.registerAllowedMethod('disableResume', [], null) | ||
helper.registerAllowedMethod('durabilityHint', [String.class], null) | ||
helper.registerAllowedMethod('issueCommentTrigger', [String.class], null) | ||
helper.registerAllowedMethod('label', [String.class], null) | ||
helper.registerAllowedMethod('options', [Closure.class], null) | ||
helper.registerAllowedMethod('pipeline', [Closure.class], null) | ||
helper.registerAllowedMethod('post', [Closure.class], null) | ||
helper.registerAllowedMethod('quietPeriod', [Integer.class], null) | ||
helper.registerAllowedMethod('rateLimitBuilds', [Map.class], null) | ||
helper.registerAllowedMethod('stage', [Closure.class], null) | ||
helper.registerAllowedMethod('stage', [String.class, Closure.class], { stageName, body -> | ||
def stageResult | ||
helper.registerAllowedMethod('when', [Closure.class], { bodyWhen -> | ||
helper.registerAllowedMethod('branch', [String.class], { branchName -> | ||
if(branchName == env.BRANCH_NAME) { | ||
return true | ||
} | ||
throw new RuntimeException("Stage \"${stageName}\" skipped due to when conditional") | ||
}) | ||
return bodyWhen() | ||
}) | ||
|
||
switch (currentBuild.result) { | ||
case 'FAILURE': | ||
break | ||
default: | ||
try { | ||
stageResult = body() | ||
} | ||
catch (RuntimeException re) { | ||
// skip stage due to when conditional | ||
} | ||
catch (Exception e) { | ||
throw e | ||
} | ||
} | ||
return stageResult | ||
}) | ||
helper.registerAllowedMethod('stages', [Closure.class], null) | ||
helper.registerAllowedMethod('stash', [Map.class], null) | ||
helper.registerAllowedMethod('steps', [Closure.class], null) | ||
helper.registerAllowedMethod('timeout', [Map.class], null) | ||
helper.registerAllowedMethod('timestamps', [], null) | ||
helper.registerAllowedMethod('triggers', [Closure.class], null) | ||
} | ||
|
||
void registerScriptedMethods() { | ||
helper.registerAllowedMethod('credentials', [String.class], { s -> s }) | ||
helper.registerAllowedMethod('deleteDir', [], null) | ||
helper.registerAllowedMethod('dir', [String.class], null) | ||
helper.registerAllowedMethod('environment', [Closure.class], { Closure c -> | ||
|
||
def envBefore = [env: binding.getVariable('env')] | ||
println "Env section - original env vars: ${envBefore.toString()}" | ||
c.resolveStrategy = Closure.DELEGATE_FIRST | ||
c.delegate = envBefore | ||
c() | ||
|
||
def envNew = envBefore.env | ||
envBefore.each { k, v -> | ||
if (k != 'env') { | ||
envNew["$k"] = v | ||
} | ||
|
||
} | ||
println "Env section - env vars set to: ${envNew.toString()}" | ||
binding.setVariable('env', envNew) | ||
}) | ||
helper.registerAllowedMethod('junit', [Map.class], null) | ||
helper.registerAllowedMethod('sh', [String.class], null) | ||
helper.registerAllowedMethod('timeout', [Integer.class, Closure.class], null) | ||
helper.registerAllowedMethod('unstash', [String.class], null) | ||
} | ||
|
||
void registerSharedLibraryMethods() { | ||
helper.registerAllowedMethod('dockerLogin', [Map.class], { true }) | ||
helper.registerAllowedMethod('gitCheckout', [Map.class], null) | ||
helper.registerAllowedMethod('notifyBuildResult', [], null) | ||
helper.registerAllowedMethod('withGithubNotify', [Map.class, Closure.class], null) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import org.junit.Before | ||
import org.junit.Test | ||
import static com.lesfurets.jenkins.unit.MethodCall.callArgsToString | ||
import static org.junit.Assert.assertFalse | ||
import static org.junit.Assert.assertTrue | ||
|
||
class OpbeansPipelineStepTests extends ApmBasePipelineTest { | ||
String scriptName = 'vars/opbeansPipeline.groovy' | ||
|
||
@Override | ||
@Before | ||
void setUp() throws Exception { | ||
binding.setProperty('BASE_DIR', '/') | ||
binding.setProperty('DOCKERHUB_SECRET', 'secret') | ||
super.setUp() | ||
} | ||
|
||
@Test | ||
void test_when_master_branch() throws Exception { | ||
def script = loadScript(scriptName) | ||
script.call() | ||
printCallStack() | ||
assertTrue(helper.callStack.findAll { call -> call.methodName == 'stage' }.any { call -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NOTE: we will have to refactor this calls to grab stages, steps, whatever we need on a helper class (on in this PR). |
||
callArgsToString(call).contains('Build') | ||
}) | ||
assertTrue(helper.callStack.findAll { call -> call.methodName == 'stage' }.any { call -> | ||
callArgsToString(call).contains('Test') | ||
}) | ||
assertTrue(helper.callStack.findAll { call -> call.methodName == 'stage' }.any { call -> | ||
callArgsToString(call).contains('Release') | ||
}) | ||
assertJobStatusSuccess() | ||
} | ||
|
||
@Test | ||
void test_when_no_release() throws Exception { | ||
def script = loadScript(scriptName) | ||
// When the branch doesn't match | ||
env.BRANCH_NAME = 'foo' | ||
script.call() | ||
printCallStack() | ||
// Then no publish shell step | ||
assertFalse(helper.callStack.findAll { call -> call.methodName == 'sh' }.any { call -> | ||
callArgsToString(call).contains('make publish') | ||
}) | ||
assertJobStatusSuccess() | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
/** | ||
Opbeans Pipeline | ||
*/ | ||
|
||
def call(Map pipelineParams) { | ||
pipeline { | ||
agent { label 'linux && immutable' } | ||
environment { | ||
BASE_DIR = 'src/github.com/elastic' | ||
NOTIFY_TO = credentials('notify-to') | ||
JOB_GCS_BUCKET = credentials('gcs-bucket') | ||
JOB_GCS_CREDENTIALS = 'apm-ci-gcs-plugin' | ||
DOCKERHUB_SECRET = 'secret/apm-team/ci/elastic-observability-dockerhub' | ||
PIPELINE_LOG_LEVEL = 'INFO' | ||
PATH = "${env.PATH}:${env.WORKSPACE}/bin" | ||
HOME = "${env.WORKSPACE}" | ||
} | ||
options { | ||
timeout(time: 1, unit: 'HOURS') | ||
buildDiscarder(logRotator(numToKeepStr: '20', artifactNumToKeepStr: '20', daysToKeepStr: '30')) | ||
timestamps() | ||
ansiColor('xterm') | ||
disableResume() | ||
durabilityHint('PERFORMANCE_OPTIMIZED') | ||
rateLimitBuilds(throttle: [count: 60, durationName: 'hour', userBoost: true]) | ||
quietPeriod(10) | ||
} | ||
triggers { | ||
issueCommentTrigger('(?i).*(?:jenkins\\W+)?run\\W+(?:the\\W+)?tests(?:\\W+please)?.*') | ||
} | ||
stages { | ||
/** | ||
Checkout the code and stash it, to use it on other stages. | ||
*/ | ||
stage('Checkout') { | ||
steps { | ||
deleteDir() | ||
gitCheckout(basedir: BASE_DIR) | ||
stash allowEmpty: true, name: 'source', useDefaultExcludes: false | ||
} | ||
} | ||
/** | ||
Build the project from code.. | ||
*/ | ||
stage('Build') { | ||
steps { | ||
withGithubNotify(context: 'Build') { | ||
deleteDir() | ||
unstash 'source' | ||
dir(BASE_DIR){ | ||
sh 'make build' | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
Execute unit tests. | ||
*/ | ||
stage('Test') { | ||
steps { | ||
withGithubNotify(context: 'Test', tab: 'tests') { | ||
deleteDir() | ||
unstash 'source' | ||
dir(BASE_DIR){ | ||
sh "make test" | ||
} | ||
} | ||
} | ||
post { | ||
always { | ||
junit(allowEmptyResults: true, | ||
keepLongStdio: true, | ||
testResults: "${BASE_DIR}/**/junit-*.xml") | ||
} | ||
} | ||
} | ||
stage('Release') { | ||
when { | ||
branch 'master' | ||
beforeAgent true | ||
} | ||
steps { | ||
withGithubNotify(context: 'Release') { | ||
deleteDir() | ||
unstash 'source' | ||
dir(BASE_DIR){ | ||
dockerLogin(secret: "${DOCKERHUB_SECRET}", registry: 'docker.io') | ||
sh "VERSION=latest make publish" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
post { | ||
always { | ||
notifyBuildResult() | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Opbeans Pipeline | ||
|
||
``` | ||
opbeansPipeline() | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The automation to generate the README.md should not be affected by this hook.