Jenkins Pipeline rename file - jenkins

I have to make a change to an existing Jenkins pipeline. I need to rename the file to incorporate the version. The version is already being used in other portions of the script.
This is what I have found but rename-item is not recognized as a command. Where can i find the commands I can use? How to I make this work?
stage('Rename') {
bat """
rename-item -Path "C:\\temp\\OriginalName.exe" -NewName "OriginalName-${version}.exe"
"""
}
TIA

Related

Copying a file to jenkins workspace location

I need to copy one file to the workspace location of jenkins for my pipeline jenkins job. For that I need to use bat command for windows.
What is the bat command to copy the file from one source location to the destination location.
I have used the code like
bat '''xcopy /Y /s "<source location>"*.*"<Destination location>"'''
The above code was getting the error as file not found but i have provided the right path of the files. Can anyone please help me with this.
I'm not sure what you are trying to do with the *.* but if you just want to copy a file from the source location to the destination, you should use:
bat '''xcopy /Y /s "<source location>" "<Destination location>"'''
no *.*. You will get file not found with *.*. If this doesn't fulfill your needs, then elaborate on exactly what you are trying to do here and I should be able to help.

Jenkins groovy file can't import another groovy

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")

Outputting script output values from jenkins to a file

I have my jenkins pipeline working and calling Ps scripts.
env.output= powershell(returnStdout: true, script: '.\\scripts\\script.ps1 -target_servername')
I would like to save the env.output to an automatically generated file within the workspace directory.
so like %workspace%\logs\%jobname%_%job_no%.log
And I would like to append details onto the file.
Your are alreaddy using powershell. So i think the Add-Content Commandlet is the perfect tool for your. Just switch to a multiline powershell script.

How would I pass a variable into Jenkins which contains wildcards?

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.

How do I make jar files available to ant in a Jenkins pipeline?

I've put together a basic Jenkins pipeline and it does what I expect for the most part.
However, I'm using ant and it requires access to specific jar files. I've specified the build step like so:
stage('Build') {
// Build the project
env.PATH = "${tool 'ant'}/bin:${env.PATH}"
sh 'ant -f dita-tools/build_all.xml -lib $WORKSPACE/dita-ot/lib:$WORKSPACE/dita-ot/lib/saxon'
}
The build I run through this pipeline fails and generates the following error:
java.lang.ClassNotFoundException: org.dita.dost.module.GenMapAndTopicListModule
From what I can tell, this is due to ant not having access to dost.jar that is in the dita ot. I've tried defining this argument a number of ways including specifically referencing dost.jar (I have a number of jars to include) but every time it fails with the same error.
When I put together a stand-alone ant project in Jenkins, ant has no problem accessing the jar by way of the argument I provide above. Is there a better way for me to supply this argument/dependency in a pipeline?
UPDATE:
I added an echo statement for the classpath to my build script and was able to verify that adding the jars to the classpath in the build script does in fact work. So, for all intents and purposes, ant has access to all the relevant base toolkit jars for the target but the error persists. At this point, it appears that the problem has something to do with how the jenkins pipeline works as opposed to the dita ot itself?
I assume you use custom plugins, if yes, please make sure, you correctly defined your jars in the plugin.xml like so:
<feature extension="dita.conductor.lib.import" file="lib/my.jar"/>
UPDATE
java.lang.ClassNotFoundException: org.dita.dost.module.GenMapAndTopicListModule
This error means, that the main DITA-OT jar is not found on your classpath. So this indicates, that this is not a plugin issue.
Usually you don't have to setup the classpath, Ant does that for you. Please also read Creating an Ant build script.
Please try a snippet like this one:
node {
try {
checkout scm
stage('Build') {
sh '''
dir=$(pwd)
curl [your-dita-ot-url] --output dita-ot.zip
unzip -qq "$dir/dita-ot.zip"
rm dita-ot.zip
chmod +x ${WORKSPACE}/dita-ot/bin/ant
${WORKSPACE}/dita-ot/bin/ant -f ${WORKSPACE}/build.xml -Ddita.dir=$dir/dita-ot -Dbranch.name=$BRANCH_NAME
'''
}
} catch (e) {
currentBuild.result = "FAILED"
throw e
} finally {
notifyBuild(currentBuild.result)
}
}

Resources