Jenkins parameterized build

Directory

Foreword:

The following is the official introduction of Jenkins:

First create a free-style job, and then check “This project is parameterized”, as shown below:

Tick parameter build

choice parameterized:

options:

Use parameters:

Add build step

Windows build script

test verification


Foreword:

Many times we need to execute builds according to different conditions, such as executing sit, uat, and prd environment builds in automated testing. Jenkins supports parameterized builds.

The following is the official introduction of Jenkins:

Parameters allow you to prompt the user for one or more inputs that will be passed into the build. For example, you might have a project that allows users to upload a zip file containing the binaries to test, thus running tests on demand. This can be done by adding a file parameter here.
Or, you might have a project that releases some software, and you want users to enter release notes that will be uploaded with the software. This can be done by adding a multiline string parameter here.
Each parameter has a name and some kind of value, depending on the parameter type. When the build starts, these name-value pairs are exported as environment variables, allowing subsequent parts of the build configuration (such as build steps) to access these values, for example using the ${PARAMETER_name} syntax (or %PARAMETER_NAME% on Windows).
This also means that each parameter defined here should have a unique name.

When a project is parameterized, the usual “Build Now” link will be replaced by a “Build with Parameters” link where the user will be prompted to specify a value for each defined parameter. If they choose not to enter anything, the build will start with the default values for each parameter.

If the build is started automatically, e.g. if started by an SCM trigger, the default values for each parameter will be used.

When a parameterized build is queued, an attempt to start another build of the same project will only succeed if the parameter value is different, or if the “Execute concurrent builds if necessary” option is enabled.

See the parameterized build documentation for details on this feature.

First create a free-style job, and then check “This project is parameterized”, as shown in the figure below :

The above figure is constructed using choice parameterized parameters to simulate different test environments

Check parameter construction

choice parameterized:

Define a simple list of strings for the user to choose from. , which you can use at build time, or as an environment variable, or as variable substitution in configuration.

Options:

Alternate options, one per line. The first line will be used as the default option.

Other parameters are similar

Using parameters:

Linux uses the ${PARAMETER_name} syntax (or %PARAMETER_NAME% on Windows).

Add Build Step

This article uses the windows system to build

Because you need to judge different conditions, you need to use conditional judgment. Since some students are not very familiar with batch processing commands, the usage method of the if command is attached:

C:\Users\Administrator>help if
Perform conditional processing in batch programs.

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command

  NOT specifies that Windows should only
                    The command should be executed.

  ERRORLEVEL number If the last program run returned a value equal to or greater than
                    Specifies the numeric exit code for which the specified condition is true.

  string1==string2 Specifies that the condition is true if the specified literal string matches.

  EXIST filename The specified condition is true if the specified filename exists.

  command Specifies the command to execute if the condition is met. if specified
                    If the condition is FALSE, the command can be followed by an ELSE command, which will
                    Execute the command after the ELSE keyword.

The ELSE clause must appear after the IF on the same line. For example:

    IF EXIST filename. (
        del filename.
    ) ELSE (
        echo filename. missing.
    )

Since the del command needs to be terminated with a new line, the following clauses will not be valid:

IF EXIST filename. del filename. ELSE echo filename. missing

Since the ELSE command must be on the same line as the end of the IF command, the following clauses also
will not work:

    IF EXIST filename. del filename.
    ELSE echo filename.missing

The following clauses are valid if all placed on the same line:

    IF EXIST filename. (del filename.) ELSE echo filename. missing

If command extensions are enabled, the IF will change as follows:

    IF [/I] string1 compare-op string2 command
    IF CMDEXTVERSION number command
    IF DEFINED variable command

Among them, compare-op can be:

    EQU - equal to
    NEQ - not equal to
    LSS - less than
    LEQ - less than or equal to
    GTR - greater than
    GEQ - greater than or equal to

And the /I switch, if specified, tells string comparisons to be done case insensitive.
The /I switch can be used on the form string1==string2 of IF. These
The comparisons are all generic; the reason is that if string1 and string2 are both
Composed of numbers, strings are converted to numbers for numeric comparison.

The CMDEXTVERSION condition works the same as for ERRORLEVEL, except that it
is being compared against the build number associated with the command extension. first version
it's 1. The version number is incremented by one each time there is a considerable enhancement to the command extension.
The CMDEXTVERSION condition is not true when command extensions are disabled.

If the environment variable is defined, the DEFINED condition acts the same as EXIST,
Except it takes an environment variable and returns true.

If there is no environment variable named ERRORLEVEL, %ERRORLEVEL%
A string expression that expands to the current value of ERROLEVEL; otherwise, you get
its value. After running the program, the following statement illustrates the use of ERRORLEVEL:

    goto answer %ERRORLEVEL%
    :answer0
    echo Program had return code 0
    :answer1
    echo Program had return code 1

You can also use the numerical comparison above:

    IF %ERRORLEVEL% LEQ 1 goto okay

If there is no environment variable named CMDCMDLINE, %CMDCMDLINE%
Will expand to the original passed to CMD.EXE before any processing by CMD.EXE
command line; otherwise, you get its value.

If there is no environment variable named CMDEXTVERSION,
%CMDEXTVERSION% will expand to the current value of CMDEXTVERSION
String expression; otherwise, you get its value. 

I also encountered pitfalls in writing before, but noticed this sentence: Since the ELSE command must be on the same line as the end of the IF command, the following clause will not be valid: IF EXIST filename. del filename.
ELSE echo filename.missing
It seems to tell us to write the if statement on the same line, otherwise you will always be prompted for syntax errors when you run Jenkins. . .

I looked at the Windows startup script of jmeter later, they are all written in this way: IF %ERRORLEVEL% LEQ 1 goto okay
: okay

goto END

:END

Windows Build Script

So the final Windows build script is probably like this, just for demonstration, you can actually add code execution logic in different branches, such as sit executes the sit environment script, uat executes the uat environment script. . .

if %execute_env% == debug goto debug
if %execute_env% == sit goto sit
if %execute_env% == uat goto uat
if %execute_env% == uat goto prd
:debug
echo debugging environment
goto END

:sit
echo test environment
goto END

:uat
echo UAT test environment
goto END

:prd
echo production environment
goto END

:END

Test Verification

You can see that the first option is selected by default, we choose the first option to build, and then view the build console output

Then we choose other options to see if we can really switch the environment, choose sit build:

Here, the parametric construction is realized