numb3rs1x
numb3rs1x

Reputation: 5193

Capture and send output from sh method in a Jenkinsfile

I have a Jenkinsfile that is running a shell command and I want to send the output of that sh method as the message: of the slackSend method.

I have the following so far, but the message to the slack channel is empty. I'm not certain how to capture the output in a way that I can reference it in the message section:

node {

        checkout scm

        stage 'run shell command'

        def shell_command =  sh "ls -l"
        def shell_output = apply_cluster

        stage 'notify slack-notification'
        slackSend channel: '#slack-notifications', color: 'good', message:     shell_output, teamDomain: 'company', token: env.SLACK_TOKEN
}

Upvotes: 2

Views: 2213

Answers (1)

numb3rs1x
numb3rs1x

Reputation: 5193

After a little more research, I discovered a couple of things I did not initially understand: The first is that even with defining a function in a Jenkinsfile, if it's a DSL method like sh, it will still get executed, so the second function I defined isn't necessary. Second, if you want to return stdout, you have to specify that as part of the command. The new code looks like this:

node {

        checkout scm

        stage 'run shell command'

        def shell_command =  sh script: "ls -l", returnStdout: true

        stage 'notify slack-notification'
        slackSend channel: '#slack-notifications', color: 'good', message: shell_command, teamDomain: 'company', token: env.SLACK_TOKEN
}

Upvotes: 3

Related Questions