1

I tried so many ways to store a value in jenkins. But I could not assign a value into a variable in Shell script of Jenkins.

First I have set a ENV variable called branch which has the value is */release/hotfix

So I tried to spliit that word and save in new variable like this.

VALUE=$($branch | sed -e 's/\*\/.*\///g') 

I tried so many experiment to save the value. It won't work.

In my research, I found a way to assign a value to variable but the plugin ( How to set environment variables in Jenkins? ) may not be safe to use.

why cant i save value to variable in Jenkins shell? How do I do that?

Sarasa Gunawardhana
  • 1,099
  • 3
  • 14
  • 34
  • 1
    My observation, when you are running multiple shell commands in Jenkins, every line executes on a different shell. So, if you want to store the value of some variable and use them, then you have to run them as the same single line command(ugly way). – Vaibhav Jain Feb 22 '19 at 10:12
  • ohhh undestood dude. so thats why I could not access the value inside variable . @VaibhavJain – Sarasa Gunawardhana Feb 22 '19 at 10:24

1 Answers1

2

I found the way to achieve this problem.

first I create a file using touch command

touch enviroments.cfg

Then save value into variable while saving to that file

echo "export VALUE=$(echo $branch | sed -e 's/\*\/.*\///g')" >> enviroments.cfg

to access the that saved variable again, I reopened that file and get that value like this

. './enviroments.cfg' (you can not use **source** command to read, use **dot**(.) instead of **source** command)

echo $VALUE

You can read values anywhere like this.

Sarasa Gunawardhana
  • 1,099
  • 3
  • 14
  • 34