Skip to content
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

Merged
merged 8 commits into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repos:
- id: check-xml
exclude: ^target/
- id: end-of-file-fixer
exclude: (^src/test/resources/preCommitToJunit/output|^target/)
exclude: (^src/test/resources/preCommitToJunit/output|^target/|^vars/README.md)
Copy link
Member Author

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.


- repo: https://github.com/detailyang/pre-commit-shell.git
rev: master
Expand Down
119 changes: 119 additions & 0 deletions src/test/groovy/ApmBasePipelineTest.groovy
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()
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
}
}
66 changes: 66 additions & 0 deletions src/test/groovy/OpbeansPipelineStepTests.groovy
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 ->
Copy link
Contributor

Choose a reason for hiding this comment

The 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()
}

}
8 changes: 8 additions & 0 deletions vars/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,13 @@ notifyBuildResult(es: 'http://elastisearch.example.com:9200', secret: 'secret/te
* shouldNotify: boolean value to decide to send or not the email notifications, by default it send
emails on Failed builds that are not pull request.

## opbeansPipeline
Opbeans Pipeline

```
opbeansPipeline()
```

## preCommit
Run the pre-commit for the given commit if provided and generates the JUnit
report if required
Expand Down Expand Up @@ -759,3 +766,4 @@ withVaultToken(path: '/foo', tokenFile: '.myfile') {

* path: root folder where the vault token will be stored. (Optional). Default: ${WORKSPACE} env variable
* tokenFile: name of the file with the token. (Optional). Default: .vault-token

117 changes: 117 additions & 0 deletions vars/opbeansPipeline.groovy
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()
}
}
}
}
5 changes: 5 additions & 0 deletions vars/opbeansPipeline.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Opbeans Pipeline

```
opbeansPipeline()
```