This repository has been archived by the owner on Oct 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add releaseNotification step to simplify agent release pipelines (#976)…
… (#981) * Add releaseNotification step to simplify agent release pipelines * REmove imports * update README docs Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> (cherry picked from commit 6e3bb9a) Co-authored-by: Victor Martinez <victormartinezrubio@gmail.com>
- Loading branch information
1 parent
6d152c7
commit 465d30b
Showing
4 changed files
with
178 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// 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 org.junit.Assert.assertNull | ||
import static org.junit.Assert.assertTrue | ||
|
||
class ReleaseNotificationStepTests extends ApmBasePipelineTest { | ||
|
||
@Override | ||
@Before | ||
void setUp() throws Exception { | ||
super.setUp() | ||
script = loadScript('vars/releaseNotification.groovy') | ||
} | ||
|
||
@Test | ||
void test_without_arguments() throws Exception { | ||
script.call() | ||
printCallStack() | ||
assertTrue(assertMethodCallOccurrences('emailext', 1)) | ||
assertTrue(assertMethodCallOccurrences('slackSend', 0)) | ||
} | ||
|
||
@Test | ||
void test_without_arguments_and_env_variables() throws Exception { | ||
addEnvVar('SLACK_CHANNEL', '#foo') | ||
addEnvVar('NOTIFY_TO', 'to@acme.com') | ||
script.call() | ||
printCallStack() | ||
assertTrue(assertMethodCallContainsPattern('emailext', 'to@acme.com')) | ||
assertTrue(assertMethodCallContainsPattern('slackSend', '#foo')) | ||
} | ||
|
||
@Test | ||
void test_without_arguments_and_env_variables_all_parameters() throws Exception { | ||
addEnvVar('SLACK_CHANNEL', '#foo') | ||
addEnvVar('NOTIFY_TO', 'to@acme.com') | ||
script.call(slackColor: 'good', | ||
slackCredentialsId: 'credentials', | ||
slackChannel: '#channel', | ||
to: "to@foo.com", | ||
subject: "[example] Release tag *0.1.1* has been created", | ||
body: "Build: (<http://foo.bar|here>) for further details.") | ||
printCallStack() | ||
assertTrue(assertMethodCallContainsPattern('emailext', 'to@foo.com')) | ||
assertTrue(assertMethodCallContainsPattern('slackSend', '#channel')) | ||
assertTrue(assertMethodCallContainsPattern('slackSend', 'good')) | ||
} | ||
|
||
@Test | ||
void test_transformSlackURLFormatToEmailFormat_null() throws Exception { | ||
def ret = script.transformSlackURLFormatToEmailFormat(null) | ||
printCallStack() | ||
assertNull(ret) | ||
} | ||
|
||
@Test | ||
void test_transformSlackURLFormatToEmailFormat_empty() throws Exception { | ||
def ret = script.transformSlackURLFormatToEmailFormat('') | ||
printCallStack() | ||
assertTrue(ret.equals('')) | ||
} | ||
|
||
@Test | ||
void test_transformSlackURLFormatToEmailFormat_foo() throws Exception { | ||
def ret = script.transformSlackURLFormatToEmailFormat('foo') | ||
printCallStack() | ||
assertTrue(ret.equals('foo')) | ||
} | ||
|
||
@Test | ||
void test_transformSlackURLFormatToEmailFormat_url() throws Exception { | ||
def ret = script.transformSlackURLFormatToEmailFormat('(<URL|description>)') | ||
printCallStack() | ||
assertTrue(ret.equals('URL')) | ||
} | ||
} |
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,51 @@ | ||
// 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. | ||
|
||
/** | ||
Send notifications with the release status by email and slack | ||
releaseNotification(slackColor: 'good', | ||
subject: "[${env.REPO}] Release tag *${env.TAG_NAME}* has been created", | ||
body: "Build: (<${env.RUN_DISPLAY_URL}|here>) for further details.") | ||
*/ | ||
def call(Map args = [:]) { | ||
def slackChannel = args.get('slackChannel', env.SLACK_CHANNEL) | ||
def slackColor = args.get('slackColor') | ||
def credentialsId = args.get('slackCredentialsId', 'jenkins-slack-integration-token') | ||
def to = args.get('to', env.NOTIFY_TO) | ||
def subject = args.get('subject', '') | ||
def body = args.get('body', '') | ||
|
||
if (slackChannel?.trim()) { | ||
slackSend(channel: slackChannel, | ||
color: slackColor, | ||
message: "${subject}. ${body}", | ||
tokenCredentialId: credentialsId) | ||
} else { | ||
log(level: 'INFO', text: 'releaseNotification: missing slackChannel therefore skipped the slack notification.') | ||
} | ||
|
||
emailext(subject: subject, | ||
to: to, | ||
body: transformSlackURLFormatToEmailFormat(body)) | ||
} | ||
|
||
def transformSlackURLFormatToEmailFormat(String message) { | ||
// transform slack URL format '(<URL|description>)' to 'URL'. | ||
return message?.replaceAll('\\(<', '')?.replaceAll('\\|.*>\\)', '') | ||
} |
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 @@ | ||
Send notifications with the release status by email and slack. | ||
|
||
If body is slack format based then it will be transformed to the email format | ||
|
||
``` | ||
releaseNotification(slackColor: 'good', | ||
subject: "[${env.REPO}] Release tag *${env.TAG_NAME}* has been created", | ||
body: "Build: (<${env.RUN_DISPLAY_URL}|here>) for further details.") | ||
``` | ||
|
||
* body: this is the body email that will be also added to the subject when using slack notifications. Optional | ||
* slackChannel: the slack channel, multiple channels may be provided as a comma, semicolon, or space delimited string. Default `env.SLACK_CHANNEL` | ||
* slackColor: an optional value that can either be one of good, warning, danger, or any hex color code (eg. #439FE0) | ||
* slackCredentialsId: the slack credentialsId. Default 'jenkins-slack-integration-token' | ||
* subject: this is subject email that will be also aggregated to the body when using slack notifications. Optional | ||
* to: who should receive an email. Default `env.NOTIFY_TO` |