Jenkins: Running groovy script with ant commands - jenkins

I have a groovy script with ant commands on it. The script is successfully run in my local machine but when I tried it with Jenkins the groovy script always fail. Jenkins always return error that "ant can't create task or type p4Change". I already added Apache ant support in the global configuration. How do I configure ant to successfully run the groovy script I have. Any idea. Thanks.
Sample code snippets:
I have execute.groovy file with ant commands
ant=new AntBuilder()
def checkChanges {
ant.p4change(description:"Checking",port:'perforce:1666',user:optional,view:"'${workspace}'...")}
And I created a batch file that will run the execute.groovy file
call groovy execute.groovy project bopolz18 -c bopolz18.Workspace 150718
In my machine this works well but in Jenkins when I execute the batch command it fails giving the error mention above.

Related

Error with Jenkins executing weblogic.jar

I am using jenkins with windows 10 and weblogic. I created a pipeline to connect git, compile with maven and weblogic deploy. When run the command sh 'java weblogic.WLST script.py' i have the error :
Could not find or load main class weblogic.WLST"
But when a run this command in prompt windows, i dont have problems. I have the environment variables JAVA_HOME and CLASSPATH.
Do I need to configure anything inside the jenkins to use this jar, weblogic.jar?

How to include CSV file in JMETER running in Jenkins

I am using Jenkins as my deployment pipeline. I have a JMETER project that I will be executing in a build step in Jenkins. That JMETER project has a dependency on a csv file for parameters. How do I get that file included in the Jenkins pipeline and how do I tell JMETER where to look for it in the csv data set config?
I could also do it via a gradle command if that is an option.
Thanks.
The easiest option is running JMeter in command-line non-GUI mode, the relevant Jenkins Pipeline snippet would be:
node {
stage 'Run JMeter Test'
bat 'c:/jmeter/bin/jmeter.bat -n -t c:/jmeter/extras/Test.jmx -l test.jtl'
}
The above setup assumes Windows operating system, if your Jenkins master or build agent is running Linux, Unix or MacOSX - just change bat to sh. You will also need to amend JMeter installation path to reflect your environment. See Running a JMeter Test via Jenkins Pipeline - A Tutorial to see example configuration.
In case of command-line non-GUI mode of JMeter execution you need to copy your CSV file(s) to "bin" folder of your JMeter installation.
In case of using JMeter Ant Task - the same approach, drop your CSV file(s) to JMeter's "bin" folder
In case of JMeter Maven Plugin you will need to copy CSV file(s) to src/test/jmeter folder of your Maven project.
And finally you can just use full path(s) (not relative) to CSV file(s) in CSV Data Set Config elements.

Execute a script from jenkins pipeline

I have a jenkins pipeline that builds a java artifact,
copies it to a directory and then attempts to execute a external script.
I am using this syntax within the pipeline script to execute the external script
dir('/opt/script-directory') {
sh './run.sh'
}
The script is just a simple docker build script, but the build will fail
with this exception:
java.io.IOException: Failed to mkdirs: /opt/script-directory#tmp/durable-ae56483c
The error is confusing because the script does not create any directories. It is just building a docker image and placing the freshly built java artifact in that image.
If I create a different job in jenkins that executes the external script as
its only build step and then call that job from my pipeline script using this syntax:
build 'docker test build'
everything works fine, the script executes within the other job and the pipeline
continues as expected.
Is this the only way to execute a script that is external to the workspace?
What am I doing wrong with my attempt at executing the script from within
the pipeline script?
The issue is that the jenkins user (or whatever the user is that runs the Jenkins slave process) does not have write permission on /opt and the sh step wants to create the script-directory#tmp/durable-ae56483c sub-directory there.
Either remove the dir block and use the absolute path to the script:
sh '/opt/script-directory/run.sh'
or give write permission to jenkins user to folder /opt (not preferred for security reasons)
Looks like a bug in Jenkins, durable directories are meant to store recovery information e.g. before executing an external script using sh.
For now all you can do is make sure that /opt/script-directory has +r +w and +x set for jenkins user.
Another workaround would be not to change the current directory, just execute sh with it:
sh '/opt/script-directory/run.sh'
I had a similar concern when trying to execute a script in a Jenkins pipeline using a Jenkinsfile.
I was trying to run a script restart_rb.sh with sudo.
To run it I specified the present working directory ($PWD):
sh 'sudo sh $PWD/restart_rb.sh'

