Jenkins Conditional Behavior - jenkins

I have a project that sometimes doesn't link correctly if you try to rebuild it after making updates. After doing a clean build, it works fine. I'd like to replicate this behavior in Jenkins - first try rebuilding, then if that fails, run the clean script and try again, and only if that fails, fail the build.
How do I build in conditionals like this?

You can use a simple try/catch block in your pipeline script:
try {
// build step without clean
}
catch (error) {
// clean step
// build step
}
(but you might want to consider cleaning before every build and/or figuring out why it's failing)

Related

Gradle publishToMavenLocal

When I use gradle clean build publishToMavenLocal with
publishing {
publications {
maven(MavenPublication) {
from components.java
}
}
}
using gradle 7.1.
It works on local gradle build. However, when I run on Jenkins, I get:
org.jenkinsci.plugins.workflow.steps.MissingContextVariableException: Required context class hudson.FilePath is missing
Perhaps you forgot to surround the code with a step that provides this, such as: node
When, I comment out the publishing section in build.gradle it works in Jenkins.
Any ideas?
Thanks
Publishing to Maven local requires disk space, or working space.
Based on: https://support.cloudbees.com/hc/en-us/articles/4402585187483-How-to-troubleshoot-hudson-FilePath-is-missing-in-a-Pipeline-run

gtest dependency for Bazel java_tools build?

I am trying to follow the instructions for contributors here:
https://bazel.build/contributing.html
I have a successful build off of master (i.e. bazel build //src:bazel), but the doc suggests also "you might want to build the various tools Bazel uses." I am trying to do that, for example:
cd src/java_tools/singlejar
bazel build //...
but it fails with:
ERROR: /Users/.../bazel/third_party/protobuf/3.2.0/BUILD:621:1: no such target '//external:gtest': target 'gtest' not declared in package 'external' defined by /Users/plaird/scone/public/bazel/WORKSPACE and referenced by '//third_party/protobuf/3.2.0:test_plugin'.
Do I need to build gtest locally, and then add it to the WORKSPACE file?
bazel build //..., no matter where you invoke it, will build everything in the project. It looks like what you probably want is bazel build //src/java_tools/singlejar/..., which will build all targets under that directory.
In general, though, you probably don't need to compile singlejar separately. I've been working on Bazel for several years and 99% of the time you don't have to build the tools separately.
In terms of the error you're getting, it would be nice if we could get //... building, but it hasn't been a huge priority. The protobuf code build is weird and I don't recommend trying to debug it, just jump into whatever you want to actually work on.

How to use .travis.yml to cancel a Travis build

For a project certain preconditions have to met before a CI build makes sense. Not all of them can be ensured prior to commit. Hence, to safe resources I'd like to evaluate those preconditions in .travis.yml (i.e. using shell commands) and cancel the Travis build if not met.
I don't think it'd be helpful or feasible to start experimenting with kill variations in the before_install phase. There must be a better way.
Put these checks in your regular install commands and have them exit with a failure if the preconditions are not met.
install:
- check_preconditions && actually_build
This will result in "failed" builds whenever this happens. A (slightly silly) workaround is to abort with success, which instead will show a "successful" build even when nothing was actually built.
install:
- { check_preconditions && actually_build; true; }

Why does Jenkins change my gradlew, and how to prevent that?

I'm trying to 'release' my project using the gradle release-plugin
The plugin starts by checking if my working copy is clean, so that only properly versioned stuff gets released.
This works just fine on my local machine. But when I try the same thing in a Jenkins job, the build fails complaining various stuff is changed in the workplace. I decided that a lot of stuff was just internally used by jenkins and added it to gitignore:
caches/
native/
wrapper/
But it also considers gradlew as changed:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':checkCommitNeeded'.
> You have uncommitted files:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
M gradlew
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Why does Jenkins change that file how do I prevent that?
I think the following settings on the Jenkins job might be relevant:
Checkout/merge to local branch (optional) is set to master. Without this setting the release plugin complains about not being on a branch
Clean after checkout is currently checked, but checking/unchecking it didn't make a difference
Make gradlew executable is checked, and at least to me sounds like a likely cause, but unchecking it makes the build faile because gradlew is not executable
Pretty old question, but for the record to anyone coming over here, jenkins isn't at fault here, you should commit gradlew with executable bit set:
# git update-index --chmod=+x gradlew
# git commit
Then you will no longer need the jenkins setting to set it executable, which is the workaround causing your issue.

Archiving artifacts not in the workspace when build fails

When an ANT build step fails in my build I'd like to archive the logs in order to determine the problem. The relevant logs, however, are not located in the workspace, so I have to use a full path to them.
The standard artifact archiving feature does not work well with full paths, so first I have to copy the logs into the workspace within some build step so that I can later archive them. I do not want to incorporate the copying code into the original ANT script (it does not really belong there). On the other hand, since the build step fails the build I can't execute the code that copies the artifacts into the workspace as a separate build step as it is never reached.
I am considering using ANT -keep-going option, but how will I then fail the build?
Any other ideas (artifact plugins that handle full paths gracefully, for example)?
Update: I've worked around the problem by creating a symbolic link in the workspace to the directory that contains the files to be archived. Kludgy, but effective.
I would recommend using Flexible Publish plugin in conjunction with the Conditional Build Step plugin.
The Flexible Publish plugin allows you to schedule build steps AFTER the build steps have normally run. This allows you to catch both successful and failed builds and execute something - say a script that copies the files from OUTSIDE the workspace to INSIDE the workspace. The Conditional BuildSet plugin allows conditionalizing the steps so that they only run when the build fails. Using these two plugins, you can copy the files into the workspace upon failure, then archive them with the usual Jenkins mechanisms.

Resources