<p>In Groovy you have to use double quotes to get string interpolation:</p>

<pre><code>    if ( "$output" != null ) {
        slackSend (channel: "@${name}", color: '#36A64F', message: "Job succeeded")
    } else {
        slackSend (channel: "@${name}", color: '#36A64F', message: "Job failed")
    }
</code></pre>

<p>You can also probably do something like this to avoid it entirely and have less duplication:</p>

<pre><code>name = '@' + readFile('name.txt')

slackSend (channel: name //etc.
</code></pre>

CC BY-SA 3.0
2017-05-09T03:11:18.963Z
2017-05-09T03:11:18.963Z
Michael Pereira
<p>This is probably 100% user error, but I can't get a pipeline job to use a variable as the channel name in a slackSend step. </p>

<p>This uses both the <a href="https://github.com/jenkinsci/slack-plugin" rel="noreferrer">Slack plugin</a> and the <a href="https://wiki.jenkins-ci.org/display/JENKINS/Build+User+Vars+Plugin" rel="noreferrer">Build User Vars Plugin</a>. I'm trying to use <code>BUILD_USER_FIRST_NAME</code> to identify which channel to Slack back to (ie, the one starting the build should be the one to get the message). </p>

<p>Got halfway there with <a href="https://support.cloudbees.com/hc/en-us/articles/226287607-How-to-use-Build-User-Vars-Plugin-with-Pipeline" rel="noreferrer">this article</a> on how to use the build user plugin in a pipeline job, but substituting the variable is tripping me up. </p>

<p>The relevant part of the job looks like this;</p>

<pre><code>stage ('message') {

    wrap([$class: 'BuildUser']) {

        sh "printf '%s' ${BUILD_USER_FIRST_NAME} &gt; name.txt"

    }

    archiveArtifacts artifacts: 'name.txt'
    name = readFile('name.txt')
    echo "name is $name" //works fine, I get Alex

    if ( "$output" != null ) {
        slackSend (channel: '@$name', color: '#36A64F', message: "Job succeeded")
    } else {
        slackSend (channel: '@$name', color: '#36A64F', message: "Job failed")
    }

}
</code></pre>

<p>I've tried the following in place of the channel bit, all have failed with indeterminate <code>Slack post may have failed. Response: Invalid channel specified</code> errors.</p>

<pre><code>channel: '@${name}',
channel: '@" + ${name} + "',
channel: '@"${name}"',
</code></pre>

<p>Expected output is that it would resolve to <code>channel: '@Alex',</code></p>

<p>What's the proper syntax?</p>

<jenkins><jenkins-plugins><jenkins-pipeline><slack>
Jenkins Pipeline job; proper quoting for slackSend step
Alex
# Jenkins Pipeline job; proper quoting for slackSend step

(Asked by: username_0 on 2017-05-08)

This is probably 100% user error, but I can't get a pipeline job to use a variable as the channel name in a slackSend step. 
This uses both the Slack plugin and the Build User Vars Plugin. I'm trying to use BUILD_USER_FIRST_NAME to identify which channel to Slack back to (ie, the one starting the build should be the one to get the message). 
Got halfway there with this article on how to use the build user plugin in a pipeline job, but substituting the variable is tripping me up. 
The relevant part of the job looks like this;
stage ('message') {

    wrap([$class: 'BuildUser']) {

        sh "printf '%s' ${BUILD_USER_FIRST_NAME} > name.txt"

    }

    archiveArtifacts artifacts: 'name.txt'
    name = readFile('name.txt')
    echo "name is $name" //works fine, I get username_0

    if ( "$output" != null ) {
        slackSend (channel: '@$name', color: '#36A64F', message: "Job succeeded")
    } else {
        slackSend (channel: '@$name', color: '#36A64F', message: "Job failed")
    }

}

I've tried the following in place of the channel bit, all have failed with indeterminate Slack post may have failed. Response: Invalid channel specified errors.
channel: '@${name}',
channel: '@" + ${name} + "',
channel: '@"${name}"',

Expected output is that it would resolve to channel: '@username_0',
What's the proper syntax?

**Tags:** jenkins, jenkins-plugins, jenkins-pipeline, slack


---

## 1 answers

### Answer by username_1 on 2017-05-09. Score: 6 | Accepted answer

In Groovy you have to use double quotes to get string interpolation:
    if ( "$output" != null ) {
        slackSend (channel: "@${name}", color: '#36A64F', message: "Job succeeded")
    } else {
        slackSend (channel: "@${name}", color: '#36A64F', message: "Job failed")
    }

You can also probably do something like this to avoid it entirely and have less duplication:
name = '@' + readFile('name.txt')

slackSend (channel: name //etc.


---

