Bash variables defined during a step

16 views
Skip to first unread message

Samuel Mutel

unread,
Mar 6, 2018, 6:53:06 AM3/6/18
to Jenkins Users
Hello,

I would like to define a variable in shell and to use it.
How can I do this?

For example:

sh "find packages -name internal-* -type d -exec bash -c 'for arg; do export VERSION=\$(grep \${arg##*/} nexus-cicd-versions.txt | cut -d: -f2);sed -i \'s/#VERSION#/${env.VERSION}/g\' \$arg/0/package.json; done' _ {} +"

The content of VERSION is the result of a grep from file. I displayed the variable, it works.
Now I would like to use VERSION in the sed part. But I am not able to get the value of VERSION.
I tried ${env.VERSION}, ${VERSION}, $VERSION, ...

Thanks for your help on this topic.

Victor Martinez

unread,
Mar 6, 2018, 11:48:28 AM3/6/18
to Jenkins Users
GString might be a bit crazy to escape some chars, maybe it's worthy to create a sh file somewhere and just call it? Then you get the benefits of being able to call/use it without the need to be in the pipeline itself. 

My two cents

Victor Martinez

unread,
Mar 6, 2018, 11:50:18 AM3/6/18
to Jenkins Users
In your case you just need to escape \$VERSION to avoid the GString to be interpolated. 

sh "find packages -name internal-* -type d -exec bash -c 'for arg; do export VERSION=\$(grep \${arg##*/} nexus-cicd-versions.txt | cut -d: -f2);sed -i \'s/#VERSION#/\${VERSION}/g\' \$arg/0/package.json; done' _ {} +"


But again, GString might be a bit tricky when escaping in the sh step within the  jenkins pipeline

Cheers

Björn Pedersen

unread,
Mar 6, 2018, 11:52:48 AM3/6/18
to Jenkins Users

So you want the $VERSION you defined in the shell:
The you need to use \$VERSION or \${VERSION} I guess.

Otherwise you would try to use the groovy variable (that probably does not even exist).

Björn
 

Samuel Mutel

unread,
Mar 6, 2018, 1:27:51 PM3/6/18
to jenkins...@googlegroups.com
Yes it is what I did and I think it is the best way. Thanks. 

Le 6 mars 2018 5:48 PM, "Victor Martinez" <victormar...@gmail.com> a écrit :
GString might be a bit crazy to escape some chars, maybe it's worthy to create a sh file somewhere and just call it? Then you get the benefits of being able to call/use it without the need to be in the pipeline itself. 

My two cents

--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/3F2ozjnSiTE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/af107496-9a0e-4dbf-89d7-32e60bc192f0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages