Insert the content of a text file in a job description - jenkins

I'm trying to insert the content of a file inside a job description. My build generates a file and I can find it easily with the following URL: http:/[my-domain]job/[my-job]/lastBuild/artifact/[my-file]. In my case, this is a text file and I would like to display it in the job description. I can easily insert a link to this file with HTML but how can I insert the content of this file ?

What is your Jenkins running on? Windows or Linux?
The Project Description Setter plugin is indeed the way to go, but you need to display the content of your file in the build log first, before the plugin will pick it up.
Like Christopher said, you don't need a job URL to access the file that you have in your workspace.
For Linux, put the following into your shell execute step:
echo -n "[DESC] " && cat myfile
For Windows, use this:
echo|set /p="[DESC] " & type myfile
This will print the content of file and prefix it with "[DESC]". We need this prefix (it can be anything you want) to identify this line to the Description Setter plugin
In the job configuration, under Set build description, type:
\[DESC\] (.*)
One note: only the first line of the file will be printed in description

The Project Description Setter plugin can do this.

Related

How to make path of File Parameter Dynamic in Jenkins Job

I am using a file parameter in my Jenkins Job.
E.g. src/main/resources/file1.txt;
Now I have three files in the same directory (src/main/resources) : file1.txt, file2.txt, file3.txt
If I enter the path of file param as : src/main/resources/file1.txt
Then the uploaded file will be replacing the file1.txt file in my workspace.
Problem Statement :
I want the file parameter to be dynamic in nature so that I can upload file1/2/3 and it should replace the corresponding file in my workspace in directory(src/main/resources/).
Need suggestion if this can be achieved in Jenkins.
This is possible. You need to use 'Active Choice Reactive Reference Plugin'.
https://plugins.jenkins.io/uno-choice/

Can I get the arguments passed to bazel itself?

I want to create some "build traceability" functionality, and include the actual bazel command that was run to produce one of my build artifacts. So if the user did this:
bazel run //foo/bar:baz --config=blah
I want to actually get the string "bazel run //foo/bar:baz --config=blah" and write that to a file during the build. Is this possible?
Stamping is the "correct" way to get information like that into a Bazel build. Note the implications around caching though. You could also just write a wrapper script that puts the command line into a file or environment variable. Details of each approach below.
I can think of three ways to get the information you want via stamping, each with differing tradeoffs.
First way: Hijack --embed_label. This shows up in the BUILD_EMBED_LABEL stamping key. You'd add a line like build:blah --embed_label blah in your .bazelrc. This is easy, but that label is often used for things like release_50, which you might want to preserve.
Second way: hijack the hostname or username. These show up in the BUILD_HOST and BUILD_USER stamping keys. On Linux, you can write a shell script at tools/bazel which will automatically be used to wrap bazel invocations. In that shell script, you can use unshare --uts --map-root-user, which will work if the machine is set up to enable bazel's sandboxing. Inside that new namespace, you can easily change the hostname and then exec the real bazel binary, like the default /usr/bin/bazel shell script does. That shell script has full access to the command line, so it can encode any information you want.
Third way: put it into an environment variable and have a custom --workspace_status_command that extracts it into a stamping key. Add a line like build:blah --action_env=MY_BUILD_STYLE=blah to your .bazelrc, and then do echo STABLE_MY_BUILD_STYLE ${MY_BUILD_STYLE} in your workspace status script. If you want the full command line, you could have a tools/bazel wrapper script put that into an environment variable, and then use build --action_env=MY_BUILD_STYLE to preserve the value and pass it to all the actions.
Once you pick a stamping key to use, src/test/shell/integration/stamping_test.sh in the Bazel source tree is a good example of writing stamp information to a file. Something like this:
genrule(
name = "stamped",
outs = ["stamped.txt"],
cmd = "grep BUILD_EMBED_LABEL bazel-out/volatile-status.txt | cut -d ' ' -f 2 >\$#",
stamp = True,
)
If you want to do it without stamping, just write the information to a file in the source tree in a tools/bazel wrapper. You'd want to put that file in your .gitignore, of course. echo "$#" > cli_args is all it takes to dump them to a file, and then you can use that as a source file like normal in your build. This approach is simplest, but interacts the most poorly with Bazel's caching, because everything that depends on that file will be rebuilt every time with no way to control it.

