1
votes

I am trying to read a file inside jenkins pipeline.

#!groovy
// pipeline config
def javaAgent = 'build && java'

def environment = "test"
def machineName = "dev1"

@Library(['my-lib@feature/jenkins-pipeline-for-index-creation']) _

// pipeline
node(javaAgent) {
properties([
        [$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator',daysToKeepStr: '1', numToKeepStr: '4']],
        parameters([            
            choice(
                choices: ["test","production"].join("\n"),
                defaultValue: 'test',
                description: 'Env for Index deployment',
                name: 'environment'
            )
        ])
    ])
    
    try{
        stage('Collect info') {
                checkout scm
        }
        stage('Creating Indexes'){
        
            environment=params.environment
            if(environment=="production"){
                machineName = "prod1"
            }   
            
            myVar.createIndexes environment: environment, 
                    deployToMachine: machineName        

        }

    } catch (e){
            pcSlack.notify channel:"ksr", message: ${environment}+" Couchbase index creation failed : "+e
            currentBuild.result = 'FAILURE' 
    }   
}

For that, i have defined below function in a library which I have imported in my jenkins file.

    void createIndexes(Map args) {
      String files = sh script: "ls -lart", returnStdout: true
      echo "workspace files: ${files}"
      def filePath = readFile('./indexes/test.txt')                 
      def queries = filePath.readLines()   
      //To-do Add code for reading queries
    }

However, i keep getting below exception.

[Pipeline] Start of Pipeline
[Pipeline] node
Running on build-java-1 in /home/jenkins/agent/workspace/..
[Pipeline] {
[Pipeline] properties
[Pipeline] stage
[Pipeline] { (Collect info)
[Pipeline] checkout
Fetching changes from the remote Git repository
..
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Creating Indexes)
[Pipeline] sh
+ ls -lart
[Pipeline] echo
workspace files: total 96
drwxr-xr-x 4 jenkins jenkins 4096 Jan 24 11:06 ..
..
drwxr-xr-x 2 jenkins jenkins 4096 Jan 24 11:07 indexes
..
drwxr-xr-x 8 jenkins jenkins 4096 Jan 24 11:16 .git

[Pipeline] readFile
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
Terminated
[Pipeline] // node
[Pipeline] End of Pipeline
[Office365connector] No webhooks to notify
java.lang.NoSuchMethodError: No such DSL method '$' found among steps [ArtifactoryGradleBuild, MavenDescriptorStep, addBadge, addErrorBadge, addHtmlBadge, addInfoBadge, addInteractivePromotion, addShortText, addWarningBadge, ansiColor, ansiblePlaybook, ansibleVault, archive, artifactoryDistributeBuild, artifactoryDownload, artifactoryMavenBuild, artifactoryPromoteBuild, artifactoryUpload, bat, bitbucketStatusNotify, build, catchError, checkout, collectEnv, conanAddRemote, conanAddUser, createSummary, deleteDir, deployArtifacts, dir, dockerFingerprintFrom, dockerFingerprintRun, dockerPullStep, dockerPushStep, dockerPushWithProxyStep, echo, emailext, emailextrecipients, envVarsForTool, error, fileExists, findFiles, getArtifactoryServer, getContext, git, httpRequest, initConanClient, input, isUnix, jiraComment, jiraIssueSelector, jiraSearch, junit, library, libraryResource, load, loadRunnerTest, lock, mail, milestone, neoloadRefreshTrends, neoloadRun, newArtifactoryServer, newBuildInfo, newGradleBuild, newMavenBuild, node, nodesByLabel, office365ConnectorSend, parallel, powershell, properties, publishBuildInfo, publishHTML, pwd, pybat, pysh, readFile, readJSON, readManifest, readMavenPom, readProperties, readTrusted, readYaml, removeBadges, removeHtmlBadges, removeSummaries, resolveScm, retry, runConanCommand, runLoadRunnerScript, script, sh, sha1, slackSend, sleep, sseBuildAndPublish, sshagent, stage, stash, step, svChangeModeStep, svDeployStep, svExportStep, svUndeployStep, svn, task, tee, timeout, timestamps, tm, tool, touch, uftScenarioLoad, unarchive, unstash, unzip, validateDeclarativePipeline, waitForQualityGate, waitUntil, withContext, withCredentials, withDockerContainer, withDockerRegistry, withDockerServer, withEnv, withMaven, withPythonEnv, wrap, writeFile, writeJSON, writeMavenPom, writeYaml, ws, xrayScanBuild, xunit, zip] or symbols [AUnit, BoostTest, CTest, CUnit, Check, CppTest, CppUnit, Custom, FPCUnit, GoogleTest, JUnit, MSTest, MbUnit, NUnit2, NUnit3, PHPUnit, QtTest, UftScenarioLoad, UnitTest, Valgrind, absolute, addALMOctaneSonarQubeListener, all, allOf, always, androidLint, ant, antFromApache, antOutcome, antTarget, any, anyOf, apiToken, aqua, architecture, archiveArtifacts, artifactManager, artifactsPublisher, authorizationMatrix, batchFile, bitbucket, bitbucketPush, bitbucketpr, booleanParam, branch, brokenBuildSuspects, brokenTestsSuspects, buildButton, buildDiscarder, buildParameter, buildingTag, bzt, caseInsensitive, caseSensitive, certificate, changeRequest, changelog, changeset, checkoutToSubdirectory, checkstyle, choice, choiceParam, cleanWs, clock, cloud, cobertura, command, concordionPublisher, configFile, configFileProvider, convertTestsToRun, copyArtifactPermission, copyArtifacts, credentials, cron, crumb, cucumber, culprits, defaultView, demand, dependenciesFingerprintPublisher, deployLambda, developers, disableConcurrentBuilds, disableResume, docker, dockerCert, dockerfile, downloadSettings, downstream, dry, dumb, durabilityHint, embUnit, envVars, environment, equals, eventSourceLambda, expression, extendedChoice, failed, file, fileCopyOperation, fileCreateOperation, fileDeleteOperation, fileDownloadOperation, fileJoinOperation, fileOperations, fileParam, filePath, filePropertiesToJsonOperation, fileRenameOperation, fileTransformOperation, fileUnTarOperation, fileUnZipOperation, fileZipOperation, findbugs, findbugsPublisher, fingerprint, folderCopyOperation, folderCreateOperation, folderDeleteOperation, folderRenameOperation, frameOptions, freeStyle, freeStyleJob, fromScm, fromSource, git, gitParameter, github, githubPush, globalConfigFiles, go, gradle, gtester, headRegexFilter, headWildcardFilter, healthAnalyzer, hyperlink, hyperlinkToModels, inheriting, inheritingGlobal, installSource, invokeLambda, invokerPublisher, isRestartedRun, jacoco, jacocoPublisher, jdk, jdkInstaller, jgit, jgitapache, jgivenPublisher, jnlp, jobDsl, jobName, junitPublisher, label, lastCompleted, lastDuration, lastFailure, lastGrantedAuthorities, lastStable, lastSuccess, lastSuccessful, latestSavedBuild, legacy, legacySCM, list, loadRunnerTest, local, location, logRotator, loggedInUsersCanDoAnything, masterBuild, maven, maven3Mojos, mavenErrors, mavenLinkerPublisher, mavenMojos, mavenWarnings, modernSCM, msbuild, msbuildError, msbuildWarning, myView, newContainerPerStage, node, nodeProperties, nonInheriting, none, not, notifyBitbucket, notifyStash, nunit, office365ConnectorSend, office365ConnectorWebhooks, openTasksPublisher, overrideIndexTriggers, paneStatus, parallelsAlwaysFailFast, parameters, passed, password, pattern, pcBuild, perfReport, performanceReport, performanceTest, permalink, permanent, pipeline-model, pipelineGraphPublisher, pipelineMaven, pipelineTriggers, plainText, plugin, pmd, pollSCM, preserveStashes, previous, projectNamingStrategy, proxy, publishGherkinResults, publishLambda, queueItemAuthenticator, quietPeriod, recipients, relative, requestor, run, runFromAlmBuilder, runLoadRunnerScript, runParam, schedule, scmRetryCount, scriptApprovalLink, search, security, shell, skipDefaultCheckout, skipStagesAfterUnstable, skipped, slave, snapshotDependencies, sourceRegexFilter, sourceWildcardFilter, specific, spotbugsPublisher, sseBuild, sshUserPrivateKey, stackTrace, standard, status, string, stringParam, swapSpace, tag, testCase, text, textParam, tmpSpace, toolLocation, triggeredBy, unsecured, uploadResultToALM, upstream, upstreamDevelopers, userSeed, usernameColonPassword, usernamePassword, veracode, veracodeDynamicRescan, viewsTabBar, weather, withAnt, withSonarQubeEnv, workspace, xUnitDotNet, zfs, zip] or globals [Artifactory, Constants, acceptanceTest, artifactoryUtils, assertions, bdd, bitbucketUtils, clair, currentBuild, docker, dockerUtils, env, fileLoader, giantswarm, gitUtils, gmpUtils, gradle, jenkinsUtils, kubectlUtils, manager, metadata, mvnUtils, npmUtils, params, pcArtifactory, pcBase, pcCouchbase, pcCouchbaseServer, pcK8s, pcPromote, pcSlack, pcapiBase, pcapiK8s, pcapiPromote, pipeline, pipelineUtils, scm, scp, serviceUtils, slackUtils, slaves, sonar, ssh, terminal, unixTimestamp, utils, veracodeUtils, xray]
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:199)
    at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
    at sun.reflect.GeneratedMethodAccessor392.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:158)
    at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:155)
    at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:156)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:160)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:130)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
    at WorkflowScript.run(WorkflowScript:62)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)
    at sun.reflect.GeneratedMethodAccessor391.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.ClosureBlock.eval(ClosureBlock.java:46)
    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$101(SandboxContinuable.java:34)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.lambda$run0$0(SandboxContinuable.java:59)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:136)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:58)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:182)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:332)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:83)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:244)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:232)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
    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

Further, if i remove the readFile function call, my jenkins pipeline executes successfully.

Any idea what is the issue here?

1
Can you share your jenkinsfile script?dot
@dot its a very simple file. Will share it shortly. It has library imports. Then node->stage->and a function call.Manish Bansal
@dot Added jenkins file as well.Manish Bansal
I could execute it and read the contents of the file in ./indexes/file.txt. Is there any code that's missing? DId you have something logic after queries statement?dot
As of now no logic. However, the global var in which this function is written has many functions which executes successfully in other pipelines. Since it is a big library, i could see various vars having multiple functions using readFile function without any issue.Manish Bansal

1 Answers

2
votes

The slack statement in catch block has wrong syntax for string concatenation, ${environment} should either be wrapped in double quotes (") or ${} removed to fix the issue:

pcSlack.notify channel:"ksr", message: "${environment}"+" Couchbase index creation failed : "+e
pcSlack.notify channel:"ksr", message: environment+" Couchbase index creation failed : "+e

Or all in a single string:

pcSlack.notify channel:"ksr", message: "${environment} Couchbase index creation failed : ${e}"