Jenkins - how to set Variable value into the zip file name? - jenkins

I have a variable, set as global environment, which outputs the timestamp.
echo "Current build version: ${BUILDVERSION}"
Current build version: 20211117-114343
Now I want to add this value to zip step for setting the zip file name and I check the content of the zip file.
zip zipFile: 'test_${BUILDVERSION}.zip'
sh 'zipinfo -1 test_${BUILDVERSION}.zip'
ZIP is not created properly: test_${BUILDVERSION}.zip while zipinfo takes this value properly. zipinfo -1 test_20211117-114343.zip
Can you please assist what I do wrong? Thanks

Solution
Use double quotes. Read about String interpolation in Groovy
zip zipFile: "test_${BUILDVERSION}.zip"

Related

Unix. Parse file with full paths to SHA256 checksums files. Run command in each path/file

I have a file file.txt with filenames ending with *.sha256, including the full paths of each file. This is a toy example:
file.txt:
/path/a/9b/x3.sha256
/path/7c/7j/y2.vcf.gz.sha256
/path/e/g/7z.sha256
Each line has a different path/file. The *.sha256 files have checksums.
I want to run the command "sha256sum -c" on each of these *.sha256 files and write the output to an output_file.txt. However, this command only accepts the name of the .sha256 file, not the name including its full path. I have tried the following:
while read in; do
sha256sum -c "$in" >> output_file.txt
done < file.txt
but I get:
"sha256sum: WARNING: 1 listed file could not be read"
which is due to the path included in the command.
Any suggestion is welcome
#!/bin/bash
while read in
do
thedir=$(dirname "$in")
thefile=$(basename "$in")
cd "$thedir"
sha256sum -c "$thefile" >>output_file.txt
done < file.txt
Modify your code to extract the directory and file parts of your in variable.

Acces groovy var in sh in jenkinsfile

I am using enviroment variable ARTIFACT_VERSION, and want to put it in shell script
sh "wget -q http://nexus.com/$polygon/$env.ARTIFACT_VERSION/$polygon-$env.ARTIFACT_VERSION.zip"
And got this errors
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field java.lang.String zip
And if I input var with this style ${env.ARTIFACT_VERSION}
I get in output result like I am using \n (but version input is correct)
wget -q http://nexus.com/polygon/myversion
/polygon-myversion
.zip
The link is correct when i used it without vars - all is ok
myversion parameter was getting as a result from grep command
and it had \n at the end

Cannot get Description field of a Jenkins Job or Parameter

Is it possible to obtain the Job Description or the Job Parameter Description in run-time or later like the BUILD_ID or JOB_NAME?
I search for plugins or workarounds and nothing.
Thanks.
This would be Tip/workaround
https://<<yourjenkinsdomain>>/job/<<yourjobname>>/configure (will open the configuration of your job)
However
https://<<yourjenkinsdomain>>/job/<<yourjobname>>/config.xml (will give the job configuration in an xml format)
You can download this xml via curl at run time or using jenkins cli and use a grep with -B option to find description per value.
Considering you have copied the with name "config.xml"
cat config.xml | grep -B 1 "description"
Will give you description and build parameter name
Grep command
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines.
Places a line containing a group separator (--) between
contiguous groups of matches. With the -o or --only-matching
option, this has no effect and a warning is given.
Sample output :
cat config.xml | grep -B 1 "description"
<actions/>
<description>Job description : Automation </description>
--
<name>branch</name>
<description>mandatory parameter , used for automation</description>
--
Alternative :
jenkins cli has an option to set value
set-build-description Sets the description of a build.
set-build-parameter Update/set the build parameter of the current build in progress. [deprecated]
you can write a small script and get the values into variables and use them

How to use file parameter in jenkins

I am executing parameterised build in jenkins to count no. of lines in file which has 1 file parameter. Its file location is pqr. The name of the script file is linecount.sh which is saved at remote server. When i tried to execute it using command sh linecount.sh filename, it works perfectly from jenkins. But as i remove filename from the argument and execute same script as parameterised build it is showing below error on console :
Started by user Prasoon Gupta
[EnvInject] - Loading node environment variables.
Building in workspace users/Prasoon/sample_programs
Copying file to pqr
[sample_programs] $ /bin/sh -xe /tmp/hudson3529902665956638862.sh
+ sh linecount.sh
PRASOON4
linecount.sh: line 15: parameterBuild.txt: No such file or directory
Build step 'Execute shell' marked build as failure
Finished: FAILURE
I am uploading file (parameterBuild.txt) from my local machine. Why is it giving this error?
My doubt is in shell script I used argument as $1. How can I refer this when I am taking file as parameter.
The uploaded file will not retain the same name as it has on your local computer. It will be named after the File location argument specified in the file parameter settings:
In this example I will get a file called file.txt in my workspace root, regardless of what I call it on my computer.
So if I now build my job and enter the following in the parameter dialog (note that my local filename is table.html):
Then I get the following in the log (I have a build step which does ls -l):
Building on master in workspace /var/lib/jenkins/workspace/fs
Copying file to file.txt
[fs] $ /bin/sh -xe /tmp/hudson845437350739055843.sh
+ ls -l
total 4
-rw-r--r-- 1 jenkins jenkins 292 Feb 15 07:23 file.txt
Finished: SUCCESS
Note that table.html now is called file.txt, e.g. what I entered as File location.
So in you're case the command should be:
sh linecount.sh pqr
There is a a bug since ages that makes impossible to use fileParameter:
Handle file parameters
file parameter not working in pipeline job
There is a workaround for this issue https://github.com/janvrany/jenkinsci-unstashParam-library
and in a pipeline script you do:
library "jenkinsci-unstashParam-library"
node {
def file_in_workspace = unstashParam "file"
sh "cat ${file_in_workspace}"
}
If it's to do with Free-Style job & if your configuration looks similar to this - https://i.stack.imgur.com/vH7mQ.png then you can run simply do sh linecount.sh ${pqr} to get what you are looking for?

KSH Resolving Environmental Variables for an Entire File

I have a list of file names with environment variables in them. I'd like to read the file line by line and then set a variable to the read in line however have the envirnment variable translated to the appropriate environment variable. Here is my script so far:
#!/bin/ksh
. /test/currentEnv.sh
while read line
do
echo $line
done < $1
if my source file is:
foo1$ENVVAR1.csv
foo2$ENVVAR2.csv
foo3$ENVVAR3.csv
and my Environment variables in currentEnv.sh are
$ENVVAR1=hello; export ENVVAR1
$ENVVAR2=world; export ENVVAR2
$ENVVAR3=test; export ENVVAR3
I'd like the results of the script to be
foo1hello.csv
foo2world.csv
foo3test.csv
currently it just dumps out the original file:
foo1$ENVVAR1.csv
foo2$ENVVAR2.csv
foo3$ENVVAR3.csv
Edit
I was able to get the majority of my files resolved using:
#!/bin/ksh
. /test/currentEnv.sh
while read line
do
eval echo $line
done < $1
however some of my variables are in the middle of string like:
foo3$ENVVAR3_bar.csv
this seems to look for an env variable $ENVVAR3_bar and doesn't find it I need this to output:
foo3test_bar.csv
You declare a variable without the dollar sign:
$var=value # no
var=value # yes
Since underscore is a valid character for a variable name, ksh is trying to expand the variable named ENVVAR3_bar: you need to use braces to separate the variable name from the surrounding text:
foo3$ENVVAR3_bar.csv # no
foo3${ENVVAR3}_bar.csv # yes

Resources