Executing groovy scripts on Jenkins' slaves

I'm trying to execute a simple groovy script on Jenkins' slaves using the Groovy plugin.
I'm using the execute groovy script option and not the execute system groovy script option. I'm also using the "install automatically" (install from groovy website) option of the plugin of the latest version(2.4.6) . However, the job fails and I'm getting this:
Building remotely on .... (...) in workspace C:\Jenkins_Slave\workspace\...
Unpacking https://dl.bintray.com/groovy/maven/apache-groovy-binary-2.4.6.zip to C:\Jenkins_Slave\tools\hudson.plugins.groovy.GroovyInstallation\groovy_2_4_6 on ...
[groovy_reset_dead_slave] $ C:\Jenkins_Slave\tools\hudson.plugins.groovy.GroovyInstallation\groovy_2_4_6\bin\groovy.bat C:\Jenkins_Slave\workspace\...\hudson5850644430171226650.groovy
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
C:\Jenkins_Slave\workspace\...\hudson5850644430171226650.groovy: 5: unable to resolve class jenkins.model.Jenkins
# line 5, column 1.
import jenkins.model.Jenkins
^
1 error
Build step 'Execute Groovy script' marked build as failure
Finished: FAILURE
How can I overcome this issue and make it work properly?
From the Groovy plugin page:
Groovy Script vs System Groovy Script
The plain "Groovy Script" is run in a forked JVM, on the slave where the build is run. It's the basically the same as running the "groovy" command and pass in the script.
The system groovy script, OTOH, runs inside the Jenkins master's JVM. Thus it will have access to all the internal objects of Jenkins, so you can use this to alter the state of Jenkins. It is similar to the Jenkins Script Console functionality.
When not using the system Groovy, you do not have Jenkins object in your classpath. You need to make sure the required classes are accessible from the job you are running, and pass them in the "Class path" field of the "execute groovy script" step.
We can use RemoteDignostics class
The following Script runs on master but uses RemoteDignostics to run groovy on Worker Nodes/slave.
import hudson.util.RemotingDiagnostics
import jenkins.model.Jenkins
String agent_name = 'your agent name'
groovy_script = '''
println System.getenv("PATH")
println "uname -a".execute().text
'''.trim()
String result
Jenkins.instance.slaves.find { agent ->
agent.name == agent_name
}.with { agent ->
result = RemotingDiagnostics.executeGroovy(groovy_script, agent.channel)
}
println result

Run Jenkins ant build within special shell environment

Our internal build system uses a shell script to setup the environment for building projects. Then the actual build tools (ant or make) can reference environment variables for configuring various things. In essence, it does:
$ /path/to/setup_env.sh .
[build env] $ ant compile
Note that the first command launches and initializes a new shell and expects all subsequent build operations to be performed in that shell.
Now I am trying to replicate the same within Jenkins. How do I run a shell script and then have the subsequent ant build step take place in the same environment?
The 'Execute Shell' built-in as well as the EnvInject plugin didn't help since they discard any changes to the environment before moving to the next build step.
I'd prefer not to modify the ant build file since the same should continue to work in the current internal build system.
This is a "solution" that worked out for us. The key idea is that the setup_env.sh script launches a new shell in which it exports a bunch of environment variables. What we needed was access to those variable definitions. So we did a three part Jenkins Build:
Step 1 - Execute Shell
Use the 'Execute Shell' Jenkins built-in to run our setup_env.sh script. Then feed the newly launched shell a simple python script that dumps the environment to a file.
/path/to/setup_env.sh . <<< 'python <<SC
print "Exporting env to buildenv.properties file"
import os
f = open("buildenv.properties", "w")
env = os.environ
for k in env:
f.write("%s=%s\n" % (k, env[k]))
f.close()
print "Done exporting env"
SC'
Step 2 - Inject Environment Variables
Now we use the EnvInject Plugin to inject environment variables from the file dumped in the previous step. The config here is simple, just specify the dumped properties file name as the Properties File Path field value.
Step 3 - Invoke Ant
Here, we kick off the normal ant build. Since the environment now contains all the required definitions, the build completes as normal.
Try EnvInject Plugin.

Resources