Outputting script output values from jenkins to a file - jenkins

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.

Related

Are there any api's available to automate the "Apply new configuration" action in Jenkins Configuration as Code plugin

How can I apply a new Jenkins configuration as code YAML file from a groovy script, similar to clicking the "Apply new configuration" button in the UI.
I didn't try yet, but this looks as a feasible approach to me:
In the Jenkins home directory (/var/lib/jenkins on my Ubuntu installation) there is a file io.jenkins.plugins.casc.CasCGlobalConfig.xml. Edit that in order to provide a path/URL to your YAML file. On my system that file has the content like below, and I assume that just replacing the value of the <configurationPath> element with your yaml path should be sufficient.
<io.jenkins.plugins.casc.CasCGlobalConfig plugin="configuration-as-code#1569.vb_72405b_80249">
<configurationPath>/PATH/TO/MY/jenkins.casc.yaml</configurationPath>
</io.jenkins.plugins.casc.CasCGlobalConfig>
This configuration seems to be automatically applied each time when Jenkins is restarted.
In order to apply the configuration immediately, there seems to be jenkins-cli api for that. See the http://YOUR_JENKINS/manage/cli page and look for the reload-jcasc-configuration command, which suggests: java -jar jenkins-cli.jar -s http://YOUR_JENKINS/ -webSocket reload-jcasc-configuration
You ask for a "groovy script". With that I can't help, but I guess that modifying a file or running the jenkins-cli should be perfectly doable in a groovy 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.

TFS - Reference Filepath of Downloaded Secure File

I have a Build Pipeline in Azure DevOps where I am downloaded a secure key file. In my next step, I want to reference the filepath of this newly downloaded file with a Command Line Script task such as:
sfdx force:auth:jwt:grant --jwtfile $env:DOWNLOADSECUREFILE_SECUREFILEPATH
The problem is that the environment variable does not seem to exist-- the Script is taking the literal plaintext of "$env:DOWNLOADSECUREFILE_SECUREFILEPATH" rather than transposing the filepath as I am expecting. I should note that I am not using full YAML, I am using the new "Visual Designer" interface.
Has anyone done this in the past?
If you're running it from a Command Line task, then it's not PowerShell. $env::... is PowerShell syntax. Try %DOWNLOADSECUREFILE_SECUREFILEPATH%. Or if this is a non-Windows agent (say, a Bash script), just $DOWNLOADSECUREFILE_SECUREFILEPATH

Jenkins Publish Over SSH Plugin filename variable

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.

Automation of documenting code base using Doxygen plug in Jenkins CI environment

I use a shell script to create/run doxygen doxyfile to document my code base
which works absolutely fine(Schedule runs and recursive scan code base also
works fine).
Now my requirement is to do the same job using Jenkins CI.
I added doxygen plug which generates documentation output and stores the result in Jenkin workspace.
My question, is there any another ways to run script and generate doxyfile in
Jenkins environment and also
How to create url link to display doxygen HTML output.
Have you seen the Jenkins DocLink plugin? This plugin makes it easy to put a link on your project page to documentation generated in a build.

Resources