Is it possible to define a new build parameter/ENV variable in a build step so it was available in the next one?
Let's say I have 2 different "Execute shell" steps and want in the second step access the variable defined in the first one.
PS: the value for the variable is set in runtime - read from 3rd party resource, so I cannot harcode it, thus need to set it from the shell script.
The plugin EnvInject will do that for you.
It can be configured as pre-SCM step or as build steps. Put it in between your two existing build steps.
Update
In your case, it may be easier to just read the value of the "3rd party" file as part of your second build step:
var=$(<3rdpartyfile.txt)
After the above line, the contents of your 3rdpartyfile.txt will be available in environment variable var. You can now use $var as you would any other variable
You can also use something like
stage('stage-1') {
steps {
script{
env.variable = ${value};
}
}
}
Now you can use the variable env.variable throughout the pipeline
Related
I want to pass variable between release tasks. (from triggered build to script)
Files:
Script1: (saves a env variable)
Write-Output ("##vso[task.setvariable variable=MyVar;]$MyVarValue")
Script2: (prints the env variable value)
Write-host $env:MyVar
Script3: (same as Script2)
Write-host $env:MyVar
First approach: build
MyBuild:
Script1
Script2
This is working properly, the second script writes the value of $env:MyVar created in the first.
Second Approach: release
- MyRelease:
Script1
Script2
Also works properly.
My problem comes when my release changes to:
MyRelease:
Triggered_build (MyBuild)
Script3
In that last case, the Script3 is not printing the $env:MyVar, so I guess that release uses a different environment than the used for the triggered build?
Is there a way to do something like that?
No way to do that directly with TFS.
I have read about variable groups, but is not possible to set the variables dynamically in execution time with a script.
The only solution found is using the plugin Variable (de|re)Hydration Tasks
Solution from: Is possible to pass a variable from a Build to a Release in TFS 2017?
Is there any plugin available for Jenkins which provides a key-value store option for Jenkins?
The plugin which's functionality is close to that is the credentials plugin.
The goal is to have a plugin which stores global configuration parameters and this parameters are available to Jenkins jobs.
Go to Manage jenkins -> Configure System -> Global Properties -> Environment Variables:
Check the box and Click on ADD
Enter Key-value and Save
To access the variable simply ${<Your-key>}
Could the enthronement variables fit your need?
They are like regular shell variable.
If you are using the pipeline you can define it this way:
environment {
VAR = 'your_value'
}
and use it later in your build.
This is explained there: https://jenkins.io/doc/pipeline/tour/environment/
If you are writing your pipeline from the UI, you can add a 'source' step in your build step.
source your_environnement_setting
test='Hello'
And then the variables can simply be used like any shell var:
echo $test
If you have variables that you do not know in advance, but you know when you are triggering your job, you can also use the parametrized plugin:
https://wiki.jenkins.io/display/JENKINS/Parameterized+Build
We are a .net shop so use Jenkins MSBuild plugin to do the build. For each project we have a development and release build, but I notice their only differences are:
/p:Configuration=Debug or /p:Configuration=Release in MSBuild
Release build has an extra copy step at the end
So I am thinking if I can merge them together into one build, and change the above configurations in the build based on the git branch names. I can get the git branch name like this %GIT_BRANCH%= origin/development, but I can't figure out:
How to construct a conditional step to use the above %GIT_BRANCH%.
How to use the above %GIT_BRANCH% to change the /p:Configuration=?
Can anyone shed me some lights?
I assume that, in merging these into a single job, you're expecting to parameterize the build (e.g., defining a Choice parameter named "Type" with values of "Development" and "Release"). If so, you could check "Prepare an environment for the run", then add something like the following to the "Evaluated Groovy script" field:
if ("Release".equals(Type)) {
def map = [Configuration: "Release"]
return map
}
if ("Development".equals(Type)) {
def map = [Configuration: "Debug"]
return map
}
This will define a Configuration environment variable with the specified value, which can then be passed to MSBuild as a ${Configuration} or %Configuration% environment variable (e.g., "/p:Configuration=${Configuration}" as the Command Line Argument).
If you have an additional build step that should only execute conditionally (if the Type is "Release"), you can use the Conditional BuildStep Plugin, in which case you can use a Regular Expression Match, with the Expression "^Release$" and the Label ${ENV,var="Type"} to access the value of the "Type" environment variable and compare it to the "^Release$" regex pattern. You would then do your copy step as the step to execute if the condition is met.
Alternatively, and more simply, you can use IF logic in a Windows batch file, such as this:
IF "%Type%" EQU "Release"
(
rem Copy your files wherever
)
I'm using TFS2015 update 1. I want to pass information from one build step to the next, how is this possible?
That seems like a very simple task, but I can't figure out how that's suppose to work. Passing a variable to a build step is easy, but passing information from one step to the next seems to be impossible. I hope I'm wrong.
You can call the task.setvariable Logging Command, which sets a variable in the variable service of taskcontext. The first task can set a variable, and following tasks are able to use the variable. The variable is exposed to the following tasks as an environment variable.
Example:
##vso[task.setvariable variable=testvar;]testvalue
How can I make generic parameters in jenkins that will be updated automatically for example I want to be able to create a parameter which hold today's date and it will be update automatically and not manually ?
thanks!
You might want to use EnvInject.
Setup your job and then add a build step "Inject envornment variables" from a file with an absolute path or the path relative to current job's workspace, which will contain something like:
DATE_VARIABLE="20150708"
OTHER_OPTIONAL_VARIABLE="value"
In previous execute shell step you may for example do:
echo "DATE_VARIABLE="`date +"%d%m%Y"` > env_inject.txt
After all that you don't need Parameters for a build, since you will have the needed parameter injected as an environment variable.