Jenkins Pipeline Parameters: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 14: Line 14:
=Parameter Declaration=
=Parameter Declaration=


A parameter is declared in the pipeline's [[Jenkins_Concepts#Jenkinsfile|Jenkinsfile]] with a different syntax depending on whether the pipeline is [[Jenkins_Pipeline_Syntax#Declarative_Pipeline|declarative]] or [[Jenkins_Pipeline_Syntax#Scripted_Pipeline|scripted]]. Once the Jenkinsfile is committed to the repository, the Jenkins server monitoring the branch will automatically update the UI based on the new Jenkinsfile, making the parameter available in the UI. For more details see [[#Relationship_Between_the_Pipeline_UI_and_Jenkinsfile_Parameters|Relationship between the Pipeline UI and Jenkinsfile Parameters]] below.
A parameter is declared in the pipeline's [[Jenkins_Concepts#Jenkinsfile|Jenkinsfile]] with a different syntax depending on whether the pipeline is [[Jenkins_Pipeline_Syntax#Declarative_Pipeline|declarative]] or [[Jenkins_Pipeline_Syntax#Scripted_Pipeline|scripted]]. Once the Jenkinsfile is committed to the repository, the Jenkins server monitoring the branch will automatically update the UI based on the new Jenkinsfile, making the parameter available in the UI. For more details see [[#Relationship_Between_the_Pipeline_UI_and_Jenkinsfile_Parameters|Relationship between the Pipeline UI and Jenkinsfile Parameters]] below. As such, a parameter enables the pipeline to prompt the user for a piece of information to be passed into the build.  


==Declarative Pipeline Paramenter Declaration==
==Declarative Pipeline Paramenter Declaration==
Line 39: Line 39:
Once a parameter is defined in Jenkinsfile, and the Jenkinsfile committed to the repository on a specific branch, the multi-branch pipeline that tracks the branch automatically updates the pipeline UI for that branch, so the parameter become available in "Build with Parameters" associated with the branch.
Once a parameter is defined in Jenkinsfile, and the Jenkinsfile committed to the repository on a specific branch, the multi-branch pipeline that tracks the branch automatically updates the pipeline UI for that branch, so the parameter become available in "Build with Parameters" associated with the branch.


=Scripted Pipeline Parameter Support=


-----






 
Each parameter has a "Name" and a "Value", which depends on the parameter type. A "Default Value" can be specified. The name-value pairs will be exported as [[Jenkins Pipeline Environment Variables|environment variables]] when the build starts, allowing subsequent parts of the build configuration, such as build steps, to access those values using the following syntax:
 
A parameter enables the pipeline to prompt the user for a piece of information to be passed into the build. Each parameter has a "Name" and a "Value", which depends on the parameter type. A "Default Value" can be specified. The name-value pairs will be exported as [[Jenkins Pipeline Environment Variables|environment variables]] when the build starts, allowing subsequent parts of the build configuration, such as build steps, to access those values using the following syntax:
  ${PARAMETER_NAME}
  ${PARAMETER_NAME}
The parameter values can also be retrieved in a [[Jenkins_Concepts#Jenkinsfile|Jenkinsfile]] with the following syntax:
The parameter values can also be retrieved in a [[Jenkins_Concepts#Jenkinsfile|Jenkinsfile]] with the following syntax:

Revision as of 23:58, 17 April 2020

External

Internal

Overview

A parameter is a way to configure a pipeline. If you think of a pipeline as a function, then a parameter is the function's argument.

Parameter Declaration

A parameter is declared in the pipeline's Jenkinsfile with a different syntax depending on whether the pipeline is declarative or scripted. Once the Jenkinsfile is committed to the repository, the Jenkins server monitoring the branch will automatically update the UI based on the new Jenkinsfile, making the parameter available in the UI. For more details see Relationship between the Pipeline UI and Jenkinsfile Parameters below. As such, a parameter enables the pipeline to prompt the user for a piece of information to be passed into the build.

Declarative Pipeline Paramenter Declaration

Declarative pipelines support parameters by default, with the parameters directive.

Scripted Pipeline Paramenter Declaration

properties([

  parameters([

    string(
        name: 'Greeting',
        defaultValue: 'Hello', 
        description: 'How should I greet the world?')

  ])
])

Relationship Between the Pipeline UI and Jenkinsfile Parameters

Once a parameter is defined in Jenkinsfile, and the Jenkinsfile committed to the repository on a specific branch, the multi-branch pipeline that tracks the branch automatically updates the pipeline UI for that branch, so the parameter become available in "Build with Parameters" associated with the branch.




Each parameter has a "Name" and a "Value", which depends on the parameter type. A "Default Value" can be specified. The name-value pairs will be exported as environment variables when the build starts, allowing subsequent parts of the build configuration, such as build steps, to access those values using the following syntax:

${PARAMETER_NAME}

The parameter values can also be retrieved in a Jenkinsfile with the following syntax:

properties([
  ...
  parameters([
    string(
      name: 'branch',
      defaultValue: 'develop',
      description: 'Branch name or branch head\' SHA1 hash to be used to run this build on.'
    ),
    string(
      name: 'artifact version',
      description: 'The version of the artifact (ex: 1.0.0-build.1)'
    ),
    text(
      name: 'Test Packages,
      defaultValue: 'test-package-1',
    )
 ])
])

They can be reported, for debugging purposes, as follows:

params.get(propertyName)

Each parameter should have a unique name. Spaces are not permitted in parameter names.

When the build is parameterized, the "Build Now" control will be replaced with "Build with Parameters", where users will be prompted to specify values for each defined parameter. If they choose not to do anything, the build will start with the "Default Value". If the build is started automatically, the "Default Value" is used, unless the build triggering function provides the corresponding values, as follows:

build(
  job: "IntegrationTesting",
  parameters: [
    string(name: 'branch', value: serverSha),
    string(name: 'branchName', value: branch)
  ] + artifacts.keySet().sort().collect({ name ->
          echo "name: ${name}, value: ${artifacts[name]['artifact']}"
          string(name: name, value: artifacts[name]['artifact'])
    }),
    quietPeriod: 0,
    wait: true,
    propagate: true
)

When a parameterized build is in the queue, attempting to start another build of the same project will only succeed if the parameter values are different, or if the Execute concurrent builds if necessary option is enabled.

Relationship Between UI and Jenkinsfile Parameters

  • TODO_nw3td What is the relationship between the parameters declared in the pipeline's UI and the parameters declared in the pipleline's Jenkinsfile?
  • TODO_n88Y6 Establish the precedence between a defaultValue declared in the "parameter" section in Jenkinsfile and one declared for the same parameter in the UI. It seems the file value influences the UI value.


Parameters Types

String

Multi-Line String Parameter

File Parameter