NealR
NealR

Reputation: 10689

Get environment variable from previous build

I'm using the EnvInject Jenkins plugin to set an environment variable during a build step in our deployment pipeline. The next project (according to the order) is a manual step to deploy.I need to access an environment variable set in the build step.

According to the console output I'm doing that properly:

[EnvInject] - Executing scripts and injecting environment variables after the SCM step.
[EnvInject] - Injecting as environment variables the properties content 
FOR=%%i IN ("%NewFile%") DO ( SET FileName=%%~ni )
FILE_NAME=%FileName%

[EnvInject] - Variables injected successfully.

However in my next step, where I need to access the $FILE_NAME variable in order to specify the correct folder to scp from I get the following output:

[SCP] No file(s) found: releases\$FILE_NAME\*

Is there a setting I'm missing or am I attempting to access the variable incorrectly?

I see this option below - "Inject Environment variable" - however this is the same step I used previously to create the $FILE_NAME variable. If this is the correct step... how do I use it to get the variable? :)

EDIT

By clicking the options below...

enter image description here

...I'm able to get the following console output when the project builds.

 [EnvInject] - Loading node environment variables.
 [EnvInject] - Preparing an environment for the build.
 [EnvInject] - Keeping Jenkins system variables.
 [EnvInject] - Keeping Jenkins build variables.
 [EnvInject] - Injecting contributions.
 [EnvInject] - Unset unresolved 'USERNAME' variable.

However I'm still unable to access $FILE_NAME. Below is the option I've selected to set the variable in the previous build step.

enter image description here

And here is the script that goes in the "Properties Content":

FOR /F "delims=|" %%I IN ('DIR "C:\Jenkins\workspace\Project\releases\*.nupkg" /B /O:D') DO SET NewFile=%%I
FOR %%i IN ("%NewFile%") DO ( SET FileName=%%~ni )
FILE_NAME = %FileName% 

Upvotes: 2

Views: 2613

Answers (1)

NealR
NealR

Reputation: 10689

In a previous build step I look for the most recent file name. In that step I simply output the current version of the file I'm working with like so:

@echo FILE_NAME=%FileName%>CurrentVersion.txt

Then I add an "Inject Environment Variables" build step and point to the output file. This adds the environment variable as I was hoping for.

enter image description here

Upvotes: 2

Related Questions