mattermostSend using withCredentials

I have a groovy script that sends Mattermost messages to one of our MM Channels after a Jenkins run has completed with an error. It’s setup like this and works fine atm:

             pipelineContext.mattermostSend (
                channel: '#my-channel',
                endpoint: "<MM webhook URL>",
                text: "Build Failure: ${pipelineContext.env.JOB_NAME} ${pipelineContext.env.BUILD_NUMBER} (<${pipelineContext.env.BUILD_URL}|Open>)",
                message: "Build Failure: ${pipelineContext.env.JOB_NAME} ${pipelineContext.env.BUILD_NUMBER} (<${pipelineContext.env.BUILD_URL}|Open>)"
            )

Recently, it was decided that the endpoint should be in the Jenkins credentials configuration for the project as a Secret Text. I’ve set the matterMost send to now be the following:

			final String CREDENTIAL_ID = 'mm-credential'
			
			pipelineContext.withCredentials([pipelineContext.string(credentialsId: 'CREDENTIAL_ID', variable: 'CREDENTIAL')]) {
				pipelineContext.mattermostSend (
					channel: '#my-channel',
					endpoint: "$CREDENTIAL",
					text: "Build Failure: ${pipelineContext.env.JOB_NAME} ${pipelineContext.env.BUILD_NUMBER} (<${pipelineContext.env.BUILD_URL}|Open>)",
					message: "Build Failure: ${pipelineContext.env.JOB_NAME} ${pipelineContext.env.BUILD_NUMBER} (<${pipelineContext.env.BUILD_URL}|Open>)"
				)
			}

However I keep getting errors now when I try to pass in the new credential to the message: Mattermost Send Pipeline step configured values from global config - connector: false, icon: true, channel: false, color: trueERROR: Mattermost notification failed. See Jenkins logs for details.

I’ve tried tinkering with how to pass in the CREDENTIAL value for awhile but can’t seem to get a working MatterMost call to go through. Unfortunately I don’t have access to the jenkins logs atm so I wanted to see if anyone had any ideas what could be the problem?

What happens if you use credentialsId: 'mm-credential' directly?

I still get the same error.

Hello @vince92079 and welcome to this community :wave:

Would it be possible to ask your administrator to get the logs?
I know nothing about the plugins you’re using, but why is endpoint "$CREDENTIAL" and not "${CREDENTIAL}"?
Same thing about 'CREDENTIAL_ID'? Shouldn’t it be '$CREDENTIAL_ID' or '${CREDENTIAL_ID}'?

My $0.02.

I want to say groovy supports both, but thats a very good point, the second one is more explicit

single quotes pass the string directly through, so it would be literally $CREDENTIALS_ID ${CREDENTIALS_ID}, which in this case would be not right at all. Double quotes, groovy/pipeline processes it first, before using it. Which for shell statements is often not desired, but in this case would be right

Totally second logs though, its just random guesses at this point

1 Like

Yeah I’m currently fighting the admins to get at the logs and I completely agree I need access to those. I thought that maybe there was some simple mistake I had missed with these variables that someone with more Groovy and Jenkins knowledge would point out (I’m mostly a Java dev that got handed this so I’m trying to ramp up as fast as I can).

Everything I’m doing with the code is just taken verbatim from the Jenkins Mattermost Plugin Documentation. I’ve tried changing the CREDENTIAL_ID value every way I can think of (${CREDENTIAL_ID}, '${CREDENTIAL_ID}', etc... before I posted and I still get the same generic error in the logs.

So it sounds like there’s something I’m not aware of that the log will hopefully explain.

1 Like