From adc233816626a0e97fe679a57af9c39e4f7c391d Mon Sep 17 00:00:00 2001 From: Sayali Gaikawad <61760125+gaiksaya@users.noreply.github.com> Date: Wed, 19 Oct 2022 15:52:41 -0700 Subject: [PATCH] Add standard release pipeline library (#11) * Add standard release pipeline library Signed-off-by: Sayali Gaikawad --- README.md | 6 ++ .../TestStandardReleasePipeline.groovy | 68 +++++++++++++++++++ ...tandardReleasePipelineWithArgs_JenkinsFile | 12 ++++ ...ardReleasePipelineWithArgs_JenkinsFile.txt | 12 ++++ .../jobs/StandardReleasePipeline_JenkinsFile | 11 +++ .../StandardReleasePipeline_JenkinsFile.txt | 12 ++++ vars/standardReleasePipeline.groovy | 48 +++++++++++++ 7 files changed, 169 insertions(+) create mode 100644 tests/jenkins/TestStandardReleasePipeline.groovy create mode 100644 tests/jenkins/jobs/StandardReleasePipelineWithArgs_JenkinsFile create mode 100644 tests/jenkins/jobs/StandardReleasePipelineWithArgs_JenkinsFile.txt create mode 100644 tests/jenkins/jobs/StandardReleasePipeline_JenkinsFile create mode 100644 tests/jenkins/jobs/StandardReleasePipeline_JenkinsFile.txt create mode 100644 vars/standardReleasePipeline.groovy diff --git a/README.md b/README.md index 4376c4eed..de6fc4ad5 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,12 @@ lib = library(identifier: 'jenkins@', 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). diff --git a/tests/jenkins/TestStandardReleasePipeline.groovy b/tests/jenkins/TestStandardReleasePipeline.groovy new file mode 100644 index 000000000..6f0fdfa6a --- /dev/null +++ b/tests/jenkins/TestStandardReleasePipeline.groovy @@ -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 + } +} diff --git a/tests/jenkins/jobs/StandardReleasePipelineWithArgs_JenkinsFile b/tests/jenkins/jobs/StandardReleasePipelineWithArgs_JenkinsFile new file mode 100644 index 000000000..4cb1bbec8 --- /dev/null +++ b/tests/jenkins/jobs/StandardReleasePipelineWithArgs_JenkinsFile @@ -0,0 +1,12 @@ +standardReleasePipeline(overrideAgent: 'AL2-X64', + overrideDockerImage: 'test:image') +{ + fakePublishToMaven( + mavenArtifactsPath: "/maven", + autoPublish: true + ) +} + +def fakePublishToMaven(Map args) { + echo "fakePublishToMaven ${args}" +} \ No newline at end of file diff --git a/tests/jenkins/jobs/StandardReleasePipelineWithArgs_JenkinsFile.txt b/tests/jenkins/jobs/StandardReleasePipelineWithArgs_JenkinsFile.txt new file mode 100644 index 000000000..b30fd1ff1 --- /dev/null +++ b/tests/jenkins/jobs/StandardReleasePipelineWithArgs_JenkinsFile.txt @@ -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) diff --git a/tests/jenkins/jobs/StandardReleasePipeline_JenkinsFile b/tests/jenkins/jobs/StandardReleasePipeline_JenkinsFile new file mode 100644 index 000000000..0291aaffe --- /dev/null +++ b/tests/jenkins/jobs/StandardReleasePipeline_JenkinsFile @@ -0,0 +1,11 @@ +standardReleasePipeline() +{ + fakePublishToMaven( + mavenArtifactsPath: "/maven", + autoPublish: true + ) +} + +def fakePublishToMaven(Map args) { + echo "fakePublishToMaven ${args}" +} \ No newline at end of file diff --git a/tests/jenkins/jobs/StandardReleasePipeline_JenkinsFile.txt b/tests/jenkins/jobs/StandardReleasePipeline_JenkinsFile.txt new file mode 100644 index 000000000..a44747eed --- /dev/null +++ b/tests/jenkins/jobs/StandardReleasePipeline_JenkinsFile.txt @@ -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) diff --git a/vars/standardReleasePipeline.groovy b/vars/standardReleasePipeline.groovy new file mode 100644 index 000000000..5e8390dad --- /dev/null +++ b/vars/standardReleasePipeline.groovy @@ -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 - A closure containing release steps to be executed in release stage. +@param args.overrideAgent - Jenkins agent label to override the default. +@param args.overrideDockerImage - 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' + } + } + } + } + } \ No newline at end of file