首页 > 解决方案 > 由于收件人列表为空,Jenkins Pipelines 定期构建不发送电子邮件通知

问题描述

我们使用每天定期运行的简单管道作业设置了夜间构建,但开发人员没有收到电子邮件通知。

我们使用emailext 插件将这些电子邮件和Kubernetes 代理作为节点发送。

该作业由计时器启动,因为它是定期构建,使其运行以下管道配置(您可以忽略代理的容器定义,因为它不相关,IMO):

pipeline {
  agent {
    kubernetes {
      yaml """
metadata:
  labels:
    some-label: some-label-value
spec:
  containers:
    - name: agent
      image: python:3.7
      command:
        - cat
      tty: true
"""
    }
  }
  options {
    timestamps()
  }
  stages {
    stage('SCM') {
      steps {
        checkout(
          changelog: false,
          poll: false,
          scm: [$class                           : 'GitSCM',
                userRemoteConfigs                : [[credentialsId: 'Git SSH Key', url: 'git@bitbucket.org:company/repository.git']]],
                branches                         : [[name: 'master']],
                doGenerateSubmoduleConfigurations: false,
                extensions                       : [[$class   : 'CloneOption',
                                                     depth    : 1,
                                                     noTags   : false,
                                                     reference: '',
                                                     shallow  : true],
                                                    [$class: 'PruneStaleBranch'],
                                                    [$class: 'GitLFSPull'],
                                                    [$class             : 'SubmoduleOption',
                                                     disableSubmodules  : false,
                                                     parentCredentials  : true,
                                                     recursiveSubmodules: true,
                                                     reference          : '',
                                                     trackingSubmodules : false]],
                submoduleCfg                     : []
        )
      }
    }
    stage('Build') {
      steps {
        container('agent') {
          echo '-> install tox'
          sh 'pip install tox'

          sh 'python --version'
          sh 'pip --version'
        }
      }
    }
    stage('Test') {
      steps {
        container('agent') {
          sh 'tox -c ./tox.ini'
        }
        post {
          always {
            echo '-> collecting artifacts'
            archiveArtifacts allowEmptyArchive: true, artifacts: '*.txt'

            echo '-> collecting test results'
            junit allowEmptyResults: true, testResults: 'output/pytest.xml'
          }
        }
      }
    }
  }
  post {
    changed {
      emailext(
        subject: '$DEFAULT_SUBJECT',
        body: '$DEFAULT_CONTENT',
        recipientProviders: [culprits(),
                             developers(),
                             requestor(),
                             brokenBuildSuspects(),
                             brokenTestsSuspects(),
                             upstreamDevelopers()]
      )
    }
  }
}

当手动启动作业(启动开发人员收到相关电子邮件)时,上述内容确实有效,但是,当从定期构建(cron)触发作业时 - 收件人列表始终为空:

尝试向空的收件人列表发送电子邮件,被忽略。

可能是什么问题?

标签: jenkinsjenkins-pipelineemail-ext

解决方案


另一种方法是使用 git 获取提交者:

// Get all commits from the latest merge in an array 
def gitCommits = sh(returnStdout: true, script: 'git log --merges -1 --format=%p').trim().split(' ')

// Get committer emails:
def emails = ""
gitCommits.each {
 emails = emails + sh(returnStdout: true, script: 'git --no-pager show -s --format=%ae ${it}').trim() + ","
}
echo "${emails}"

推荐阅读