How to extract info from Jenkins console logs during execution to use later in same build execution? - jenkins

I'm trying to implement polling of BrowserStack from Jenkins in order to keep the build execution open: https://www.browserstack.com/docs/automate/cypress/polling-callback
During the build execution the console logs print the BrowserStack Build ID, and then the tests start:
12:27:38 Visit the Automate dashboard for test reporting: https://automate.browserstack.com/dashboard/v2/builds/abc123
I need to extract this Build ID 'abc123' to use later.
How can I extract this info, set it as a variable and use again in the shell command?
Use it like so: $ browserstack-cypress build-info <buildId>
https://www.browserstack.com/docs/automate/cypress/cli-reference#get-the-build-information

You can use the callback_url key in run_settings option in browserstack.json to get an update after that build is done running. The payload POSTed to this callback URL is the same as the one you get with the build-info BUILD_ID command.
Protip: You can keep polling the build status using the build-info command in a loop to monitor the build status, and then close the Local connection, and fail the build if the tests failed.

The callback URL needs to be the URL on which the status of the build will be posted after the build is completed. You can generate a callback URL from 'https://webhook.site/' for testing. You will also get the build ID in the Payload.

Related

How to view Jenkins console output during a build from the terminal after invoking build using curl from the terminal?

I have built a Jenkins job to run automated ZAP-Proxy scans.
I used curl -X POST -u YOUR_USER:YOUR_USER_PASSWORD http://YOUR_JENKINS_URL/job/YOUR_JOB to build the job from the terminal. Is there a way to display the console output in the terminal while the job is building?
It is possible, but it's a little more complicated for two reasons:
When you trigger a new build via curl, then the build will not start immediately. The build will enter the build queue, and it will only start executing once Jenkins found a suitable executor. Before that time, there is no build URL at all.
Technically, the (continuous) console output is delivered in multiple fragments that must be retrieved individually via HTTP.
So, once you triggered the build, you need to find its URL after if left the build queue. It can be done nicely in Groovy -- as a simpler heuristic, you could just wait for certain time and then use the lastBuild reference.
For fetching the console log fragments, you'll use the <buildUrl>/logText/progressiveText end point. Create a loop fetching that URL and checking the X_More_Data and X_Text_Size HTTP headers for information on whether (and what) console output is available. You can do that in bash with curl in a loop; I found this Groovy example on the Web.
In the end, the most elegant solution is probably to
trigger a new build by submitting a Groovy script that will trigger the build and then waits for the build to leave the build queue, returning the build URL.
Then use another script that will poll/fetch/display that build's console output.
If you use the CLI interface for submitting the script, then you can do all those steps in a single script. If you use the REST API, then second part ("continuous output") probably won't work due to output buffering on REST API side.

Jenkins-cli's set-build-parameter command not working anymore, how to modify the value of a build parameter across build steps?

In our Jenkins job, we need to sent an email containing a url in a post-build step (we also post the url to a Slack channel, but that's just about the same thing). The value of the url is dependent on the result of the build. Modification to an environment variable doesn't persist across build step. Writing the url to a file in one step and reading it in another is not an option either since the email/slack plugin has no ability of shell script execution. As a result, we've been using build parameter to solve this issue and the set-build-parameter command of jenkins-cli works fine.
After a recent upgrade of Jenkins (we're now at 2.75), set-build-parameter fails to work anymore. It reports the following error:
ERROR: Failed to identify the build being executed
According to Jenkins-cli's help command, set-build-parameter seems to be deprecated:
set-build-parameter
Update/set the build parameter of the current build in progress. [deprecated]
So, what's the correct way to modify the value of a build parameter across build steps in the latest version of Jenkins?
I frequently manipulate build parameters in shell script blocks, you have to edit them, save them in a file f. e.:
buildParamter=true>variable.txt
Now you can use the envInject Plugin and reference the the newly created file.

How can I add a Jenkins post action to execute two different scripts once the build successed or failed exclusively?

I want to execute a shell scripts if the Jenkins job build successful or another scripts if the Jenkins job build failed.
I added the post build task plugin, but it seems only can execute a shell in all status or just successful status, cannot specify another shell script to be run once build failed, the two scripts should be run exclusively.
are there anyone can help me on this?
use Post Build Task plugin for conditional steps
I use Conditional BuildStep Plugin and Text Finder Run Condition Plugin to execute steps when the build fails.
But there's a limitation with this method: You have to find a text that is displayed ONLY when your build fails.
See the pictures below:
I add exit 0 at the end of the script to force the build to be successful so that the program can execute the next step, which is Conditional steps(multiple).
You will see Execute script after a failed build. being displayed on console output when the job fails.
This is the execution result.
You will also need to find a text that is displayed ONLY when the first step succeeds so that you can trigger another script.

Access Jenkins build log within build script

How would you go about accessing contents of the build (console) log from within a running build script?
I have a deploy script that runs, logs into a series of servers and runs scripts on those servers. I need to obtain certain output from some of those remote scripts and use them later in the build process and also in the completion email.
You can do something like this in the Post Build section, but I don't think you can do it earlier in the job. With the groovy post build plugin you can get information from the console log:
if(manager.logContains("text to find")) {
do something
}

Run Nant script as a Jenkins post build action

Is it possible to run a Nant script as a Jenkins post build action?
I get the option to run a script as a Build Step but not as a build action. Is there any particular plugin which enables this option.
The reason I am looking for this functionality is that I need to run a script which depends on the ArtifactDeployer post build action. If i specify the code in the build step it gets executed before the ArtifactDeployer and the build fails
You can use the Post Build Task Plugin
edit
One way of getting the build number if it's not working with this plugin is using the Groovy Post Build Plugin
With it you can execute groovy code as a post build action, get the build number and execute NAnt
the build number is accessible from the following property
manager.build.number
Post-build Actions -> Execute a set of script run after a build when it succeeds or fails. My experience shows that it only sometimes runs when a build is aborted.
As advised above, the Post-build Actions -> Post build task (via the named post task plug in) is always evaluated for run (regardless of the build exit status). Additional setting via phrase in the log ("Build was aborted") works reliable for me.
My problem was to run something even on an aborted build and post build task sorted out this problem.

Resources