0

I'm trying to set the output of certain DSL plugin commands to env. variables in Jenkins DSL and having no luck.

I tried imitating the following way of setting sh scripts output to variables:

env.BUILD_NUM = sh([script: "get_build_number_from_s3 ${env.TARGET_ENV}", returnStdout: true]).trim()

The first one I'm trying to do is the git module.

I set my command like the following:

env.DEVOPS_REPO_CLONE = git([branch: "development", credentialsId: 'bitbucket', url: 'https://bitbucket.org/team/repo.git'])
sh 'ls -lah'

but I get the following error:

WorkflowScript: 119: Expected a step @ line 119, column 13.
               env.DEVOPS_REPO_CLONE = git([branch: "development", credentialsId: 'bitbucket', url: 'https://bitbucket.org/team/repo.git'])
               ^

1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:133)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:559)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:520)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:319)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE

Basically, I'm trying to set the output of commands to variables, that way I can only show the output when DEBUG flag is set to abstract extraneous output for developers so they don't have to parse through pages of text.

Thanks for your inputs.

daspilker
  • 8,154
  • 1
  • 35
  • 49
Grant Zukel
  • 1,153
  • 2
  • 24
  • 48

1 Answers1

0

Seems to be that you need to create these variables in environment {...} or script { ... } scope. Example:

pipeline {
    environment {
        FOO = sh (
                script: 'pwd',
                returnStdout: true
              ).trim()
    }

    agent { label "master" }
    stages {
        stage("first") {
            steps {
                script {
                    BAR = sh (
                            script: 'ls',
                            returnStdout: true
                          ).trim()
                }
                sh "echo ${FOO}"
                sh "echo ${BAR}"
            }
        }
    }
}

You can use returnStdout or returnStatus options for shell scripts as it is described here and here.

biruk1230
  • 3,042
  • 4
  • 16
  • 29
  • I'm not asking about how to run shell scripts I'm asking about how to run jenkins plugin commands from the declarative syntax and mute the output. I can mute the output of my shell scripts but when I run anchore plugin there is no option to mute output, or if I run git plugin I can't mute the output unelss I convert to shell commands and run them with sh script which I don't want to do. I want to mute the plugin output – Grant Zukel Jan 04 '19 at 17:48