-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add standard release pipeline library with generic trigger (#22)
Signed-off-by: Sayali Gaikawad <[email protected]>
- Loading branch information
Showing
10 changed files
with
244 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
tests/jenkins/TestStandardReleasePipelineWithGenericTriggers.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* 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 TestStandardReleasePipelineWithGenericTriggers extends BuildPipelineTest { | ||
|
||
@Before | ||
void setUp() { | ||
helper.registerAllowedMethod("GenericTrigger", [Map.class], null) | ||
binding.setVariable('tag', '1.0.0') | ||
binding.setVariable('release_url', 'https://api.github.com/repos/Codertocat/Hello-World/releases/17372790') | ||
super.setUp() | ||
} | ||
|
||
|
||
@Test | ||
void testStandardReleasePipelineWithGenericTriggers() { | ||
super.testPipeline('tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggers_Jenkinsfile') | ||
} | ||
|
||
@Test | ||
void testStandardReleasePipelineWithTagTriggers() { | ||
super.testPipeline('tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggersTag_Jenkinsfile') | ||
} | ||
|
||
@Test | ||
void 'validate override values'() { | ||
runScript("tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggers_Jenkinsfile") | ||
def echoCommand = getCommands('echo').findAll{ | ||
command -> command.contains('agent') | ||
} | ||
|
||
assertThat(echoCommand.size(), equalTo(1)) | ||
assertThat(echoCommand, hasItem('Executing on agent [docker:[image:centos:7, reuseNode:false, stages:[:], args:, alwaysPull:true, containerPerStageRoot:false, label:AL2-X64]]')) | ||
} | ||
|
||
@Test | ||
void 'validate default triggers'(){ | ||
runScript("tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggers_Jenkinsfile") | ||
def cmd = getCommands('GenericTrigger').findAll{ | ||
c -> c.contains('generic') | ||
} | ||
assertThat(cmd.size(), equalTo(1)) | ||
assertThat(cmd, hasItem('{genericVariables=[{key=ref, value=$.release.tag_name}, {key=isDraft, value=$.release.draft}, {key=release_url, value=$.release.url}], tokenCredentialId=opensearch-ci-webhook-trigger-token, causeString=A tag was cut on opensearch-ci repo, printContributedVariables=false, printPostContent=false, regexpFilterText=$isDraft, regexpFilterExpression=true}')) | ||
} | ||
|
||
@Test | ||
void 'validate release is published'(){ | ||
runScript("tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggers_Jenkinsfile") | ||
def cmd = getCommands('sh').findAll{ | ||
c -> c.contains('curl') | ||
} | ||
assertThat(cmd.size(), equalTo(1)) | ||
assertThat(cmd, hasItem("curl -X PATCH -H 'Accept: application/vnd.github+json' -H 'Authorization: Bearer GIT_TOKEN' https://api.github.com/repos/Codertocat/Hello-World/releases/17372790 -d '{\"tag_name\":\"1.0.0\",\"draft\":false,\"prerelease\":false}'")) | ||
|
||
} | ||
|
||
@Test | ||
void 'use tag as trigger'(){ | ||
runScript("tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggersTag_Jenkinsfile") | ||
def cmd = getCommands('GenericTrigger').findAll{ | ||
c -> c.contains('generic') | ||
} | ||
assertThat(cmd.size(), equalTo(1)) | ||
assertThat(cmd, hasItem('{genericVariables=[{key=ref, value=.ref}, {key=isDraft, value=$.release.draft}, {key=release_url, value=$.release.url}], tokenCredentialId=opensearch-ci-webhook-trigger-token, causeString=A tag was cut on opensearch-ci repo, printContributedVariables=false, printPostContent=false, regexpFilterText=$ref, regexpFilterExpression=^refs/tags/.*}')) | ||
} | ||
|
||
@Test | ||
void 'validate release is not published'(){ | ||
runScript("tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggersTag_Jenkinsfile") | ||
def cmd = getCommands('sh').findAll{ | ||
c -> c.contains('curl') | ||
} | ||
assertThat(cmd.size(), equalTo(0)) | ||
} | ||
|
||
def getCommands(String method) { | ||
def echoCommands = helper.callStack.findAll { call -> | ||
call.methodName == method | ||
}.collect { call -> | ||
callArgsToString(call) | ||
} | ||
return echoCommands | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggersTag_Jenkinsfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
standardReleasePipelineWithGenericTrigger(jsonValue: '.ref', | ||
tokenIdCredential: 'opensearch-ci-webhook-trigger-token', | ||
causeString: 'A tag was cut on opensearch-ci repo', | ||
regexpFilterText: '$ref', | ||
regexpFilterExpression: '^refs/tags/.*') | ||
{ | ||
fakePublishToNpm( | ||
tag: "${tag}" | ||
) | ||
} | ||
|
||
def fakePublishToNpm(Map args) { | ||
echo "fakePublishToNpm ${args}" | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggersTag_Jenkinsfile.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
StandardReleasePipelineWithGenericTriggersTag_Jenkinsfile.run() | ||
StandardReleasePipelineWithGenericTriggersTag_Jenkinsfile.standardReleasePipelineWithGenericTrigger({jsonValue=.ref, tokenIdCredential=opensearch-ci-webhook-trigger-token, causeString=A tag was cut on opensearch-ci repo, regexpFilterText=$ref, regexpFilterExpression=^refs/tags/.*}, groovy.lang.Closure) | ||
standardReleasePipelineWithGenericTrigger.pipeline(groovy.lang.Closure) | ||
standardReleasePipelineWithGenericTrigger.timeout({time=1, unit=HOURS}) | ||
standardReleasePipelineWithGenericTrigger.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]]) | ||
standardReleasePipelineWithGenericTrigger.GenericTrigger({genericVariables=[{key=ref, value=.ref}, {key=isDraft, value=$.release.draft}, {key=release_url, value=$.release.url}], tokenCredentialId=opensearch-ci-webhook-trigger-token, causeString=A tag was cut on opensearch-ci repo, printContributedVariables=false, printPostContent=false, regexpFilterText=$ref, regexpFilterExpression=^refs/tags/.*}) | ||
standardReleasePipelineWithGenericTrigger.stage(Release, groovy.lang.Closure) | ||
standardReleasePipelineWithGenericTrigger.script(groovy.lang.Closure) | ||
StandardReleasePipelineWithGenericTriggersTag_Jenkinsfile.echo(fakePublishToNpm [tag:1.0.0]) | ||
standardReleasePipelineWithGenericTrigger.script(groovy.lang.Closure) | ||
standardReleasePipelineWithGenericTrigger.postCleanup() | ||
postCleanup.cleanWs({disableDeferredWipeout=true, deleteDirs=true}) | ||
standardReleasePipelineWithGenericTrigger.script(groovy.lang.Closure) |
15 changes: 15 additions & 0 deletions
15
tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggers_Jenkinsfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
standardReleasePipelineWithGenericTrigger(overrideAgent: 'AL2-X64', | ||
overrideDockerImage: 'centos:7', | ||
tokenIdCredential: 'opensearch-ci-webhook-trigger-token', | ||
causeString: 'A tag was cut on opensearch-ci repo', | ||
publishRelease: true) | ||
{ | ||
fakePublishToMaven( | ||
mavenArtifactsPath: "/maven", | ||
autoPublish: true | ||
) | ||
} | ||
|
||
def fakePublishToMaven(Map args) { | ||
echo "fakePublishToMaven ${args}" | ||
} |
16 changes: 16 additions & 0 deletions
16
tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggers_Jenkinsfile.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
StandardReleasePipelineWithGenericTriggers_Jenkinsfile.run() | ||
StandardReleasePipelineWithGenericTriggers_Jenkinsfile.standardReleasePipelineWithGenericTrigger({overrideAgent=AL2-X64, overrideDockerImage=centos:7, tokenIdCredential=opensearch-ci-webhook-trigger-token, causeString=A tag was cut on opensearch-ci repo, publishRelease=true}, groovy.lang.Closure) | ||
standardReleasePipelineWithGenericTrigger.pipeline(groovy.lang.Closure) | ||
standardReleasePipelineWithGenericTrigger.timeout({time=1, unit=HOURS}) | ||
standardReleasePipelineWithGenericTrigger.echo(Executing on agent [docker:[image:centos:7, reuseNode:false, stages:[:], args:, alwaysPull:true, containerPerStageRoot:false, label:AL2-X64]]) | ||
standardReleasePipelineWithGenericTrigger.GenericTrigger({genericVariables=[{key=ref, value=$.release.tag_name}, {key=isDraft, value=$.release.draft}, {key=release_url, value=$.release.url}], tokenCredentialId=opensearch-ci-webhook-trigger-token, causeString=A tag was cut on opensearch-ci repo, printContributedVariables=false, printPostContent=false, regexpFilterText=$isDraft, regexpFilterExpression=true}) | ||
standardReleasePipelineWithGenericTrigger.stage(Release, groovy.lang.Closure) | ||
standardReleasePipelineWithGenericTrigger.script(groovy.lang.Closure) | ||
StandardReleasePipelineWithGenericTriggers_Jenkinsfile.echo(fakePublishToMaven [mavenArtifactsPath:/maven, autoPublish:true]) | ||
standardReleasePipelineWithGenericTrigger.script(groovy.lang.Closure) | ||
standardReleasePipelineWithGenericTrigger.postCleanup() | ||
postCleanup.cleanWs({disableDeferredWipeout=true, deleteDirs=true}) | ||
standardReleasePipelineWithGenericTrigger.script(groovy.lang.Closure) | ||
standardReleasePipelineWithGenericTrigger.string({credentialsId=jenkins-github-bot-token, variable=GIT_TOKEN}) | ||
standardReleasePipelineWithGenericTrigger.withCredentials([GIT_TOKEN], groovy.lang.Closure) | ||
standardReleasePipelineWithGenericTrigger.sh(curl -X PATCH -H 'Accept: application/vnd.github+json' -H 'Authorization: Bearer GIT_TOKEN' https://api.github.com/repos/Codertocat/Hello-World/releases/17372790 -d '{"tag_name":"1.0.0","draft":false,"prerelease":false}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* 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 including generic triggers. A tag or a draft release can be used as a trigger using this library. The defaults are all set to trigger via a draft release. If the release is successful, the release can be published by using right params. | ||
@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.tokenIdCredential <Required> - Credential id containing token for trigger authentication | ||
@param args.overrideAgent <Optional> - Jenkins agent label to override the default ('Jenkins-Agent-AL2-X64-C54xlarge-Docker-Host') | ||
@param args.overrideDockerImage <Optional> - Docker image to override the default ('opensearchstaging/ci-runner:release-centos7-clients-v1') | ||
@param args.jsonValue <Optional> - Json value retrieved from payload of the webhook. Defaults to '$.release.tag_name' | ||
@param args.causeString <Optional> - String mentioning why the workflow was triggered. Defaults to 'A tag was cut on GitHub repository causing this workflow to run' | ||
@param args.regexpFilterText <Optional> - Variable to apply regular expression on. Defaults to '$isDraft' | ||
@param.regexpFilterExpression <Optional> - Regular expression to test on the evaluated text specified. Defaults to '' | ||
@param.publishRelease <Optional> - If set to true the release that triggered the job will be published on GitHub. | ||
*/ | ||
|
||
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') | ||
} | ||
triggers { | ||
GenericTrigger( | ||
genericVariables: [ | ||
[key: 'ref', value: (args.jsonValue ?: '$.release.tag_name')], | ||
[key: 'isDraft', value: '$.release.draft'], | ||
[key: 'release_url', value: '$.release.url'] | ||
], | ||
tokenCredentialId: args.tokenIdCredential, | ||
causeString: args.causeString ?: 'A tag was cut on GitHub repository causing this workflow to run', | ||
printContributedVariables: false, | ||
printPostContent: false, | ||
regexpFilterText: (args.regexpFilterText ?: '$isDraft'), | ||
regexpFilterExpression: (args.regexpFilterExpression ?: 'true') | ||
) | ||
} | ||
environment { | ||
tag = "$ref" | ||
} | ||
stages{ | ||
stage("Release") { | ||
steps { | ||
script { | ||
body() | ||
} | ||
} | ||
} | ||
} | ||
post { | ||
success { | ||
script { | ||
if (args.publishRelease && release_url!= null){ | ||
withCredentials([string(credentialsId: 'jenkins-github-bot-token', variable: 'GIT_TOKEN')]){ | ||
sh "curl -X PATCH -H 'Accept: application/vnd.github+json' -H 'Authorization: Bearer ${GIT_TOKEN}' ${release_url} -d '{\"tag_name\":\"${tag}\",\"draft\":false,\"prerelease\":false}'" | ||
} | ||
} | ||
} | ||
} | ||
always { | ||
script { | ||
postCleanup() | ||
} | ||
} | ||
} | ||
} | ||
} |