Issue
I want to use jenkins env variable with SH, but inside multiple quote from curl
.
Example:
script {
container('deployer') {
sh "curl -X POST --data-urlencode \"payload={'channel': '#notification-test-devops', 'username': 'Jenkins Build', 'text': '[Blocked Production] ${env.JOB_BASE_NAME} (${env.BRANCH_NAME} #${BUILD_NUMBER}) <${BUILD_URL}|View Build>', 'icon_emoji': ':jenkins:'}\" https://hooks.slack.com/services/XXX/XXX/XXX"
}
}
Current result seems it's not rendered:
[Blocked Staging] ${env.JOB_BASE_NAME} (${env.BRANCH_NAME} #${BUILD_NUMBER}) <${BUILD_URL}|View Build>
Expectation: The jenkins env variable will be rendered in Slack message.
Solution
So after few tries, this is works.
container('deployer') {
wrap([$class: 'BuildUser']) {
sh """
curl -X POST --data-urlencode \"payload={'channel': '#notification-test-devops', 'username': 'Jenkins Build', 'text': \\\"[Blocked Production] ${env.JOB_NAME} #${BUILD_NUMBER}\nCommit ID: ${GIT_COMMIT}\nCommit Message: ${GIT_COMMIT_MSG}\n<${BUILD_URL}|View Build>\\\", 'icon_emoji': ':jenkins:'}\" https://hooks.slack.com/services/XXX/XXX/XXX
"""
}
}
You can ignore the wrapping, because I only use it with build-user-vars-plugin
Answered By - M Tri
Answer Checked By - Mary Flores (JavaFixing Volunteer)