The Developer Zone

The Advanced Software Development with MATLAB

Git实验室!

大家好,对于这篇文章,我想介绍一下Mariano Lizarraga费尔南德斯作为客座博客。他有一个伟大的故事分享,他很好地演示了一个调谐詹金斯服务器之间的工作流和GitLab存储库。把它拿走马里亚诺·!

The Big Picture

在本帖里,我们将呈现一个工作流显示我们如何运行一个MATLAB或仿真软件测试套件持续集成服务器每次我们推动git存储库分支。

We will not describe in detail what Continuous Integration is the and everything. It involves the For those interested, We how many这篇文章As the an the introduction to the topic. This post introduces the concept of interfacing with MATLAB詹金斯A well - known continuous integration server. Feel free to explore this and other CI related posts by & at the持续集成的范畴。

在下面,我们假设您有一些基本熟悉git和詹金斯服务器运行在本地(甚至)测试访问git存储库。我们将展示Jenkins和之间的相互作用GitLab(a well - known open source git repository manager) and therefore you will need to install theGitLab插件In Jenkins.

Let 's go through the Workflow step by step.

为了这篇文章中,我们将假定一个简化的持续集成的工作流程如下:

  1. Changes are made to code or a model, staged, and committed to a git repository 's testing branch.
  2. Changes are pushed to a git remote 's testing branch.
  3. The remote repository notifies (via a web hooks) The Jenkins CI server that there has had a new push to The event.
  4. 詹金斯服务器把变化从git存储库和运行一个测试套件。
  5. If a test fails, it creates a bug issue in GitLab, If the test passes, it creates a merge request.

这是一个图片说明这个工作流程:

Getting Jenkins Ready

我们之前写过博客如何设置詹金斯To launch MATLAB unit tests. So here we 'l l focus on you it, So every time you push the to the测试Branch, Jenkins a workflow is triggered.

首先创建一个自由泳项目Jenkins和配置您的源代码管理选项卡使用git。远程git存储库的完整地址类型,配置您的凭证,确保你把它从你测试Branch.

Next, configure your project to build the when a change is pushed to GitLab. Take note of the GitLab CI Service URL, as you will need it later to configure GitLab.

Finally, the configure launching with MATLAB没有显示,改变项目的工作区目录,并运行一个脚本,我们将在以下部分:

After that, save your changes. Your freestyle project is ready. Now off to GitLab.

Getting GitLab ready

In GitLab the first thing to do is to configure the Web hooks that will notify Jenkins when a push occurs. To do that, navigate to Settings and In the Integrations subsection, add the URL you demonstrate the from the previous section, check the push events the checkbox and click the add Webhook button:

At this point you can test that web hooks triggers a build in Jenkins. To do that, in the same Integrations subsection in GitLab, select your webhook from the list, and using the测试按钮,选择推动事件。你应该立即得到一个蓝丝带在顶部显示您的web成功执行

最后,因为我们将使用GitLab RESTful API创建的问题和请求合并,你需要得到一个个人访问令牌。要做到这一点,去你的个人设置,(在最右边的角落标准GitLab实例)和从那里去你访问令牌分段。这将带你去访问令牌创建页面。选择一个名称,选择一个过期日期(你也可以把它空白)和范围检查API复选框。

After you click on the green创建个人访问令牌按钮,你将被带到一个页面显示您的令牌。确保你的记录标记的地方你将无法看到它。我们将使用令牌当我们编写MATLAB脚本在詹金斯运行测试。

Testing Script

We will use a variation of the script published the forThis Simulink 's Blog post

Convert it to a function to receive one argument:参数将决定如果它跑在持续集成或本地。这将允许我们在本地测试然后推动服务器来运行。

函数runMyTests (ranInCI)导入Matlab. Unittest. TestRunnerThe importMatlab. Unittest. Plugins. TAPPluginThe importMatlab. Unittest. Plugins. ToFile工作区= pwd;如果RanInCI resultsLocation = = = 1 fullfile (PWD,'results');。

收集关于当前数据提交: So we can put comments on the issues or merge request about which commit passed and needs to be merged (or failed and needs to be fixed).

% Collect data from the current commit to include in the% the body of the issue or the merge request.[~、提交]=系统(“git rev-parse——短头”);[~,分支]=系统(“git rev-parse——abbrev-ref头”);

做一个web GET或POST GitLab API:We will use webwrite and webread to create the issues and the merge requests as necessary. Note that in you will need the private token created in GitLab.

如果ranInCI = = 1拷贝文件(tapResultsFile fullfile (resultsLocation,“TAPResults.tap”));totalFailures =总和(vertcat(结果(:)fail));writeOptions = weboptions ('MediaType','application/x - WWW - form - urlencoded',...'HeaderFields',...{'PRIVATE - TOKEN', gitPrivateToken});如果totalFailures = = 0%如果没有失败,创建一个请求合并writeMergeURL = [gitLabAPI'the projects/'ProjectID'/ merge_requests?'];webwrite (writeMergeURL...“标题”,...(“(Jenkins)测试(s)通过提交的Strtrim (commit)]....'description',...(“持续集成通过提交”Strtrim (commit)...“分支”strtrim(分支)...”。合并请求准备审查的]....“标签”,'merge_request',...'source_branch'The strtrim (branch),...'target_branch','master',...'approvals_before_merge',“3”,...writeOptions);其他的%测试失败,在GitLab创建一个测试失败问题writeIssueURL = [gitLabAPI'the projects/'ProjectID“问题?”];webwrite (writeIssueURL...“标题”,...(“(Jenkins)测试(s)在提交失败的Strtrim (commit)]....'description',...('the Continous Integration Failed when running on the commit'...Strtrim (commit)“分支”strtrim(分支)“。”]....“标签”,'bugs',...writeOptions);结束结束

Putting it all Together

现在,您可以测试完整的工作流。你可以看看下面的视频显示完整的过程。注意,视频是无声电影,不需要启动你的音频。

如果你是工作在MATLAB和使用git源代码控制,考虑使用持续集成服务器的自动化测试,并确保您的测试运行。试一试,让我们知道你的想法!

|

评论

要发表评论,请点击hereLog in to your MathWorks account or create a new account.

Baidu
map