How to pass the parameter - jenkins

I have a jenkins JOb which is calling the BAT file which contains the call for As java -jar testrunscripts/SQLWorkbench/sqlworkbench.jar -url=jdbc:as400:/;"translate binary"=true;naming=sql;libraries=; -driver=com.ibm.as400.access.AS400JDBCDriver -username=-password=-driverjar=E:\\\\resources\\lib\\jt400.jar -script='testrunscripts/HISTORYANDNEWDIFF.sql'
1-Jenkins Integrats the SQLWorkbench which calls the one sqlscript(HISTORYANDNEWDIFF.sql). which needs the dynamic table name required .
WbExport -file='E:\\TestingDATABASE\\history_XAXPGRFE.csv' -type=text -delimiter=',';
select * from %SOURCE%.XAXPGRFE where XPORIG='JAVAPGM'
How would pass the parameter to the query from the jenkins pipeline to bat file and then sql script

Your question is a bit unclear but try to call the bat script from your Jenkins job with a key value pair:
your_bat_script.bat param1=value1
and then in the bat script call the SQL Workbench /J script (HISTORYANDNEWDIFF.sql) using the -variable flag:
java -jar testrunscripts/SQLWorkbench/sqlworkbench.jar -url=jdbc:as400:/;"translate binary"=true;naming=sql;libraries=; -driver=com.ibm.as400.access.AS400JDBCDriver -username=-password=-driverjar=E:\\resources\lib\jt400.jar -script='testrunscripts/HISTORYANDNEWDIFF.sql -variable %1'
http://www.sql-workbench.net/manual/commandline.html#cmdline-vardef
%1 will contain param1=value1

Related

how to create a directory with datestamp as its filename in jenkins pipeline?

bat 'set OutputFolderName=%date:~12,2%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%'
bat 'mkdir %OutputFolderName%'
These two commands should give the correct output but they aren't working.
This is the error I got:
Try multiline bat command as follows:
bat """
set OutputFolderName=%date:~12,2%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%
mkdir %OutputFolderName%
"""
Edit: Updated with snapshots
Have a look at my pipeline snapshot here:
Pipeline Console Output
Creates a folder something like this:

How to send credentials to a powershell script in a jenkins pipeline?

When executing the following code in a Jenkins pipeline, a "The following steps that have been detected may have insecure interpolation of sensitive variables" warning is being added to the build, with a link to https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#string-interpolation with explanation.
powershell script: """
\$ErrorActionPreference = "Stop"
cd "${WORKSPACE}\\MyDirectory"
& .\\myScript.ps1 -user "${creds_USR}" -passw "${creds_PSW}"
"""
I've already tried to change it as described in the link above, but then the variables don't seem to be replaced anymore.
powershell script: '''
\$ErrorActionPreference = \"Stop\"
cd \"$WORKSPACE\\MyDirectory\"
& .\\myScript.ps1 -user \"$creds_USR\" -passw \"$creds_PSW\"
'''
Would somebody know a working solution for this please?
Presumably you have a block like this that's generating those values:
environment {
creds = credentials('some-credentials')
}
So your build environment has those variables available to Powershell. Rather than interpolating the string that constitutes the Powershell script, then, just write the script to pull the data from the environment.
powershell script: '''\
$ErrorActionPreference = "Stop"
cd "$Env:WORKSPACE\MyDirectory"
& .\myScript.ps1 -user "$Env:creds_USR" -passw "$Env:creds_PSW"
'''

How to add a configuration parameter with spaces on Jenkins

I have a pipeline which builds a C++ project for that I am using MSBuild, untill now we were using the "Final" configuration parameter, but now I need to switch it to "Release Steam D3D11", when I try to do that I get an error on Jenkins when building the project I guess it is because of the spaces, how can I make Jenkins to take this parameter? this is what I have tried:
stage('Build'){
steps{
script {
def msbuild = tool name: 'MSBuild', type: 'hudson.plugins.msbuild.MsBuildInstallation'
bat "\"${msbuild}\" AoC/Source/project-GRDK.sln /t:Rebuild /p:configuration=Release Steam D3D11"
}
}
}
I have also tried adding ' ' to the configuration name such as :
bat "\"${msbuild}\" AoC/Source/project-GRDK.sln /t:Rebuild /p:configuration='Release Steam D3D11'"
but it does not work neither as I get this error:
00:00:01.407 MSBUILD : error MSB1008: Only one project can be specified.
I sorted it out in case somebody have the same issue, wrapping the string with spaces within \" \" does the job
bat "\"${msbuild}\" AoC/Source/age2-GRDK.sln /t:Rebuild /p:configuration=\"Release Steam D3D11\""

