How to override environment variables in Execute Shell

Hi everyone,

I have a Jenkins job that feeds a Mattermost chat channel, with output value returned from a Python script, after each build.

I defined an environment variable under Manage Jenkins → Configure System as following:

enter image description here

In “Execute shell”, I call my script and it returns the relevant value as below.

RET=$(python main.py)

However, I could not override the environment variable SCRIPT_RET with the output value RET above, to use in the custom message with $SCRIPT_RET

Any help is appreciated, thanks!

It’s hard to answer that question without more context. For example, can you show how you set SCRIPT_RET to the value or RET, and where you try to read the SCRIPT_RET value? That would all have to be in the same context, for example all in the same Freestyle job shell script, or in the same Pipeline Script.

All I want to do is to transfer the output value of my Python script to the post-build action (Mattermost chat hook in this case).

I read the value of SCRIPT_RET in Post-build actions as following:

The problem is that I could not override this environment variable with the value returned from my Python script that I call in the Freestyle job shell script as you see below.

URL to the img (not allowed to directly upload the 2nd image)

This simply does not recognise the environment variable, but treats it as a regular variable. As a result, SCRIPT_RET in the post-build action remains null, as I do not assign something to it at the beginning while creation.

I strongly recommend looking into swapping out to pipeline, you’ll be able to do this, and have much more freedom than a freestyle job (which is now ironically named).

That being said, If i remember my freestyle correctly, you’ll have to execute a shell script, write it to a file, and then use a different plugin to import into your envs. I’m not sure said plugin actually exists. I wrote GitHub - halkeye/jenkins-fileToEnv: Jenkins plugin a long time ago, but never polished it up to release.

just fyi, in pipeline it would be

script {
   env.SCRIPT_RET=sh(returnStdout: true, script: 'python main.py').trim()
}

That being said, you could do a conditional step (to do notify failure custom logic) and just curl to mattermost, googling says its just a simple post and have more freedom that way.

Hope that helps, my freestyle is super rusty

1 Like

I am not able to convert it to a pipeline right now, because it will require a bit time, but I will keep this on my mind when I am able to do so. Thank you for your help!

1 Like