Jenkins returns null instead of error in output - groovy

There is my code
def build() {
try {
// some build gradle task here
// gradle install task
}
catch(Throwable err) {
// println "DEBUGGGGGGG"
globalVar.echo "TEST#"
globalVar.echo "TEST#"
globalVar.echo err.getMessage()
globalVar.echo "TEST"
globalVar.echo err.getCause()
globalVar.echo "TESTSSSS"
throw err
}
finally {
}
jenkins Output from my job:
Pipeline] echo
TEST#
[Pipeline] echo
TEST#
[Pipeline] echo
null
[Pipeline] echo
TEST
Finished: FAILURE [Pipeline] echo null [Pipeline] echo TESTSSSS
then trows Exception
hudson.model.Run$RunnerAbortedException
at org.jfrog.hudson.pipeline.common.executors.GradleExecutor.exe(GradleExecutor.java:144)
at org.jfrog.hudson.pipeline.common.executors.GradleExecutor.execute(GradleExecutor.java:69)
at org.jfrog.hudson.pipeline.scripted.steps.ArtifactoryGradleBuild$Execution.run(ArtifactoryGradleBuild.java:89)
at org.jfrog.hudson.pipeline.scripted.steps.ArtifactoryGradleBuild$Execution.run(ArtifactoryGradleBuild.java:65)
at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1$1.call(AbstractSynchronousNonBlockingStepExecution.java:47)
at hudson.security.ACL.impersonate(ACL.java:290)
at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1.run(AbstractSynchronousNonBlockingStepExecution.java:44)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
How can I get Exception cause ? in my case if i run gradle install from command line ( without jenkins ) is :
error: incompatible types: cannot be converted to boolean
but I would to see error in jenkins job console output

Try -
try{
//your code
}
catch (exc){
println exc
}
Works for me.

Related

groovy.lang.MissingPropertyException: No such property: GIT_PASSWORD for class: groovy.lang.Binding

I have checked all the topics on the same mistake, tried everything, and yet I was unable to find anything to help me solving my issue.
my code builds a docker container, starts a flask application and run a python script, run tests and then should merge Master. The code is the following :
pipeline {
agent any
stages {
stage('Master merging'){
steps{
script{
passwordVariable = 'password'
usernameVariable = 'unsername'
// Variables for input
if(env.BRANCH_NAME == 'features'||env.BRANCH_NAME == 'main'){
sh 'git checkout origin/features'
sh 'git pull'
sh 'git remote update'
sh 'git fetch'
sh 'git checkout origin/main'
sh 'git merge origin/features'
withCredentials([usernamePassword(credentialsId : 'GitHub', passwordVariable:'GIT_PASSWORD', usernameVariable:'GIT_USERNAME')]){
sh "git push http://${GIT_USERNAME}:${GIT_PASSWORD}#github.com/Username/Repo.git"
}
}}
}
}
}
}
when I arrive to stage('master merging'), I have the following mistake
Masking supported pattern matches of $GIT_PASSWORD
[Pipeline] {
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (container shutdown)
Stage "container shutdown" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: GIT_PASSWORD for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
Thanks a lot in advance for your help :)
I have an answer :)
Prerequisites : create a credential on Manage jenkins -> Manage credentials -> jenkins -> Global credentials (unrestricted) -> username with password. Inquire github username as username and Personal access token as password. ID is up to you.
Code :
stage('Master merging'){
steps{
script{
// Variables for input
if(env.BRANCH_NAME == 'features'||env.BRANCH_NAME == 'main'){
sh 'git checkout features'
sh 'git pull'
sh 'git remote update'
sh 'git fetch'
sh 'git checkout origin/main'
sh 'git merge features'
sh "git config user.email \"GITHUB EMAIL\""
sh "git config user.name \"GITHUB USERNAME\""
withCredentials([gitUsernamePassword(credentialsId:'ID you inquired in credentials')]) {
sh 'git push https://github.com/Username/Repo.git'
}
}
}
}
}

How to hide parameters such as APIKey from Jenkins pipeline console output

