CloudBees action: Run a Jenkins® job

2 minute read

Use this action to trigger a Jenkins job. Jenkins is an open-source automation server for CI/CD.

All CloudBees action repositories are listed at CloudBees, Inc. on GitHub.

Inputs

Table 1. Input details
Input name Data type Required? Description

url

String

Yes

The Jenkins server URL.

username

String

Yes

The Jenkins username.

token

String

Yes

The Jenkins token.

job-name

String

Yes

The Jenkins job name.

test-type

String

No

Specifies the test type for generating a job test report. Supported test types are JUnit (junit) and TestNG (testng).

test-result-location

String

No

Specifies the test report file location. Accepts pattern matching, such as my-dir/*/my-file.[1]

parameters

JSON

No

Any additional parameters, formatted as JSON data in key/value pairs.

[1] You must specify the test-result-location to publish your test results in the platform Test insights dashboard.

Outputs

Table 2. Output details
Output name Data type Required? Description

jenkins_output

JSON string

No

A JSON string containing the output key/values exposed by the invoked Jenkins pipeline.[1]

[1] You must parse each output parameter in the following format:

fromJSON(steps.<STEP-ID>.outputs.jenkins_output).<OUTPUT_PARAMETER>

Replace <STEP_ID> with your step ID, and <OUTPUT_PARAMETER> with your parameter.

Usage examples

In your YAML file, add:

jobs: run-jenkins-job: steps: - name: Run Jenkins job uses: cloudbees-io/jenkins-run-job@v2 with: username: ${{ secrets.JENKINS_USERNAME }} token: ${{ secrets.JENKINS_TOKEN }} url: ${{ vars.JENKINS_URL }} job-name: jenkins_job_name test-type: Junit test-result-location: junit-service* parameters: '{"ENV_NAME":"TEST"}'

Using output from Jenkins

In the following example, output is passed from Jenkins to the platform.

The jenkinsfile below is used to define the outputs:

node { stage('Build') { env.BUILD_NUMBER="1" env.TESTVAR="Test" } }

The platform workflow file below prints the BUILD_NUMBER and TESTVAR outputs from Jenkins:

jobs: Jenkins-Output-Job: outputs: jenkins_output: ${{ steps.my-output-job.outputs.jenkins_output }} steps: - id: my-output-job name: Run Jenkins job with output uses: cloudbees-io/jenkins-run-job@v2 with: url: ${{ vars.JENKINS_URL }} username: ${{ secrets.JENKINS_USERNAME }} token: ${{ secrets.JENKINS_TOKEN }} job-name: jenkins_job_with_output_name - id: expose-output name: Expose Jenkins job output uses: docker://exozet/jq:1.6-r0 if: ${{ fromJSON(steps.my-output-job.outputs.jenkins_output).TESTVAR != '19' }} run: | echo "BUILD_NUMBER: ${{ fromJSON(steps.my-output-job.outputs.jenkins_output).BUILD_NUMBER }}" echo "TESTVAR: ${{ fromJSON(steps.my-output-job.outputs.jenkins_output).TESTVAR }}" Print-Jenkins-Outputs: if: ${{ fromJSON(needs.Jenkins-Output-Job.outputs.jenkins_output).TESTVAR != '19' }} needs: [Jenkins-Output-Job] steps: - id: print-jenkins-output uses: docker://exozet/jq:1.6-r0 name: Print Jenkins-Output-Job outputs run: | echo "BUILD_NUMBER: ${{ fromJSON(needs.Jenkins-Output-Job.outputs.jenkins_output).BUILD_NUMBER }}" echo "TESTVAR: ${{ fromJSON(needs.Jenkins-Output-Job.outputs.jenkins_output).TESTVAR }}"