How to do this Jenkins conditional build? - jenkins

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
)

Related

Jenkins Extended Choice Parameter associated to multiple environments variable

I am using in a Test Jenkins configuration the Choice Parameter in order to select the target environment against the tests are running.
Like this:
I use the selected value to pass to the maven test run as selected profile: -Dprofile=${TargetEnv}.
I would like to extend the implementation to make some additional git merge operation before the repository is build and the tests are triggered to run (I don't want to go into detail).
My question is how can I use the "Extended Choice Parameter" Jenkin plugin to have multiple environment variable set when a each choice value.
Example: In case the 'dev' is select then I would like to have two environment variables: targetEnv: qa and lowerEnvBranchName: develop-dev.
Somebody know how is possible to specify this kind of variables?

How to set the Jenkins build name based on some conditions

I wish to set the build name based on some condition.
Eg:
if the branch name( input parameter)= development
then build name= development
if the branch name = master then build name= master.
I can able to set the build name by using build name setter plugin but i need this based on condition.
Assuming that you use Free Style jobs, then you can use the Groovy plugin to execute arbitrary groovy code which has access to the Java structures for the build and Jenkins.
Simply add an Execute system Groovy scriptstep and enter your code which determines the name, then use build.setDisplayName() to set the name.
So similar to your example, here is a name setter which sets the name depending on the value of build parameter branch_name:
if (build.buildVariableResolver.resolve("branch_name").equals('master')) {
build.setDisplayName("Master build #${build.getNumber()}")
} else if (build.buildVariableResolver.resolve("branch_name").equals('development')) {
build.setDisplayName("Development build #${build.getNumber()}")
} else {
build.setDisplayName("Other build #${build.getNumber()}")
}
if the branch name( input parameter) = development then build name=
development
Assuming you are providing the branch name as a parameter and want to set the build name as the same using the name setter plugin, you should be able to configure it.
You can use the same parameter name as environment variable in the setter plugin as ${ENV,var="my-special-branch"}
If you want to set build name to a job from a parameter, you can use
currentBuild.displayName = "${nameOfYourParameter}"
Job Configuration
Buil job with parameters
Build History
Ref: How to set build name in Pipeline job?

Use TFBuild variables in steps in TFS 2015

I need use the $(Rev) property in an argument passed to MSBuild as part of my build process. I have defined MajorVersion, MinorVersion and PatchVersion as variables in my build definition, and I can use these in my arguments. If I pass an argument to MSBuild that contains this string: $(MajorVersion).$(MinorVersion).$(PatchVersion)-beta$(Rev:r) it evaluates to something like 1.3.2-beta$(Rev:r), which causes problems. If I pass this argument: $(MajorVersion).$(MinorVersion).$(PatchVersion)-beta$(Build.BuildId) then it correctly resolves to something like 1.3.2-beta204. I also in time would like to replace the SemVer tag with a variable, e.g. beta, rc, etc., so I can adopt a git-flow approach to my builds and versioning.
Any ideas why $(Rev) isn't usable?
Don't confuse build number format tokens with build variables.
$(Rev:.r) is a one of the tokens, resolved by the TFS build in runtime. You were correct to pass the resolved variable which encapsulates the build number format.
Alternatively, you can use $(Build.BuildNumber) in MSBuild with build number format 1.3.2-beta$(Rev:.r)

i want to make generic parameters in jenkins

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.

The current build parameters/ENV Variables in different build steps

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

Resources