I have a groovy script that changes the build result using setResult(hudson.model.Result.SUCCESS).
But I realized that I cannot change the job result to a better result (only to worse ones). If I will change the code to: build.setResult(hudson.model.Result.Unstable), then when the build will be successful the result will be changed (I can see in the Console Output: Build step 'Groovy Postbuild' changed build result to UNSTABLE.)
But I can't update the result to a better one.
Is there any solution?
(The same problem occurs with groovy postbuild) .
EDIT:
I'm using the MultiJob plugin in my main job for running 3 downstream jobs (named job1, job2, job3). And I wrote a groovy script so that the result of the main job will be determine only by the first two downstream jobs (when job1 and job2 are success, and job3 is unstable - I wish to set the main job result to success).
because of the problem mentioned above I can't do it... any ideas?
Thanks.
I believe that this expected behavior with Jenkins. Other methods of changing the build result (such as the Fail The Build plugin) also cannot "improve" the build status, they can only make it worse (success to unstable to failed).
Using Post Build plugin and Groovy System Script, you can change build result with Result.fromString() , for example, setting result to "Unstable":
build.result = hudson.model.Result.fromString('UNSTABLE')
In the Console you'll see:
[PostBuildScript] - Execution post build scripts.
[Current build status] check if current [ABORTED] is worse or equals then [ABORTED] and better or equals then [UNSTABLE]
Run condition [Current build status] enabling perform for step [Execute system Groovy script]
Script returned: UNSTABLE
Related
I need to exclude a job from pipeline in case my project version is pre-release.
How I know it's a pre-release?
I set the following in the version info file, that all project files and tools use:
version = "1.2.3-pre"
From CI script, I parse the file, extract the version value, and know whether it's a pre-release or not, and can set the result in an environment variable.
The only way I know to exclude a job from pipeline is to use rules, while, I know also from gitlab docs that:
rules are evaluated before any jobs run
before_script also is claimed to be called with the script, i.e. after applying the rules.
I can stop the job, only after it starts from the script itself, based on the version value, but what I need is to extract the job from the pipeline in the first place, so it's not displayed in the pipeline history. Any idea?
Thanks
How do you run (start) your pipeline, and is the information whether "it's a pre-release" already known at this point?
If yes, then you could add a flag like IS_PRERELEASE as a variable to the pipeline, and use that in the rules: section of your job. The drawback is that this will not work with automatic pipelines (triggered by a commit or MR); but you can use this approach with manually triggered pipelines (https://docs.gitlab.com/ee/ci/variables/#override-a-variable-when-running-a-pipeline-manually) or via the API (https://docs.gitlab.com/ee/ci/triggers/#pass-cicd-variables-in-the-api-call).
Is that possible to add conditionals that executes if or else block based on a user input in GitLab CI yml?
In the event the user input is a YES then it must trigger a template(ansible tower template) to restart specific service (tower-cli job launch --job-template)
Should I userules:if with when for such a conditional.Could someone put an insight on such a format.I am a first time user in Gitlab and have some experience in Jenkins.
The rules section (and also only and except) are evaluated when the pipeline is first created, but before the first job starts running because it controls which jobs will be in a pipeline or not, so you can't use variables/conditionals that come from another job or manual task variables.
An alternative is to use a normal bash conditional in your script section and simply exit 0; when the job shouldn't run.
...
Job:
stage: my_stage
when: manual
script:
- if [ $MY_USER_INPUT_VARIABLE == "don't run the job" ] then exit 0; fi
- ./run_job.sh
These are just simple examples, but you check the variable for whatever it is you need it to do (or use multiple conditionals if using multiple variables) and if the job shouldn't run, execute exit 0; which will stop the job from processing, but will not mark it as failed. Otherwise, we run whatever we need this job to do.
I'm new to gitlab CI after years of using Jenkins. I'm trying to convert my old project over to use Gitlab CI but I'm wondering if this is possible?
In Jenkins I have code that gets the number of tests and works out the percentage that passed, if over 90% pass I then set the Jenkins build to be successful, if <=90 I mark the build as failed.
In my Gitlab CI pipeline I can see gitlab already works out the percentage in success rate, is it possible for me to use this to pass or fail the job based on the value?
This should be possible in a similar way to Jenkins. Basically you need to catch output of your test tool and then process it and fail on condition. It is possible with bash, smth like:
- if [ $percentage -lt 90 ];
then
echo "Percentage of passed tests is less than 90";
exit 1;
fi;
I've seen a number of examples that execute a pre build system groovy script to the effect of
import hudson.model.*
def thr = Thread.currentThread()
def build = thr?.executable
printf "Setting SVN_UPSTREAM as "+ build.getEnvVars()['SVN_REVISION'] +"\n" ;
build.addAction(new ParametersAction(new StringParameterValue('SVN_UPSTREAM', build.getEnvVars()['SVN_REVISION'])))
Which is intended to make SVN_UPSTREAM available to all downstream jobs.
With this in mind I attempt to use $SVN_UPSTREAM in a manually executed downstream job like
https://code.mikeyp.com/svn/mikeyp/client/trunk#$SVN_UPSTREAM
Which is not resolved causing an error.
Can anyone spot the problem here?
The bleeding edge jenkins build pipeline plugin now supports parameter passing. Eliminated the need for the groovy workaround for me.
Make sure that the parameter you are passing downstream is not set as a parameter in the downstream job where you wish to use it. That is, in the downstream job, if you have "This build is parametrized" checked, do not add SVN_UPSTREAM to the list of parameters. If you do, it overrides the preset value.
Say I have a maven2/3 project in jenkins/hudson and BEFORE I run some goals on a maven project configured in the correspoing config.xml file, I want to run a system groovy script (ref. system groovy plugin) during a prestep and interrupt the whole job and set it to SUCCESS if some condition is met (for example say I find something in the log file of the previous job). I DO NOT WANT MAVEN TO START EXECUTING THE GOALS.
I have tried
import hudson.model.*
def thr = Thread.currentThread()
def build = thr?.executable
build.executor.interrupt(hudson.model.Result.SUCCESS)
out.print "HELLO"
But nothing happens, and even "HELLO" is printed in log. But then the build gets ABORTED.
Parsing POMs
Discovered a new module ...
Modules changed, recalculating dependency graph
...
...jdk1.6.0_22/bin/java -Xmx512m -cp ...
<===[JENKINS REMOTING CAPACITY]===>Build was aborted
Thanks for your time.
I do not understand fully, what you want, since you described here some solution, not your exact problem. I know three plugins, which could be useful for your problem as well:
Fail the build plugin lets you set the result of the job, and stops further processing. Any status can be set including success.
Conditional build step plugin lets you define conditions for its child build steps. If the condition is met, the child(ren) will run.
m2 extra build steps - lets you run build steps before or after maven build in a maven job in jenkins. Note, that recently, this plugin is the part of the core jenkins.
So the basic idea is that you could add conditional build step a pre-build step in your job, and the child step could be one fail-the build instance. See the picture below: