Capture output of tests in gradle - gradle

How do I capture the standard output and standard error from all test tasks in gradle? For exec tasks, I can just say standardOutput = new ByteAr... and it works - but test tasks don't seem to have a standard out.
What I'm looking to achieve is simple. (Actually, I want this for ALL gradle tasks, but test tasks are most important).
If the task succeeds...
I get no output at all
If the task fails
I get the most detailed output possible.
Simple as that. How do I achieve this?

Related

How to determine what gradle command was running

I want to measure time of execution for different gradle command so I have to know what exactly command was run.
For example when I execute from terminal
./gradlew clean
If I execute
./gradlew assembleDebug
desired result is "assembleDebug"
I know I can get list of tasks with project.tasks, but it will give me only list of tasks which my command will execute, without command that started them all.
Is there any option to determine that "first" command?
I figured out how that should work
project.gradle.startParameter.taskNames
gives list of task names. For example if I run multiple commands like
./gradlew app:clean app:cleanBuildCache
it will contain both commands.
Is is possible to cast it for string directly
project.gradle.startParameter.taskNames.toString()
So I will have
[app:clean, app:cleanBuildCache]
With single command like
./gradlew app:clean
I receive
[app:clean]
and that was what I searched for

Choice paramter in jenkins as an output from shell script (pipeline)

I'm looking for a way to use an output from shell as a jenkins parameter but in pipeline, don't want to use any of UI plugins.
For example I want to use output from command
ls /home
as a choice parameter (so I would be able to choose users from the list), is it possible to do something like that?
It must be done in pipeline, I'm not looking for some plugins which allow you to do something like that, but you need to do all in UI, if plugin support pipelines then it's also ok.
For a pipeline to run, its parameters need to be completely defined before the pipeline is started.
For the pipeline to parse the output of ls /home, it needs to run and allocate a node and a workspace, which can only happen after the pipeline is started.
So you are in a kind of a chicken-and-egg problem, where you need to run some code before you start a pipeline, but you can't run pipeline before you run that code.
So your issue boils down to "How can I run some arbitrary Groovy script before I start my pipeline?"
There are two options for this:
ActiveChoice plugin allows you to define a parameter that returns a script. Jenkins will then run the script (don't forget to approve it) in order to show you your "Build with parameters" page. Debugging this is notoriously hard, but this can go to great lengths.
Alternatively, you may want to run a scripted pipeline before you run the Declarative (main) one, as outlined e.g. in this answer. This may look a bit like this:
def my_choices_list = []
node('master') {
stage('prepare choices') {
// read the folder contents
def my_choices = sh script: "ls -l /home", returnStdout:true
// make a list out of it - I haven't tested this!
my_choices_list = my_choices.trim().split("\n")
}
}
pipeline {
parameters {
choiceParam('OPTION', my_choices_list)

Do not trigger Jenkins Downstream projects if matches a particular string

In a Jenkins job I am running a shell script, on success of which
triggering a downstream project in Postbuild.
I have a problem because my shell scripts gets successful in two
scenarios. For Ex let us consider scenario-A and scenario-B.
If script is successful with scenario-A then it should trigger the
downstream project, but if script is successful with scenario-B then
the job should end and should not trigger downstream script.
tried to match Text and use Text Finder plugin, but it makes build
unstable. I don't want the job status to be Unstable or Failed when
scenario-B passes. I am able to successfully match the strings using
few scripting in Execute shell script plugin, but what should I give
to finish the Jenkins job with success status and avoiding the
downstream project when the string matches.
Execute Shell Plugin Contains
cd dir
./myscript
string_name=`cat aaa.log | grep foo`
if [ string_name == "foo" ] then;
\\Command to aviod downstream project
fi
Sounds to me that you'd be better off trying to implement this logic via Build Flows https://wiki.jenkins.io/display/JENKINS/Build+Flow+Plugin?focusedCommentId=60917290 or Pipeline 2.0 https://jenkins.io/doc/book/pipeline/
Build flows is probably closer to what you already have right now (and in itself is kind of a bridge in between traditional jobs and Pipeline 2.0).
Your logic would be sth like (groovy code inside a Build Flow or a Pipeline 2.0 Jenkinsfile):
if (build('scenario-B-Job'))
return
else if build('scenario-A-Job') {
build('downstream-Job')
}
Not sure I get your logic exactly right (you don't mention whether A and B are mutually exclusive or if they can/must run in parallel) but I think you get the idea.

Transform standard output from Gradle's Exec task on the fly?

Is it possible to transform the standard output logs that a Gradle Exec task produces, on the fly?
We have a task executing a command line tool that takes about a minute to run which logs a lot. I would like to filter out just a few of those lines and log them to show progress without cluttering the build log.
You could set your own OutputStream implementation with Exec.setStandardOutput. Or you could execute a shell command that runs the tool and filters its output.

Gradle exec task running shell script distracts asking for input

So I'm trying to get input from the user, specifically their password before we run some commands through sudo. While using gradle to execute the shell script it prints out to the console something like
$ > Building > :gradletaskname
But when it asks for a password or other input it is not intuitive because the way gradle writes to the console it looks like the following.
$ > Building > :gradletasknamePassword:
I have tried echoing new lines but it always displays like my example above. I know this might be a silly question but I've been beating my head on this for a while now.
It's a known limitation and will be fixed in one of the next releases. Here's a link to the JIRA issue: http://issues.gradle.org/browse/GRADLE-1147

Resources