How can I hide certain parameters or **** them when I execute scripts inside stage.
The command that is producing the output which I want to hide is:
sh "./wsagent_execute.sh -s -apiKey ${WHITESOURCE_API_KEY} -projectToken ${WHITESOURCE_PROJECT_TOKEN} -C ${configPath} -d ${directoryPath} -logLevel info"
The parameters I want to hide are -apiKey and -projectToken. How can I do it?
If you get your credentials from a vault, you can use Mask Passwords plugin. It does not state that it supports Pipelines but actually it does.
pipeline {
agent any
stages {
stage('doing something') {
steps {
script {
def current_nano = "1616407597607795668"
sh label: "Now you see it", script: "echo ${current_nano}"
maskPasswords(varPasswordPairs: [[password: current_nano, var: 'IGNORE']]) {
sh label: "Now you don't", script: "echo ${current_nano}"
}
}
}
}
}
}
Output:
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/
[Pipeline] {
[Pipeline] stage
[Pipeline] { (doing something)
[Pipeline] script
[Pipeline] {
[Pipeline] sh (Now you see it)
+ echo 1616407597607795668
1616407597607795668
[Pipeline] maskPasswords
[Pipeline] {
[Pipeline] sh (Now you don't)
+ echo ********
********
[Pipeline] }
[Pipeline] // maskPasswords
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Are these keys calculated in the pipeline or are they statics?
You can try to use it like credentials in Jenkins and you don't see their values in the log.
If you use a secret define in Jenkins credential, Jenkins will automatically mask that for you and in the log, it will show as ****
let's say you have defined your apiKey in jenkins credential with Id: apikey. Than you can use that on your pipeline like below example.
more details can be found here
node() {
withCredentials([string(credentialsId: 'apikey', variable: 'TOKEN')]) {
sh "./wsagent_execute.sh -s -apiKey $TOKEN -projectToken ${WHITESOURCE_PROJECT_TOKEN} -C ${configPath} -d ${directoryPath} -logLevel info"
}
}
If you are not happy with that use mask password or something similar plugins

How to debug jenkins pipeline failure while Jenkins killed process

The goal: build an Angular app
My Jenkins setup: master >> slave >> node docker
I'm trying to build an Angular app on the slave via docker, The installation works well and the code runs on the docker node image.
node works well - relevant log
+ node -v
v14.4.0
[Pipeline] sh
+ npm install -g #angular/cli
npm WARN deprecated request#2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
/home/jenkins/jenkins_slave/workspace/build_app/.npm-global/bin/ng -> /home/jenkins/jenkins_slave/workspace/build_app/.npm-global/lib/node_modules/#angular/cli/bin/ng
> #angular/cli#9.1.7 postinstall /home/jenkins/jenkins_slave/workspace/build_app/.npm-global/lib/node_modules/#angular/cli
> node ./bin/postinstall/script.js
+ #angular/cli#9.1.7
updated 1 package in 10.171s
[Pipeline] sh
+ npm install
audited 1049 packages in 13.505s
found 1935 vulnerabilities (1587 low, 3 moderate, 345 high)
run `npm audit fix` to fix them, or `npm audit` for details
The problem is when I'm trying to build the app with `ng build', the angular start the build but Jenkins kills the process without throw any error
My code
stage('Deploy') {
steps {
script {
try {
echo 'Deploying....'
sh ".npm-global/bin/ng build"
} catch (err) {
echo err
currentBuild.result == 'FAILURE'
}
}
}
}
Edit: log after insert build code to try-catch
+ .npm-global/bin/ng build
Killed
[Pipeline] echo
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
$ docker stop --time=1 26fc42fc32db015d53d64735350ce16690d1fec8086708dd10391701d1c427fe
$ docker rm -f 26fc42fc32db015d53d64735350ce16690d1fec8086708dd10391701d1c427fe
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.lang.ClassCastException: org.jenkinsci.plugins.workflow.steps.EchoStep.message expects class java.lang.String but received class hudson.AbortException
at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:492)
at org.jenkinsci.plugins.structs.describable.DescribableModel.buildArguments(DescribableModel.java:409)
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:329)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:269)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:179)
at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
at sun.reflect.GeneratedMethodAccessor304.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:163)
at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:157)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:161)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:165)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
Caused: java.lang.IllegalArgumentException: Could not instantiate {message=hudson.AbortException: script returned exit code 137} for org.jenkinsci.plugins.workflow.steps.EchoStep
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:334)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:269)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:179)
at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
at sun.reflect.GeneratedMethodAccessor304.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:163)
at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:157)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:161)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:165)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
at WorkflowScript.run(WorkflowScript:52)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:86)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
at sun.reflect.GeneratedMethodAccessor153.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.LocalVariableBlock$LocalVariable.get(LocalVariableBlock.java:39)
at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
at com.cloudbees.groovy.cps.impl.LocalVariableBlock.evalLValue(LocalVariableBlock.java:28)
at com.cloudbees.groovy.cps.LValueBlock$BlockImpl.eval(LValueBlock.java:55)
at com.cloudbees.groovy.cps.LValueBlock.eval(LValueBlock.java:16)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:400)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:96)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:312)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:276)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE
There is no log for from Npm/Node/Angular
So my questions is
How to debug Jenkins pipeline failures and how to get more details?
How to see the ng build command output?
Edit 2
As #MattSchuchard suggested in the comment, I was trying to use err.toString() / err.getMessage() / err.getStackTrace() methods, but the resutlt error is the same.
I think that the error comes because Jenkins killed the process and not because an error occurred in my process.

Getting permission denied when trying to run a shell script on jenkins pipeline

