0

I'm trying to switch from using a freestyle Jenkins build to a pipeline project.

I like many things about it, but I wish that I could use the multibranch pipeline as that matches our company a bit better, but at present that is a not an option.

What we do currently is create a new build job with the name of <project name> - <environment>.

So I need to keep that going for now. I have a basic outline of a script that I can either copy and paste into the box or even better is to use the jenkins file from scm.

I like this one the most and that is what I'm currently using on my local Jenkins.

If I hard code the solution file and the environment I want in my script in scm it builds fine.

I don't like that option because that means I'd have to have lots of scripts with similar names just changing the branch. If I add build parameters with the solution name and environment I can easily make the script handle those as well, however what I don't like is that when I click build button it confirms that those are the parameters I want to use.

So is there a way that I can hardcode/get a plugin that lets me add those parameters as constants or environment variables or whatever so it is just part of the job?

EDIT

As an update to show what I tried yesterday and got to work for our needs is this. First was that I installed multibranch defaults plugin and followed the steps outline on their github page. With that installed and configured I added a new multibranch project, pointed it to my git repository. It now found 2 branches (as expected) and used the default config file. So far this seems like it will work for about 90% of our cases. The only problem I can see is if some people had custom steps in their existing freestyle project. But for now those can always just stay a freestyle project.

1 Answer 1

3

If I understand you correctly what you're looking for is a way to supply default parameters to your build.

In one of my builds I do something like that:

stage ('Setup') {
    try {
        timeout(time: 1, unit: 'MINUTES') {
            userInput = input message: 'Configure build parameters:', ok: '', parameters: [
                [$class: 'hudson.model.ChoiceParameterDefinition', choices: 'staging\nproduction\nfree', description: 'Choose build flavor', name: 'BUILD_FLAVOR'],
                [$class: 'hudson.model.ChoiceParameterDefinition', choices: 'Debug\nRelease', description: 'Choose build type', name: 'BUILD_TYPE'],
                [$class: 'hudson.model.ChoiceParameterDefinition', choices: 'NONE\ndevelop\nmaster\nrelease/core_0.5.0\nrelease/core_0.1.8.1\nrelease/core_0.1.9', description: 'Product core branch', name: 'CORE_BRANCH'],
                [$class: 'hudson.model.ChoiceParameterDefinition', choices: '4.1.12\n4.1.11\n4.1.10\n4.1.9\n4.1.8\n4.1.4\n3.5.5\n3.1.8\ncore\nOldVersion', description: 'Version Name', name: 'VERSION_NAME'],
                [$class: 'hudson.model.ChoiceParameterDefinition', choices: 'origin/develop\norigin/hotfix/4.1.11\norigin/release/4.1.8\norigin/hotfix/4.1.7\norigin/hotfix/4.1.9\norigin/hotfix/4.1.10\norigin/release/4.1.6\norigin/release/4.1.5\norigin/hotfix/3.5.5', description: 'Git branch', name: 'GIT_BRANCH'],
                [$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Enable Gradle debug?', name: 'DEBUG']
        ] // According to Jenkins Bug: https://issues.jenkins-ci.org/browse/JENKINS-26143
      }
    } catch (err) {
        userInput = [BUILD_FLAVOR: 'staging', BUILD_TYPE: 'Debug', CORE_BRANCH: 'NONE', VERSION_NAME: '4.1.12', GIT_BRANCH: 'origin/develop'] // if an error is caught set these values
    }
}

Explanation:

I'm using the Try/Catch method to handle exceptions and then within the "try" section, I configured the question and possible answers to select from that I want to display to the user which starts the build.

Then, in the "catch" section I've put the default values I want to set in each one of the variables incase an exception is caught, which means that 1 minute has passed without selecting the relevant items.

Here are some useful links:

Pipeline: How to manage user inputs

pipeline-plugin/TUTORIAL.md

Sign up to request clarification or add additional context in comments.

5 Comments

sort of, i just dont want to ask for input. o ended up finding a plugin called default jenkinsfile...or something like that. it lets me define a default jenkins file in the core of jenkins. the multibranch then uses all branches like i want. i found that nuget and msbuild scan directory for projects and solutions. so i am going to use it
You don't need a dedicated plugin for that as Jenkins pipeline plugin gives you this functionality.
Well nothing is set in stone as of now. This was more of how I got it working according to my needs. I'll edit to show what is currently in play
This approach , delete all created parameters in job.: WARNING: The properties step will remove all JobPropertys currently configured in this job, either from the UI or from an earlier properties step.
Depending on whether or not you are using a declarative vs a scripted pipeline, there are various ways to just set env vars for later. If you're using a scripted pipeline you can set env vars specifically, with env.var_name = "some value". You can also set it via scripts or even set it to parameters with env.var_name=params.var_name. For declarative pieplines you use an environment { } block with SOME_VAR="value" or ="${params.var_name}". For either, just having SOME_VAR="value" outside the pipeline works & you can use/change that var as you see fit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.