0

I have a pipeline script, where same methods need to be executed in different agents. I have methods defined for common functionality, however i would like to pass agent as a parameter to method and execute the function. Which basically results in error!

So, is it possible to pass agent as a method parameter and execute certain scripts inside it!?

Eg:

stage('[CD][POD] VERIFY INSTANCE HEALTH') {      
      steps{
          container(container_name_aws_lb) {
              script{

              echo "health test fails"

              startApplication("${instance1_jenkins_label}")

            }
        }
    }
}


def startApplication(String agentLabel)
{
sh "echo agent id inside method ${agentLabel}"

        agent{
            label "${agentLabel}"
        }

    sh "sudo su tip_deployment -c \'pwd\'"
    sh "sudo su tip_deployment -c \'sudo systemctl restart pics\'"

}

When i execute, it errors as below

java.lang.NoSuchMethodError: No such DSL method 'agent' found among steps [addBadge, addErrorBadge, addHtmlBadge, addInfoBadge, addShortText, addWarningBadge, archive, awaitDeploymentCompletion, awsIdentity, bat, bitbucketStatusNotify, build, catchError, cfInvalidate, cfnCreateChangeSet, cfnDelete, cfnDeleteStackSet, cfnDescribe, cfnExecuteChangeSet, cfnExports, cfnUpdate, cfnUpdateStackSet, cfnValidate, checkout, container, containerLog, createSummary, cucumberSlackSend, deleteDir, deployAPI, dir, dockerFingerprintFrom, dockerFingerprintRun, ec2ShareAmi, echo, ecrDeleteImage, ecrListImages, ecrLogin, envVarsForTool, error, fileExists, findFiles, getContext, git, input, invokeLambda, isUnix, jiraComment, jiraIssueSelector, jiraSearch, junit, library, libraryResource, listAWSAccounts, load, lock, mail,

Any guidance appreciated.

Shruthi Bhaskar
  • 1,212
  • 6
  • 20
  • 32

1 Answers1

-1

Try to split this into 2 different functions, one return the value of the agentlabel with some parameter as input. This is something you can use to select the agent label inside stage.

Then have your function separately which executes set of shell variables or some other tasks.