• 清理和通知
    • 电子邮件
    • Hipchat
    • Slack

    清理和通知

    因为 post 部分保证在 Pipeline 结束的时候运行,所以我们可以添加通知或者其他的步骤去完成清理、通知或者其他的 Pipeline 结束任务。

    Jenkinsfile (Declarative Pipeline)

    1. pipeline {
    2. agent any
    3. stages {
    4. stage('No-op') {
    5. steps {
    6. sh 'ls'
    7. }
    8. }
    9. }
    10. post {
    11. always {
    12. echo 'One way or another, I have finished'
    13. deleteDir() /* clean up our workspace */
    14. }
    15. success {
    16. echo 'I succeeeded!'
    17. }
    18. unstable {
    19. echo 'I am unstable :/'
    20. }
    21. failure {
    22. echo 'I failed :('
    23. }
    24. changed {
    25. echo 'Things were different before...'
    26. }
    27. }
    28. }

    Toggle Scripted Pipeline(Advanced)

    Jenkinsfile (Scripted Pipeline)

    1. node {
    2. try {
    3. stage('No-op') {
    4. sh 'ls'
    5. }
    6. }
    7. catch (exc) {
    8. echo 'I failed'
    9. }
    10. finally {
    11. if (currentBuild.result == 'UNSTABLE') {
    12. echo 'I am unstable :/'
    13. } else {
    14. echo 'One way or another, I have finished'
    15. }
    16. }
    17. }

    有很多方法可以发送通知,下面是一些示例展示了如何通过电子邮件、Hipchat room 或者 Slack channel 发送 Pipeline 的相关信息。

    电子邮件

    1. post {
    2. failure {
    3. mail to: 'team@example.com',
    4. subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
    5. body: "Something is wrong with ${env.BUILD_URL}"
    6. }
    7. }

    Hipchat

    1. post {
    2. failure {
    3. hipchatSend message: "Attention @here ${env.JOB_NAME} #${env.BUILD_NUMBER} has failed.",
    4. color: 'RED'
    5. }
    6. }

    Slack

    1. post {
    2. success {
    3. slackSend channel: '#ops-room',
    4. color: 'good',
    5. message: "The pipeline ${currentBuild.fullDisplayName} completed successfully."
    6. }
    7. }

    当 Pipeline 失败、不稳定甚至是成功时,团队都会收到通知,现在我们可以继续完成我们的持续交付 Pipeline 激动人心的部分:部署!

    继续“部署”