Skip to content

Commit

Permalink
Add standard release pipeline library (#11)
Browse files Browse the repository at this point in the history
* Add standard release pipeline library

Signed-off-by: Sayali Gaikawad <gaiksaya@amazon.com>
  • Loading branch information
gaiksaya committed Nov 4, 2022
1 parent 0511a5e commit adc2338
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ lib = library(identifier: 'jenkins@<tag>', retriever: modernSCM([
]))
```

#### Library Details

| Name | Description |
|--------------------------------------------------------|:-----------------------------------------------------------------------------------------|
| [standardReleasePipeline.groovy](./vars/standardReleasePipeline.groovy) | The library sets up the necessary jenkins properties for you such as agent label, docker image to use as well as workflow time out. Check how to use the [default](./tests/jenkins/jobs/StandardReleasePipeline_JenkinsFile) in your workflow and how to [overide](./tests/jenkins/jobs/StandardReleasePipelineWithArgs_JenkinsFile) agent & docker image if you need.|

## Contributing

See [developer guide](DEVELOPER_GUIDE.md) and [how to contribute to this project](CONTRIBUTING.md).
Expand Down
68 changes: 68 additions & 0 deletions tests/jenkins/TestStandardReleasePipeline.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/


import jenkins.tests.BuildPipelineTest
import org.junit.Before
import org.junit.Test
import static org.hamcrest.MatcherAssert.assertThat
import static org.hamcrest.CoreMatchers.equalTo
import static com.lesfurets.jenkins.unit.MethodCall.callArgsToString
import static org.hamcrest.CoreMatchers.hasItem



class TestStandardReleasePipeline extends BuildPipelineTest {

@Before
void setUp() {
super.setUp()
}

@Test
void testStandardReleasePipeline() {
super.testPipeline('tests/jenkins/jobs/StandardReleasePipeline_JenkinsFile')
}

@Test
void testStandardReleasePipelineWithArgs() {
super.testPipeline('tests/jenkins/jobs/StandardReleasePipelineWithArgs_JenkinsFile')
}

@Test
void 'check override values'() {
runScript("tests/jenkins/jobs/StandardReleasePipelineWithArgs_JenkinsFile")
def echoCommand = getEchoCommands().findAll{
command -> command.contains('agent')
}

assertThat(echoCommand.size(), equalTo(1))
assertThat(echoCommand, hasItem('Executing on agent [docker:[image:test:image, reuseNode:false, stages:[:], args:, alwaysPull:true, containerPerStageRoot:false, label:AL2-X64]]'))
}

@Test
void 'check default values'(){
runScript("tests/jenkins/jobs/StandardReleasePipeline_JenkinsFile")
def echoCommand = getEchoCommands().findAll{
command -> command.contains('agent')
}

assertThat(echoCommand.size(), equalTo(1))
assertThat(echoCommand, hasItem('Executing on agent [docker:[image:opensearchstaging/ci-runner:release-centos7-clients-v1, reuseNode:false, stages:[:], args:, alwaysPull:true, containerPerStageRoot:false, label:Jenkins-Agent-AL2-X64-C54xlarge-Docker-Host]]'))
}

def getEchoCommands() {
def echoCommands = helper.callStack.findAll { call ->
call.methodName == 'echo'
}.collect { call ->
callArgsToString(call)
}
return echoCommands
}
}
12 changes: 12 additions & 0 deletions tests/jenkins/jobs/StandardReleasePipelineWithArgs_JenkinsFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
standardReleasePipeline(overrideAgent: 'AL2-X64',
overrideDockerImage: 'test:image')
{
fakePublishToMaven(
mavenArtifactsPath: "/maven",
autoPublish: true
)
}

def fakePublishToMaven(Map args) {
echo "fakePublishToMaven ${args}"
}
12 changes: 12 additions & 0 deletions tests/jenkins/jobs/StandardReleasePipelineWithArgs_JenkinsFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
StandardReleasePipelineWithArgs_JenkinsFile.run()
StandardReleasePipelineWithArgs_JenkinsFile.standardReleasePipeline({overrideAgent=AL2-X64, overrideDockerImage=test:image}, groovy.lang.Closure)
standardReleasePipeline.pipeline(groovy.lang.Closure)
standardReleasePipeline.timeout({time=1, unit=HOURS})
standardReleasePipeline.echo(Executing on agent [docker:[image:test:image, reuseNode:false, stages:[:], args:, alwaysPull:true, containerPerStageRoot:false, label:AL2-X64]])
standardReleasePipeline.stage(Release, groovy.lang.Closure)
standardReleasePipeline.script(groovy.lang.Closure)
StandardReleasePipelineWithArgs_JenkinsFile.echo(fakePublishToMaven [mavenArtifactsPath:/maven, autoPublish:true])
standardReleasePipeline.script(groovy.lang.Closure)
standardReleasePipeline.postCleanup()
postCleanup.cleanWs({disableDeferredWipeout=true, deleteDirs=true})
standardReleasePipeline.sh(docker image prune -f --all)
11 changes: 11 additions & 0 deletions tests/jenkins/jobs/StandardReleasePipeline_JenkinsFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
standardReleasePipeline()
{
fakePublishToMaven(
mavenArtifactsPath: "/maven",
autoPublish: true
)
}

def fakePublishToMaven(Map args) {
echo "fakePublishToMaven ${args}"
}
12 changes: 12 additions & 0 deletions tests/jenkins/jobs/StandardReleasePipeline_JenkinsFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
StandardReleasePipeline_JenkinsFile.run()
StandardReleasePipeline_JenkinsFile.standardReleasePipeline(groovy.lang.Closure)
standardReleasePipeline.pipeline(groovy.lang.Closure)
standardReleasePipeline.timeout({time=1, unit=HOURS})
standardReleasePipeline.echo(Executing on agent [docker:[image:opensearchstaging/ci-runner:release-centos7-clients-v1, reuseNode:false, stages:[:], args:, alwaysPull:true, containerPerStageRoot:false, label:Jenkins-Agent-AL2-X64-C54xlarge-Docker-Host]])
standardReleasePipeline.stage(Release, groovy.lang.Closure)
standardReleasePipeline.script(groovy.lang.Closure)
StandardReleasePipeline_JenkinsFile.echo(fakePublishToMaven [mavenArtifactsPath:/maven, autoPublish:true])
standardReleasePipeline.script(groovy.lang.Closure)
standardReleasePipeline.postCleanup()
postCleanup.cleanWs({disableDeferredWipeout=true, deleteDirs=true})
standardReleasePipeline.sh(docker image prune -f --all)
48 changes: 48 additions & 0 deletions vars/standardReleasePipeline.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

/** A standard release pipeline for OpenSearch projects
@param Map args = [:] args A map of the following parameters
@param body <Required> - A closure containing release steps to be executed in release stage.
@param args.overrideAgent <Optional> - Jenkins agent label to override the default.
@param args.overrideDockerImage <Optional> - Docker image to override the default.
*/

void call(Map args = [:], Closure body) {
pipeline {
agent
{
docker {
label args.overrideAgent ?: 'Jenkins-Agent-AL2-X64-C54xlarge-Docker-Host'
image args.overrideDockerImage ?: 'opensearchstaging/ci-runner:release-centos7-clients-v1'
alwaysPull true
}
}
options {
timeout(time: 1, unit: 'HOURS')
}
stages{
stage("Release") {
steps {
script {
body()
}
}
}
}
post {
always {
script {
postCleanup()
sh 'docker image prune -f --all'
}
}
}
}
}

0 comments on commit adc2338

Please sign in to comment.