I'm trying to set a Jenkins pipeline to build an Ionic app inside a Linux server (ec2 instance on amazon services) My first stage inside the jenkinsfile is to run npm install, but it returns permission denied.
I've tried setting permissions to the folder using:
chmod 777 /home/ec2-user/.nvm/versions/node/v10.16.0/bin
I've also tried adding the Jenkins user to the group that also has permissions. none of these seemed to work.
This is my Jenkinsfile
pipeline {
agent any
environment {
PATH='/usr/local/bin:/usr/bin:/bin'
}
stages {
stage('NPM Setup') {
steps { sh '/home/ec2-user/.nvm/versions/node/v10.16.0/bin/npm install' }
}
stage('Android Build') {
steps {
sh 'ionic cordova build android --release'
}
}
stage('APK Sign') {
steps {
echo "Sign Android APK Action"
}
}
stage('Zip APK') {
steps {
echo "Zip the APK Action"
}
}
}
}
I get this output
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (NPM Setup)
[Pipeline] sh
+ /home/ec2-user/.nvm/versions/node/v10.16.0/bin/npm install
/var/lib/jenkins/workspace/p-ionic4_borderapp_ionic4_master#tmp/durable-9b0ecc49/script.sh: line 1: /home/ec2-user/.nvm/versions/node/v10.16.0/bin/npm: Permission denied

UrbanCode udeploy NullPointerException in Run Groovy Script step

The same step was running fine for a couple of days and now getting this error. I've seen same error with UCD 6.1.1 too, then the code was able to run in UCD 6.2.1, but now fails again. What could be the issue?
plugin: Groovy, id: com.urbancode.air.plugin.Groovy, version: 6
plugin command: 'cmd' '/C' '"f:\apps\yyyy\agent\opt\groovy-1.8.8\bin\groovy -cp "f:\apps\yyyy\agent\var\plugins\com.urbancode.air.plugin.Groovy_6_c852a5128a51660ebc6986784c1163eb465daf6abf99d541d16108d08b14ab7e\classes;f:\apps\yyyy\agent\var\plugins\com.urbancode.air.plugin.Groovy_6_c852a5128a51660ebc6986784c1163eb465daf6abf99d541d16108d08b14ab7e\lib\plugins-util.jar" f:\apps\yyyy\agent\var\plugins\com.urbancode.air.plugin.Groovy_6_c852a5128a51660ebc6986784c1163eb465daf6abf99d541d16108d08b14ab7e\run_groovy_script.groovy F:\apps\yyyy\agent\var\temp\logs4673273741404342602\input.props F:\apps\yyyy\agent\var\temp\logs4673273741404342602\output.props"'
working directory: F:\apps\yyyy\agent\var\work\XH70_Javascripts
properties:
PLUGIN_INPUT_PROPS=F:\apps\yyyy\agent\var\temp\logs4673273741404342602\input.props
PLUGIN_OUTPUT_PROPS=F:\apps\yyyy\agent\var\temp\logs4673273741404342602\output.props
groovyHome=f:\apps\yyyy\agent\opt\groovy-1.8.8
scriptBody=import groovy.io.FileType
def currentDir = new File('.')
def files = []
currentDir.eachFile(FileType.FILES) {
if (it.name.endsWith(".zip")) {
files << it.name - "-bin.zip"
}
}
println files[0]
outProps.put("zipfilename", files[0])
environment:
AGENT_HOME=f:\apps\yyyy\agent
AH_AUTH_TOKEN=****
AH_WEB_URL=https://ucd.com:8443
AUTH_TOKEN=****
CODESTATION_URL=https://ucd.com:20081
CODESTATION_USE_PROXY=false
DS_AUTH_TOKEN=****
DS_SYSTEM_ENCODING=Cp437
JAVA_OPTS=-Dfile.encoding=Cp437 -Dconsole.encoding=Cp437
PLUGIN_HOME=f:\apps\yyyy\agent\var\plugins\com.urbancode.air.plugin.Groovy_6_c852a5128a51660ebc6986784c1163eb465daf6abf99d541d16108d08b14ab7e
PROXY_HOST=ucd.com
PROXY_PORT=20080
UD_DIALOGUE_ID=b9521caf-6399-4455-a8e4-7e79bc3c95b2
WE_ACTIVITY_ID=f819fba0-4800-4c87-9e29-5efa3698fe76
================================================================================
Running Groovy Script
command: f:\apps\yyyy\agent\opt\groovy-1.8.8\bin\groovy.bat C:\Windows\TEMP\groovy_script_8111838756530262064.groovy F:\apps\yyyy\agent\var\temp\logs4673273741404342602\input.props F:\apps\yyyy\agent\var\temp\logs4673273741404342602\output.props
null
Caught: java.lang.NullPointerException
java.lang.NullPointerException
at java_util_Map$put.call(Unknown Source)
at groovy_script_8111838756530262064.run(groovy_script_8111838756530262064.groovy:26)
Caught: com.urbancode.air.ExitCodeException: Command failed with exit code: 1
com.urbancode.air.ExitCodeException: Command failed with exit code: 1
at com.urbancode.air.CommandHelper.runCommand(CommandHelper.groovy:195)
at com.urbancode.air.CommandHelper$runCommand$0.callCurrent(Unknown Source)
at com.urbancode.air.CommandHelper.runCommand(CommandHelper.groovy:121)
at com.urbancode.air.CommandHelper$runCommand.call(Unknown Source)
at run_groovy_script.run(run_groovy_script.groovy:64)
It could be that currentDir.eachFile() isn't returning any files, so files[0] is null.

Resources