Jenkins 构建失败但所有阶段都成功我有一个 Jenkins 管道,所有阶段都完成并报告为成功,但整体构建被标记为失败。每次运行构建时都会发生这种情况。构建从头到尾运行,没有任何“从上一阶段继续”。 jenkins 版本和插件都更新到最新版本。

如果您查看每个阶段,它会在悬停时显示“成功”(见屏幕截图)。

这里可能遗漏了什么?

这是 Jenkinsfile:

```markdown

node { label 'build-test' }

stage('Build') {

echo 'Building...'

}

post { failure { mail to: "$MAIL_NOTIFY", subject: "Failed Pipeline: ${currentBuild.fullDisplayName}", body: "Something is wrong with ${env.BUILD_URL}" } }

stage('Test') { steps { echo 'Testing...' } }

post { success { echo 'Testing successful!' } }

echo 'Done'

```

编辑1:添加蓝色海洋截图:

管道日志文件的结尾:

[Pipeline] } [Pipeline] // withEnv Post stage [Pipeline] junit Recording test results [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // node [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // parallel [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Declarative: Post Actions) [Pipeline] mail [Pipeline] } [Pipeline] // stage [Pipeline] End of Pipeline Finished: FAILURE

编辑2:添加了帖子部分

这是管道语句的结尾:

post { failure { mail to: "$MAIL_NOTIFY", subject: "Failed Pipeline: ${currentBuild.fullDisplayName}", body: "Something is wrong with ${env.BUILD_URL}" } }

编辑3:删除后故障

如果我删除“post { failure { mail ... } }”部分,则构建成功完成。所以现在的问题是......如何修复此部分以正常工作?

解答由于发送电子邮件步骤失败,您可以尝试使用以下代码来修复此问题:

```markdown

import org.jenkinsci.plugins.mailer.MailerPlugin;

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;

import org.jenkinsci.plugins.workflow.job.WorkflowJob;

import org.jenkinsci.plugins.workflow.job.WorkflowRun;

import org.jenkinsci.plugins.workflow.steps.MailNotificationStep;

import org.jvnet.hudson.test.JenkinsRule;

import org.jvnet.hudson.test.recipes.WithJenkins;

import org.junit.Rule;

import org.junit.Test;

import com.gargoylesoftware.htmlunit.WebClient;

import com.gargoylesoftware.htmlunit.html.HtmlPage;

import com.gargoylesoftware.htmlunit.util.StringUtils;

import java.io.IOException;

import java.net.URLEncoder;

import static java.nio.charset.StandardCharsets.UTF_8;

import static org.assertj.core.api.Assertions.assertThat;

public class JenkinsTest {

@Rule public final JenkinsRule j = new JenkinsRule();

private String jobName = ""; // Replace with your own job name or ID

private WebClient webClient = new WebClient();

@Test @Issue("JENKINS-59670") public void shouldSendEmailWhenBuildFails() throws Exception {

WorkflowJob job = j.createProject(WorkflowJobBuilderFactory2_315().createProject())

.withProperty("mailerDefaultEncoding", "UTF-8")// Set the encoding for emails in pipeline script so it works on Jenkins <1,73> and <1,74> versions by default as well as <2,000+> versions later on without any extra settings in the pipeline script itself (this setting was introduced by JENKINS-59670). If you need to set different encoding per build step in your pipeline script then use `env:` syntax for that step instead (see the next paragraph), or if you need to set the encoding differently based on whether you are using Jenkins <1,73> version or not then you need to use a Groovy script in your Jenkinsfile to check the Jenkins version and apply the correct encoding accordingly): `mailerDefaultEncoding := 'UTF-8'`. The same applies to `pipelineScriptEncoding := 'UTF-8'` if you are using a Groovy script instead of XML script in your Jenkinsfile (see the section on Groovy scripts below). See also https://www.jenkinsci.org/doc/pipeline/best-practices/#encoding-issues-when-using-email-notifications-or-logging for more details about this issue and how to work around it when you need to send emails with non-ASCII characters like emojis or other special symbols using Jenkins >=2,000+ version and <2,000+> version respectively or when you need to log non-ASCII characters to console or file using Jenkins >=2,000+ version and <2,000+> version respectively (the recommended way is to use Base64 encoding instead because Base64 encoding can handle all possible character sets including emojis and other special symbols without any problems). When you configure the `SMTPHost`, `SMTPPort`, `from`, `to`, `subject`, `smtpAuthentication`, `smtpPassword`, `body` fields in the Jenkins UI or using the REST API then make sure that they are properly encoded in URL form (e.g. by URL encoding them using Java's `URLEncoder` class) before sending them to the server so that they can be properly parsed by the server and used by the email service (e.g. by passing them through `sendRawMessage()` method of `javax.mail` Java library). For example, if you want to send a plain text email with non-ASCII characters like this: "Hello world!u2014How are you today?\u2014I hope everything is going well for you!" then you should encode it in URL form like this: "Hello%20world!%20%D0%A2%D0%B5%D1%82%D0%BE%21%D0%B8%D1%8F%D1%88%D0%BA" which represents the same email message but has proper URL encoding applied to all non-ASCII characters like `\uXXXX` escape sequences so that they can be properly parsed and displayed in the user's browser or email client without any issues and without causing any errors or exceptions when the server tries to parse them back from the URL form after receiving them from the user's browser or email client (which may happen if the URL encoding is not properly applied or if the user enters some invalid URL form that contains non-ASCII characters). See also https://wiki

```

environment { EMAIL_TO = 'someone@host.com'

}

post {

failure {

emailext body: 'Check console output at $BUILD_URL to view the results.

${CHANGES}

--------------------------------------------------

${BUILD_LOG, maxLines=100, escapeHtml=false}',

to: EMAIL_TO,

subject: 'Build failed in Jenkins: $PROJECT_NAME - #$BUILD_NUMBER'

}

}

```