You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 27, 2025. It is now read-only.
You can use the Jenkins™plugin for MATLAB®in freestyle and multi-configuration projects. This guide demonstrates how to run your MATLAB code or Simulink®model on Jenkins.
1
+
When you define a [Jenkins™Pipeline](https://www.jenkins.io/doc/book/pipeline/), wether in the web UI or with a [`Jenkinsfile`](https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#setting-environment-variables), you can use the plugin to run your MATLAB® code or Simulink®models on Jenkins. This guide demonstrates how to configure the plugin and use it in freestyle, multi-configuration, and Pipeline projects.
2
2
3
-
-[Use MATLAB for Build](#use-matlab-for-build)
4
-
-[Specify Build Steps](#specify-build-steps)
5
-
-[Run MATLAB Tests](#run-matlab-tests)
6
-
-[Run MATLAB Command](#run-matlab-command)
3
+
4
+
-[Configure Plugin in Web UI](#configure-plugin-in-web-ui)
5
+
-[Use MATLAB for Build](#use-matlab-for-build)
6
+
-[Specify Build Steps](#specify-build-steps)
7
+
-[Run MATLAB Command](#run-matlab-command)
8
+
-[Run MATLAB Tests](#run-matlab-tests)
7
9
-[Set Up Freestyle Project](#set-up-freestyle-project)
8
10
-[Set Up Multi-Configuration Project](#set-up-multi-configuration-project)
11
+
-[Set Up Pipeline Project](#set-up-pipeline-project)
12
+
-[Add MATLAB to System Path](#add-matlab-to-system-path)
-[Use MATLAB for Matrix Build](#use-matlab-for-matrix-build)
16
+
17
+
## Configure Plugin in Web UI
18
+
You can use the web UI provided by Jenkins to configure the plugin in freestyle and multi-confiuraton projects. To run MATLAB or Simulink in a Pipeline project, see [Set Up Pipeline Project](#set-up-pipeline-project).
9
19
10
-
## Use MATLAB for Build
20
+
###Use MATLAB for Build
11
21
Once you install the plugin, **Use MATLAB version** appears in the **Build Environment** section of the project configuration window.
@@ -24,12 +34,12 @@ If the build agent already has your desired MATLAB on the path, then you are not
24
34
| Linux®| /usr/local/MATLAB/R2019a |
25
35
| Mac | /Applications/MATLAB_R2019a.app |
26
36
27
-
## Specify Build Steps
37
+
###Specify Build Steps
28
38
When you set up the **Build** section of the project configuration window, the plugin provides you with the **Run MATLAB Command** and **Run MATLAB Tests** build steps.
29
39
30
40
If you use a source code management (SCM) system such as Git™, then your project must include the appropriate SCM configuration to check out the code before it can invoke the plugin. If you do not use any SCM systems to manage your code, then an additional build step is required to ensure that the code is available in the Jenkins workspace before the build starts.
31
41
32
-
### Run MATLAB Command
42
+
####Run MATLAB Command
33
43
The **Run MATLAB Command** build step enables you to specify MATLAB commands tailored to your specific needs. For example, you can use this build step to customize your test run or add a different build step to your pipeline.
34
44
35
45
If you specify more than one MATLAB command, use a comma or semicolon to separate the commands. The build fails if the execution of any command results in an error.
@@ -40,12 +50,13 @@ If you need to specify several MATLAB commands, consider writing a MATLAB script
40
50
41
51
Test artifacts are not autogenerated if you choose to run tests using this build step. You can generate your desired test artifacts by configuring the test runner in the script or function that you invoke from the **Command** box.
42
52
43
-
### Run MATLAB Tests
53
+
####Run MATLAB Tests
44
54
This build step uses a default setting to run tests authored using the MATLAB Unit Testing Framework or Simulink Test™. If your source code is organized into files and folders within a MATLAB project, then the plugin includes any test files in the project that have been labeled as `Test`. If your code does not leverage a MATLAB project or uses a MATLAB release before R2019a, then the plugin includes all tests in the project workspace, including its subfolders.
45
55
46
56
The **Run MATLAB Tests** build step enables you to generate different types of test artifacts. To publish the test results, you can use these artifacts with other Jenkins plugins. By default, the plugin assigns a name to each selected artifact and stores it in the `matlabTestArtifacts` folder of the project workspace. You can override the default artifact name and location by specifying a path relative to the project folder in the **File path** box. (If you leave the text box empty, the plugin does not generate an artifact.)
If you do not select any of the test artifact check boxes, the tests still run, and test failures fail the build.
51
62
@@ -97,6 +108,201 @@ You can define several axes in the **Configuration Matrix** section. For example
97
108
98
109
* A multi-configuration project creates a separate workspace for each user-defined axis value. If you specify the full paths to where MATLAB is installed as axis values, Jenkins fails to create separate workspaces and fails the build.
99
110
111
+
## Set Up Pipeline Project
112
+
When you define your Pipeline with a `Jenkinsfile`, the plugin provides you with a step to run MATLAB scripts, functions, and statements. The plugin also provides a step to run MATLAB and Simulink tests. (These steps are common to both Declarative and Scripted Pipeline.)
113
+
114
+
To configure the plugin for a Pipeline project:
115
+
1) Define your Pipeline in a `Jenkinsfile` in the root of your repository.
116
+
2) In the **Pipeline** section of the project configuration window, select **Pipeline script from SCM** from the **Definition** drop-down menu.
117
+
3) Select your source control system from the **SCM** drop-down menu.
118
+
4) Paste your repository URL into the **Repository URL** box.
119
+
120
+
You also can define your Pipeline directly in the project configuration window. If you select **Pipeline script** from the **Definition** drop-down menu, you can author your Pipeline code in the **Script** box. When you define your Pipeline this way, your Pipeline must include an additional stage to check out MATLAB code from source control.
121
+
122
+
### Add MATLAB to System Path
123
+
When Jenkins executes your Pipeline, it invokes the first MATLAB instance on the system path. If the PATH environment variable of the build agent does not inculde any MATLAB instances, you must update the variable with the MATLAB root folder that should be used for the build.
124
+
125
+
To update the system PATH environment variable using Declarative Pipeline syntax, use an `environment` block in your `Jenkinsfile`. For example, prepend MATLAB R2019a to the system PATH enviroment variable and use it to run your command.
126
+
127
+
```groovy
128
+
pipeline {
129
+
agent any
130
+
environment {
131
+
PATH = "C:\\Program Files\\MATLAB\\R2019a\\bin;${PATH}" // Windows agent
132
+
// PATH = "/usr/local/MATLAB/R2019a/bin:${PATH}" // Linux agent
133
+
// PATH = "/Applications/MATLAB_R2019a.app/bin:${PATH}" // Mac agent
134
+
}
135
+
stages{
136
+
stage('Run MATLAB Command') {
137
+
steps
138
+
{
139
+
runMATLABCommand "disp('Hello World!')"
140
+
}
141
+
}
142
+
}
143
+
}
144
+
```
145
+
If you define your Pipeline using scripted syntax, set the PATH environment variable in the `node` block. For example:
Use the `runMATLABCommand` step in your Pipeline to run MATLAB scripts, functions, and statements tailored to your specific needs. You can use this task to flexibly customize your test run or add a build step to your pipeline.
158
+
159
+
You must provide `runMATLABCommand` with a string that specifies the command you want to execute. If the command is the name of a MATLAB script or function, do not specify the file extension. If you specify more than one MATLAB command, use a comma or semicolon to separate the commands.
For example, in your `Jenkinsfile`, define a Declarative Pipeline to run the commands in a file named `myscript.m`.
165
+
166
+
167
+
```groovy
168
+
pipeline {
169
+
agent any
170
+
stages{
171
+
stage('Run MATLAB Command') {
172
+
steps
173
+
{
174
+
runMATLABCommand 'myscript'
175
+
}
176
+
}
177
+
}
178
+
}
179
+
```
180
+
181
+
You also can use `runMATLABCommand` in a Scripted Pipeline.
182
+
183
+
```groovy
184
+
node {
185
+
runMATLABCommand 'myscript'
186
+
}
187
+
```
188
+
189
+
MATLAB exits with exit code 0 if the specified script, function, or statement executes successfully without error. Otherwise, MATLAB terminates with a nonzero exit code, which causes the build to fail. You can use the [`assert`](https://www.mathworks.com/help/matlab/ref/assert.html) or [`error`](https://www.mathworks.com/help/matlab/ref/error.html) functions in the command to ensure that builds fail when necessary.
190
+
191
+
When you use the `runMATLABCommand` step, all of the required files must be on the MATLAB search path.
192
+
193
+
### Use `runMATLABTests` Step
194
+
195
+
Use the `runMATLABTests` step in your Pipeline to run all tests in your MATLAB project and generate artifacts. MATLAB includes any files in your project that have a `Test` label. If your Pipeline does not leverage a MATLAB project or uses a MATLAB release before R2019a, then MATLAB includes all tests in the the root of your repository including its subfolders.
196
+
197
+
For example, in your `Jenkinsfile`, define a Declarative Pipeline to run the tests in your project and fail the build if any of the tests fails.
198
+
199
+
200
+
```groovy
201
+
pipeline {
202
+
agent any
203
+
stages{
204
+
stage('Run MATLAB Tests') {
205
+
steps
206
+
{
207
+
runMATLABTests()
208
+
}
209
+
}
210
+
}
211
+
}
212
+
```
213
+
214
+
Use the `runMATLABTests` step in a Scripted Pipeline to run the tests in your project.
215
+
216
+
```groovy
217
+
node {
218
+
runMATLABTests()
219
+
}
220
+
```
221
+
222
+
To generate artifacts using the `runMATLABTests` step, provide the step with one or more name-value pair arguments to specify the artifacts and the locations to save them. Use a colon to Separate names and values.
223
+
224
+
For example, define a Declarative Pipeline to run the tests in your MATLAB project automatically, and then generate a JUnit test results report and a Cobertura code coverage report at specified locations on the build agent.
| testResultsPDF | Path to write test results report in PDF format (currently not supported on Mac platforms).<br/>**Example:**`'test-results/results.pdf'`|
258
+
| testResultsTAP | Path to write test results report in TAP format.<br/>**Example:**`'test-results/results.tap'`|
259
+
| testResultsJUnit | Path to write test results report in JUnit XML format.<br/>**Example:**`'test-results/results.xml'`|
260
+
| testResultsSimulinkTest | Path to export Simulink Test™ Manager results in MLDATX format (requires Simulink Test license and is supported in MATLAB R2019a or later).<br/>**Example:**`'test-results/results.mldatx'`|
261
+
| codeCoverageCobertura | Path to write code coverage report in Cobertura XML format.<br/>**Example:**`'code-coverage/coverage.xml'`|
262
+
| modelCoverageCobertura | Path to write model coverage report in Cobertura XML format (requires Simulink Coverage™ license and is supported in MATLAB R2018b or later).<br/>**Example:**`'model-coverage/coverage.xml'`|
263
+
264
+
265
+
## Use MATLAB for Matrix Build
266
+
Similar to multi-configuration projects, you can use the plugin to perform [matrix](https://www.jenkins.io/doc/book/pipeline/syntax/#declarative-matrix) builds in Pipeline projects. For example, you can define a Declarative Pipeline to run your test suite on different platforms or against different versions of MATLAB. (Matrix build is not supproted in Scripted Pipelines.)
267
+
268
+
Your Pipeline must have a `matrix` section to define the possible name-value combinations that should run in parallel. This example shows how to define a Pipeline to run your MATLAB code and generate test artifacts using MATLAB R2018b, R2019a, and R2020a.
269
+
270
+
```groovy
271
+
pipeline {
272
+
agent any
273
+
stages {
274
+
stage('BuildAndTest') {
275
+
matrix {
276
+
agent any
277
+
environment {
278
+
PATH = "C:\\Program Files\\MATLAB\\${VERSION}\\bin;${PATH}" // Windows agent
Copy file name to clipboardExpand all lines: examples/Run-MATLAB-Tests.md
+1-2Lines changed: 1 addition & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,8 +45,7 @@ Create a new project and configure it by following these steps:
45
45
46
46
6. In the **Build** section of Jenkins, select **Add build step > Run MATLAB Tests**. Then, select your desired test artifacts to be generated in the project workspace. The plugin in this example is configured to generate Cobertura code coverage and JUnit test result reports. For more information on how to configure the plugin, see [Plugin Configuration Guide](../CONFIGDOC.md).
7. In the **Post-build Actions** section of Jenkins, add two post-build actions to publish the Cobertura code coverage and JUnit test result reports. For each report, provide the path to the report file.
0 commit comments