-1

I am trying to save a variable after jenkins job completes. And this variable will be overwritten after each job completion. Other jobs should be able to access this variable. Is there a way to do this in Jenkins? Any plugins that can handle this?

manasa
  • 3
  • 3
  • There are many ways for jobs to exchange data. Which one to use, depends on your use case. Can you elaborate? – zett42 Nov 23 '19 at 13:48
  • So the 2 jobs use different workspaces. I need job1 to save some variables at the end. Job 2 will need to read one of them based on build parameters. Maybe like global variables in Jenkins? or like a map variable(key, value pairs)? if that is possible. – manasa Nov 26 '19 at 19:38
  • Is job1 launched by job2? Then you could use [getBuildVariables()](https://stackoverflow.com/a/42267268/7571258). – zett42 Nov 26 '19 at 19:59

1 Answers1

0

Assume you use free-style job, you need to do following two steps:

  1. Set all jobs run in same workspace as the first job workspace via click Advance... button nearby Lable Expression, check Use custom workspaces, fill same value in Directory

enter image description here

  1. Output the variable into a file in the above directory with format: var1=xxxx at the end of each job Build stage

  2. Load the file in each job via Inject environment variables to the build process enter image description here

If you use pipeline as code, it will be more easy, because all stages in one pipeline sharing same workspace and top level environments and variables can access in all stages.

yong
  • 13,357
  • 1
  • 16
  • 27
  • Is there a way to do this if it is different workspaces? – manasa Nov 26 '19 at 19:35
  • I'm not sure it will be work, but you can give a trying on add two steps in Build section: 1) Copy artifacts from another project . 2) Inject environment variables – yong Nov 27 '19 at 02:36