Using the Jenkins Command

Updated 1 month ago by Michael Cretzman

One of the steps you can include in a Harness workflow is a Jenkins command step.

With the Jenkins command, you can execute Jenkins jobs in the shell session of the workflow.

When executing a job, you can also dynamically capture the output from the job, publishing runtime variables based on the context, and using those variables in another step in the same workflow, phase, or another workflow or phase in the same pipeline.

The Shell Script workflow command step can also set and capture shell session information and publish it as output variables. For more information, see Using Shell Script Commands.

What Information is Available to Capture?

Any Jenkins job information in the particular shell session of the workflow can be captured and output using one or more Jenkins steps in that workflow. In addition, you can capture information available using the built-in Harness variables. For more information, see Variables and Expressions in Harness.

Capturing and exporting output in the Jenkins step can be very powerful. For example, a Jenkins step could capture Jenkins build information in a workflow, and a Harness service could echo the build information and use it in a complex function, and then export the output down the pipeline for further evaluation.

Intended Audience

  • Developers
  • DevOps

Before You Begin

Use the Jenkins Command Step

The following procedure adds and configures a Jenkins command step to a workflow, and captures and publishes Jenkins output in a variable. Later, we will show you how to use the published variable in a Harness service.

To use the Jenkins command step, do the following:

  1. In a Harness application, open a workflow. For this example, since we are using Jenkins, we will use a Build workflow.
  2. In the workflow Phase section, in Prepare Steps, click Add Command.


    The Add Command dialog opens.
  3. In Add Command, in Others, click Jenkins. The Jenkins dialog appears.
  4. Configure the Jenkins command step and click SUBMIT. The Jenkins command step has the following settings.

Field

Description

Jenkins Server

Select the Jenkins server you added as a Harness Artifact Server. For more information, see the Jenkins artifact server setup.

Job Name

Select the Jenkins job (also called a project) to execute. The list is automatically populated using the Jenkins Server you selected.

Job Parameters

If you are using a parameterized build, click Add to add your name/value parameters.

Treat unstable Jenkins status as success

A build is stable if it was built successfully and no publisher reports it as unstable.

A build is unstable if it was built successfully and one or more publishers report it unstable. For example, if the JUnit publisher is configured and a test fails then the build will be marked unstable.

Timeout

Enter the timeout period, in milliseconds. For example, 600000 milliseconds is 10 minutes. The timeout period determines how long to wait for the step to complete. When the timeout expires, it is considered a workflow failure and the workflow Failure Strategy is initiated.

Wait interval before execution

Set how long the deployment process should wait before executing the step.

Execute with previous steps

Check this checkbox to run this step in parallel with the previous steps.

Publish Jenkins output in the context

To export the Jenkins job output as a variable, enable Publish Jenkins output in the context.

Variable Name

Enter a unique name for the output variable. You will use this name to reference the variable elsewhere. For example, if the Variable Name is Jenkins, you would reference the Jenkins buildNumber with ${Jenkins.buildNumber}.

Scope

Select Pipeline, Workflow, or Phase. The output variables are available within the scope you set here.

The scope you select is useful for preventing variable name conflicts. You might use a workflow with published variables in multiple pipelines, so scoping the variable to Workflow will prevent conflicts with other workflows in the pipeline.

A completed Jenkins step looks something like this:

Using Published Jenkins Variables

To use the Jenkins variable you published in the Jenkins steps, you can use the syntax:

${var_name.property_name}

The syntax ${context.output("var_name").property_name} is also allowed.

For example, if the variable name you entered in the Variable Name field of the Jenkins workflow step is Jenkins, you could create a Harness service with an Exec script to echo job properties:

echo buildNumber: ${Jenkins.buildNumber}
echo buildUrl: ${Jenkins.buildUrl}
echo buildDisplayName: ${Jenkins.buildDisplayName}
echo buildFullDisplayName: ${Jenkins.buildFullDisplayName}
echo jobStatus: ${Jenkins.jobStatus}
echo description: ${Jenkins.description}

The service Exec script would look something like this:

Let's name that script Echo Jenkins Output. Later, in a workflow that uses this service, you can add that script as a command:

When the workflow is deployed, the Echo Jenkins Output Exec is run and the echo output from our Echo Jenkins Output workflow step is displayed:

Jenkins Output Service Script

Echo Jenkins Output Exec Log

echo buildNumber: ${Jenkins.buildNumber}
echo buildUrl: ${Jenkins.buildUrl}
echo buildDisplayName: ${Jenkins.buildDisplayName}
echo buildFullDisplayName: ${Jenkins.buildFullDisplayName}
echo jobStatus: ${Jenkins.jobStatus}
echo description: ${Jenkins.description}
INFO   2018-09-20 21:55:21    13
INFO 2018-09-20 21:55:21 https://jenkins.wings.software/job/build-descriptor-todolist/13/
INFO 2018-09-20 21:55:21 build-descriptor-todolist
INFO 2018-09-20 21:55:21 latest
INFO 2018-09-20 21:55:21 SUCCESS
INFO 2018-09-20 21:55:21 Command finished with status SUCCESS

Notes

Job parameters are captured in metadata, using the syntax:

${context.output("var_name").metadata.<parameter_key>}


How did we do?