I am trying to build a jar and include specific files:
jar cf models.jar target/classes/**/models
However, I am making a pipeline with variables:
jar cf ${JAR_NAME}.jar ${FILE_SEARCH_PATTERN}
This causes the command to run as:
jar cf models.jar 'target/classes/**/models'
which causes the system to not find any files as the quotes break the search.
I found a solution to my problem; while it doesn't get around how the groovy script is translated in Jenkins, this might help people trying to achieve something similar.
# in project Jenkinsfile
file_search_path = "target/classes/.*/models/.*\\.class"
# in library Jenkinsfile
files=\$(find . -print | grep -i ${FILE_SEARCH_PATH})
jar cf ${JAR_NAME}.jar \$files
Here is a link to the full version of the code.
Related
In simple form, I have 2 groovy files in the repo in /jenkins subfolder. File A.groovy and B.groovy. Inside A.groovy I have a load line
load(env.WORKSPACE + "#script/jenkins/B.groovy")
The problem is I get an error
java.nio.file.NoSuchFileException:
/Users/user/.jenkins/workspace/JobName#script/jenkins/B.groovy
But as we see, overall looks like load function created kinda almost correct url. The point is my actual fetched repo, and particularly A.groovy is getting into the additional subfolder. I see that in the very beginning of the logs and can find locally there.
Checking out git ... into /Users/user/.jenkins/workspace/JobName#script/ecb7a9317b1ad672698830264d9e0ce2b9b6f330c043bb85f48623f3cdcab65e/jenkins/A.groovy
Tried to log whole env object using echo sh(script: 'env|sort', returnStdout: true) and there is no any property containing that subfolder name at all.
Why I am getting that extra ecb7a9317b1ad672693830224d9e0ce2b9b3f730c043bb85f48925f3cdcab65e subfolder and how can I either get rid of it or get it's name somehow to compose correct url for import?
I've found a workaround (by searching the folder), but would like to find a better and native way.
Where jenkins in -name 'jenkins' is the subfolder name containing groovy scripts.
git_jenkins_folder = sh (
script: "find \"" + WORKSPACE + "\"#script -type d -name 'jenkins'",
returnStdout: true
).trim()
utils = load("$git_jenkins_folder/Utils.groovy")
Which generates the correct working path like this.
/Users/user/.jenkins/workspace/JobName#script/ecb7a9317b1ad672698830264d9e0ce2b9b6f330c043bb85f48623f3cdcab65e/jenkins
I think the issue is with the path you pass to load. The env.WORKSPACE does not end with /.
load("${env.WORKSPACE}/#script/jenkins/B.groovy")
I have a project with different modules, I want to build the code from a specific module. I'm using "Invoke Gradle Script" in the build step on Jenkins.
By default, the Gradle plugin tries to locate the wrapper executable next to the build script. If it is not there it will look in the workspace root. What I try to do was changing the Wrapper location, to force the build of the correct module.
This is the code structure that I have.
|yurora
|
|---module
|---module
|---wos
| |____src
| |____gradle
| |____wrapper
|
|---module
|---module
This is the value that I have on Wrapper location
${workspace}/yurora/wos
But I get this error FATAL: The Gradle wrapper has not been found in these directories: /var/lib/jenkins/workspace/JenkinsTaskName/yurora/wos.
If I change the value of Wrapper location to
${workspace}/wos
The code builds from the workspace root, and that is what I'm trying to avoid.
What should be the correct path on Wrapper location?
Jenkins configuration
I ended up creating a new git repo with the module that I wanted to build.
It ended up being easier for our time to do it like this, and everything worked out of the box.
I have a Jenkins job that invoke a gradle script to create a .war file from sources.
gradle war command produces a file with name Geo-1.0.5.war because build.gradle use version number:
war {
baseName = 'Geo'
version = '1.0.5'
}
This file will be copied and deployed on a Wildfly server trough SSH using "Publish Over SSH Plugin".
How can I tell to the plugin that the war filename format is something like Geo-$gradle_version.war?
This is documented if you click the (?) help icon next to the "Source files" field within Jenkins:
The string is a comma separated list of includes for an Ant fileset eg. **/*.jar
(see Patterns in the Ant manual).
So in your case, you could use **/Geo-*.war as the source pattern.
This is also shown in the screenshot on the plugin wiki page, and in the Source Files and Examples sections on the linked "Publish Over…" documentation.
In your comment to this answer, you mention that you don't want to communicate that the filename is "something like Geo-$gradle_version.war" for uploading, but rather want to use the exact filename in a script being executed on the SSH host.
You could do this by adding an Execute Shell step which determines the filename, and exporting it as an environment variable using the EnvInject Plugin. For example:
f=$(basename `find . -name 'Geo-*.war'`)
echo WAR_FILENAME=${f} > env.properties
Then, by using an Inject Environment Variables step with its path set to env.properties, the WAR_FILENAME value will be added to the build environment, available for use by subsequent steps.
In the Exec Command field of the SSH-publishing step, you can then use ${WAR_FILENAME} to refer to the exact filename uploaded.
I know its possible to run a .dsl file from an external source instead of just writing the code of the flow in the job's description, but every time I try to run lets say:
/home/flows/flow_script.dsl
I get the following error:
java.io.FileNotFoundException:/home/flows/flow_script.dsl (No such file or directory)
The path is correct, I can see the file through that path from the shell, but it doesnt let me select anything outside the "builds workspace" apparetly.
I recently ran into this very issue: my DSL script was outside of my workspace (installed via a package). The problem is that the DSL Scripts path is an Ant format that only allows specific patterns (and not absolute paths).
My workaround is hacky, but it did work: add an Execute Shell step before the "Process Job DSLs" step that symlinks the external directory into the workspace.
Something like this:
echo "Creating a symlink from /home/flows to workspace"
ln -sf "/home/flows" .flows
Then you can set the DSL Scripts path to ".flows/flow_script.dsl".
This has some additional caveats, of course: the directory you're symlinking from will need to be accessible by the jenkins user. And it likely violates a lot of best practices.
I added an 'Execute shell' build step in Jenkins to run the cpplint.py
python /var/lib/jenkins/scripts/cpplint.py --counting=detailed `find path -name *.cpp
I also added 'Scan for compiler warnings' and added CppLint.
However it always gets 0 warnings even though it displayed in the Console output some warnings such as
filename.cpp:18: Missing space after , [whitespace/comma] [3]
If you run cpplint.py with --output=vs7 it will produce the format expected by the Jenkins warnings plugin.
I use the Cppcheck Plugin and cpplint_to_cppcheckxml.py to convert cpplint.py output to the XML format expected by Cppcheck Plugin. This works really well. I can click on the offending issue in Cppcheck Results displayed on the Jenkins job page and it will display the source code with offending line highlighted. Very cool.
You must provide cpplint an absolute path to your source code directory in order for the hyperlink generation to work on Cppcheck Results page. The only con I see is your Cppcheck and cpplint results are combined and not separated.
If using Linux bash scripts, here is how I turn a relative path into absolute in order for the cpplint to generate absolute paths in it's output:
# Build cpplint reports and transform to cppcheck compatible XML format
# Convert relative path to absolute path so that Jenkins job can easily display the source code errors
srcPathAbsolute=${PWD}/../dicegame/src/main
srcPathAbsolute=$(readlink -f ${srcPathAbsolute})
cpplint.py --counting=detailed ${srcPathAbsolute}/*.cpp 2>&1| cpplint_to_cppcheckxml.py &> cpplint-cppcheck-result.xml
In my Jenkins job configuration to locate both ccplint-cppcheck-result.xml files and my normal cppcheck-result.xml.
Publish cppcheck results
Cppcheck report XMLs **/*cppcheck-result.xml
Thanks to original developer of cpplint_to_cppcheckxml.py. This script serves as an example on how to connect output of other tools into existing Jenkins plugins. Very nice!