Skip to content

Commit

Permalink
JENKINS-49997 - Added Unsuccessful build condition for _post_
Browse files Browse the repository at this point in the history
  • Loading branch information
joseblas committed Nov 28, 2018
1 parent c1c8619 commit 2861100
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkinsci.plugins.pipeline.modeldefinition.model.conditions

import hudson.Extension
import hudson.model.Result
import org.jenkinsci.Symbol
import org.jenkinsci.plugins.pipeline.modeldefinition.model.BuildCondition
import org.jenkinsci.plugins.workflow.job.WorkflowRun

import javax.annotation.Nonnull

/**
* A {@link BuildCondition} for matching unstable builds.
*
* @author Andrew Bayer
*/
@Extension(ordinal=650d) @Symbol("unsuccessful")
class Unsuccessful extends BuildCondition {
@Deprecated
@Override
boolean meetsCondition(@Nonnull WorkflowRun r) {
return meetsCondition(r, null, null)
}

@Override
boolean meetsCondition(@Nonnull WorkflowRun r, Object context, Throwable error) {
return isNotNull(r) || ! is(r, Result.SUCCESS)
}

private boolean isNotNull(WorkflowRun r){
return r.getResult() != null && getExecutionResult(r) != null
}

private boolean is(WorkflowRun r, Result res){
Result execResult = getExecutionResult(r)
return execResult == res || r.getResult() == res
}

@Override
String getDescription() {
return Messages.Unsuccessful_Description()
}

static final long serialVersionUID = 1L
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ NotBuilt.Description=Run if the build status is "Not Built"
Success.Description=Run if the build status is "Success" or hasn\'t been set yet
Unstable.Description=Run if the build status is "Unstable"
Regression.Description=Run if the current build\'s status is worse than the previous build\'s status
Unsuccessful.Description=Run if the current build\'s status is Aborted, Failure or unstable
Cleanup.Description=Always run after all other conditions, regardless of build status
Fixed.Description=Run if the previous build was not successful and the current build\'s status is "Success"
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,53 @@ public void withAllLocalUnstable() throws Exception {

}

@Test
public void withAllLocalUnsuccessfulWithUnstable() throws Exception {
env(s).put("MAKE_RESULT", Result.UNSTABLE.toString()).set();
expect(Result.UNSTABLE, "unsuccessful")
.logContains("I LOVE YOU VIRGINIA")
.logContains("I FAILED YOU, SORRY")
.logContains("I AM UNSTABLE")
.go();

}

@Test
public void withAllLocalUnsuccessfulWithAborted() throws Exception {
env(s).put("MAKE_RESULT", Result.ABORTED.toString()).set();
expect(Result.ABORTED, "unsuccessful")
.logContains("I LOVE YOU VIRGINIA")
.logContains("I FAILED YOU, SORRY")
.go();

}
@Test
public void withAllLocalUnsuccessfulWithFailure() throws Exception {
env(s).put("MAKE_RESULT", Result.FAILURE.toString()).set();
expect(Result.FAILURE, "unsuccessful")
.logContains("I LOVE YOU VIRGINIA")
.logContains("I FAILED YOU, SORRY")
.go();

}

@Test
public void withAllLocalUnsuccessfulWithSuccess() throws Exception {
env(s).put("MAKE_RESULT", Result.SUCCESS.toString()).set();
expect(Result.SUCCESS, "unsuccessful")
.logContains("I LOVE YOU VIRGINIA")
.go();

}
@Test
public void withAllLocalUnsuccessfulWithNorBuilt() throws Exception {
env(s).put("MAKE_RESULT", Result.NOT_BUILT.toString()).set();
expect(Result.NOT_BUILT, "unsuccessful")
.logContains("I LOVE YOU VIRGINIA")
.go();

}

@Test
public void withAllLocalFailure() throws Exception {
env(s).put("MAKE_RESULT", Result.FAILURE.toString()).set();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

pipeline {
agent {
label "here"
}
stages {
stage("foo") {
steps {
echo "hello"
script {
String res = env.MAKE_RESULT
if (res != null) {
echo "Setting build result ${res}"
currentBuild.result = res
// JENKINS-52114 - can't tell the difference between setting currentBuild.result in this stage
// and an error in a parallel stage, so let's error.
if (res == "FAILURE") {
error "Failing explicitly"
}
} else {
echo "All is well"
}
}
}
post {
unsuccessful {
echo "I FAILED YOU, SORRY"
}
unstable {
echo "I AM UNSTABLE"
}
always {
echo "I LOVE YOU VIRGINIA"
}
}
}
}
post {
always {
echo "And AAAAIIIAAAIAI"
}
}
}

0 comments on commit 2861100

Please sign in to comment.