What is use of sh ''' <command > ''' - three ticks - in a Jenkinsfile?

I have a Jenkinsfile which uses three tick marks surrounding a command to execute as in:
sh ''' command '''
We have no idea why three tick marks are required or what role they perform.
This syntax is seen in the Jenkinsfile doc set.
This has nothing at all to do with bash (in which triple-quotes have no special meaning at all), and everything to do with Groovy (the separate, non-bash interpreter that parses Jenkinsfiles).
In Groovy, but not in bash, strings must use triple-quotes to span multiple lines.
In the context of a sh directive in a Jenkinsfile, the content of your triple-quoted string is passed to a shell as a script to execute; however, the syntax is parsed by Groovy, so it's only Groovy that cares about the quotes themselves (as opposed to the quoted content).
Can you give more idea about what kind of command is it, is it a unix command or some script ?
The single quote and its variation like '''(3 ticks) as mentioned in question skip the variable expansion, and it could used to show what is being executed.
echo '''Updating JAVA_HOME variable :
export $JAVA_HOME="$NEW_JAVA_HOME" '''
However in your question, a command (some string) is enclosed between 3 ticks marks and sh tries to execute this command or script. One such example below
$ echo "echo hello" > /tmp/tesh.sh
$ sh '''/tmp/test.sh'''
hello

Parsing the command line in jenkins-cli groovy scripts

I have written a groovy script to collect some statistics from my Jenkins server, which works ok in the default case.
However, when I want to pass options to it, I am getting trouble:
(1) The standard groovy CliBuilder exists, but fails to instantiate under jenkins-cli:
import jenkins.model.*
import groovy.util.CliBuilder
println("CliBuilder imported; calling constructor...")
def cli = new CliBuilder(usage: 'myscript.groovy [-halr] [name]')
results in
$ java -jar "jenkins-cli.jar" -s https://myjenkins1/ groovy myscript.groovy
CliBuilder imported; calling constructor...
ERROR: Unexpected exception occurred while performing groovy command.
java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:195)
at RemoteClass.class$(RemoteClass)
at RemoteClass.$get$$class$groovy$util$CliBuilder(RemoteClass)
at RemoteClass.run(RemoteClass:4)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:266)
....
(2) the options starting with a dash are intercepted by jenkins-cli, instead of passing it to the script
$ java -jar "jenkins-cli.jar" -s https://myjenkins/ groovy ./myscript.groovy -h
ERROR: "-h" is not a valid option
java -jar jenkins-cli.jar groovy [SCRIPT] [ARGUMENTS ...] [--username VAL] [--password VAL] [--password-file VAL]
Executes the specified Groovy script.
SCRIPT : Script to be executed. File, URL or '=' to represent
stdin.
ARGUMENTS : Command line arguments to pass into script.
--username VAL : User name to authenticate yourself to Jenkins
--password VAL : Password for authentication. Note that passing a
password in arguments is insecure.
--password-file VAL : File that contains the password
Does it mean that the jenkins-cli groovy interface is meant only for simple tasks and should not handle complicated command lines?
Or are these known caveats with known workarounds?
A few issues here:
The jenkins-cli.jar requires groovy to be piped in via stdin.
Command line arguments are read via the args array
It does appear that args with a - are passed to the local jenkins-cli.jar and not the groovy script running on the server.
So given the following groovy script saved as cli.groovy:
println(args)
You would run it like this:
$ java -jar "jenkins-cli.jar" -s https://myjenkins/ groovy = foo bar baz bat < myscript.groovy
Which prints:
[foo, bar, baz, bat]

Resources