In Jenkins, how do I get the substring of a build parameter to be part of the build name?

I've got a parameterized freestyle Jenkins job, and I'd like to include part of one of the parameters in the build name. The parameter that I'm interested in is called FILE_PATH. Due to the file directory structure of the project used by this job, the file path always starts with the same string, which is 9 characters long. So if FILE_PATH="somepath/what/I/want", I would like the name of my build to be what/I/want.
Some things I've tried putting in the "Set Build Name" field provided by the Build Name Setter plugin:
${FILE_PATH:9}
This gave me the following error:
Failed to evaluate name macro:org.jenkinsci.plugins.tokenmacro.MacroEvaluationException: Unrecognized macro 'FILE_PATH' in '${FILE_PATH:9}'
${"FILE_PATH":9}
FILE_PATH was just treated as a string:
New run name is '${"FILE_PATH":9}'
${$FILE_PATH:9}
This just led to the raw expression being included with FILE_PATH being expanded:
New run name is '${somepath/what/I/want:9}'
${ENV,var="FILE_PATH":9}
This didn't get processed at all:
New run name is '${ENV,var="FILE_PATH":9}'
I've got the Build Name Setter and the Token Macro plugins installed. I'd like to do this in the Build Name plugin, if possible, although I can install any other plugins I need to get this to work.
${ENV:9,var="FILE_PATH"} should do what you are looking for.
An alternative is to also eliminate the beginning of the string, as far as you know what exactly the string is:
${ENV#somepath/,var="FILE_PATH"}
In case you ever need to strip something from the end of the string instead, use % in place of #.
I'm assuming you are using a Jenkinsfile pipeline script?
If so, you can just use the groovy substring method.
variable.substring(9)
Not sure if you can do something similar using parameterized builds.

Jenkins email ext jelly script include file contents

Can I display the contents of another file in my jelly script output?
If the file is included in the workspace of the job, declare your variable such as:
<j:set var="fileContent" value="${build.getWorkspace().child("results.html")}"/>
And call it this way:
${fileContent}
Yes, you can use the ${FILE, path} token to include the contents of a file (path is relative to your workspace directory).
This info is taken from the Content Token Reference in the email-ext part of your job configuration. Click the question mark on the right to get the full list of tokens.
Look at util:loadText which is a "tag which loads text from a file or URI into a Jelly variable."
<u:loadText var="contents" file="${filename}"/>
${contents}
Haven't used it inside of Jenkins before... let us know if it works.

Can I create a selector file using the Plastic command line utility?

I am attempting to write a batch file for my developers to run that sets up workspaces. The batch file will use Plastic's command line utility, cm.exe.
When I create a workspace in the Plastic client GUI, there is a combo box that allows me to select the repository. This generates a plastic.workspace file, and a plastic.selector file that contains a reference to the selected repository.
However, when I use the command line utlity to make a workspace, I have to specify an existing selector:
cm mkwk MyWorkspace c:\MyWorkspacePath --selector=my.selector
or edit the selector when prompted:
cm mkwk MyWorkspace c:\MyWorkspacePath --selector
Ideally, I would like to be able to do something like
cm mkwk MyWorkspace c:\MyWorkspacePath MyRepository#MyServer:8888
and have the files generated the same way that they are done by the GUI.
Is there a way to do this using cm.exe, or do I have to write all the selectors beforehand?
Although it's not documented, you can issue the "mkwk" command with the "--repository" parameter. For example:
cm mkwk code c:\code --repository=code#localhost:8087
Hope it helps!

Resources