Is it possible to use arguments
or abstraction
to minimize repeating code in a YAML file?
I'm writing a YAML file that triggers a deployment and before and after the deployment I would like to make calls to a slack channel indicating the deployment is starting, and finishing, and also if it fails.
Here is what I've written but it feels too verbose:
example_deploy:
- call: notify
in:
msgText: "Deployment starting for environment *${environment}*"
- try:
- ${oneops.environmentCommitAndDeploy(environment = 'production', platform = '${platform}', deployAllPlatforms = false )}
error:
- log: "Error trying to deploy: ${lastError.cause}"
- call: notify
in:
msgText: " :fire: Deployment failed for environment *${environment}* http://concord.com/#/process/${txId}/log"
- exit
- call: notify
in:
msgText: " :party: Deployment succeeded for environment *${environment}* http://concord.com/#/process/${txId}/log"
notify:
- task: slack
in:
channelId: ${alerts}
username: ${slackname}
iconEmoji: ${slackEmojiLooper}
text: "${msgText}"
Now if I want to have example_deploy_2
and do the same type of thing, do I have to rewrite all that code? or is there a way to have a "function" or abstract the repeated parts of the YAML?
UPDATE
I've used call
to abstract the calls to slack, but now I'm wondering if I can have a generic call to slack and dynamically update the message - because now I'm repeating the params I'm passing to the blocks of code I've defined to be call
ed
Example
example_deploy:
- call: slack_start_deploy
- try:
- ${transitionVariableUpdate(platform = '${platform}', environment = '${environment}', component = '${component_ear}' variables = { appVersion = '${BRANCH_NAME}-${BUILD_NUMBER}' })}
- ${environmentCommitAndDeploy(environment = 'qa', platform = '${platform}', deployAllPlatforms = false )}
error:
- log: "Error trying to deploy: ${lastError.cause}"
- call: slack_deploy_error
- exit
- call: slack_deploy_success
slack_start_deploy:
- slack.postMessage:
text: "${entryPoint} Deployment starting for environment *${environment}*"
channelId: ${alerts}
username: ${slackname}
iconEmoji: ${slackEmojiConcord}
slack_deploy_error:
- slack.postMessage:
text: " :fire: ${entryPoint} Deployment failed for environment *${environment}* http://concord.com/#/process/${txId}/log"
channelId: ${alerts}
username: ${slackname}
iconEmoji: ${slackEmojiConcord}
slack_deploy_success:
- slack.postMessage:
text: " :party: Deployment succeeded for environment *${environment}* http://concord.com/#/process/${txId}/log"
channelId: ${alerts}
username: ${slackname}
iconEmoji: ${slackEmojiConcord}