CastorisCausa

Personal blog and hacks by Raymundo Cassani

GitHub actions are an amazing tool to automate different stages on in the software development workflow, and they can be used in Matlab projects!

By using Matlab GitHub actions, it is possible to set up Matlab, and run Matlab code on GitHub-hosted runners with different OS (Linux, macOS and Windows). This allows painless testing of your code on different Matlab versions on different OS.

Toy example

A working toy example of using Matlab with GitHub actions can be found here. The repo consists of two main parts: a yaml file and a Matlab script:

1. yaml file

The yaml file ./github/workflows/github-actions.yaml defines:

  • When the GitHub action will run
  • The OS versions: Linux (Ubuntu 20.04), macOS (12 Monterey) and Windows (Server 2019) for the runners
  • The Matlab version (R2021b) that will be used
  • The Matlab script (wonder_script.m) in the repo to be executed
  • Other task (display results)

Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Workflow to run a Matlab script on GitHub-Hosted Linux, Windows and macOS runners
# See: https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners

# Workflow name
name: Run Matlab script on GitHub-hosted runners
# Name for each run
run-name: "${{ github.event.head_commit.message }}"

# Parameters
env:
  MATLAB_VER: R2021b # Oldest "b" release available for Windows

# When the workflow will run
on:
  # Run on Push to 'master' or 'main' branch
  push:
    branches:
      - master
      - main
  # Run manually from GitHub Actions tab
  workflow_dispatch:

jobs:
  # Ubuntu job
  Run-Ubuntu:
    name: Run on Linux (Ubuntu 20.04)
    runs-on: ubuntu-20.04
    steps:
      - name: Check out repo
        uses: actions/checkout@v3
      - name: Set up Matlab
        uses: matlab-actions/setup-matlab@v1
        with:
          release: ${{ env.MATLAB_VER }}
      - name: Run script
        uses: matlab-actions/run-command@v1
        with:
          command: wonder_script
      - name: Print PWD
        run: pwd
      - name: Print results
        run: cat ./data/results.txt

  # Windows job
  Run-Windows:
    name: Run on Windows (Server 2019)
    runs-on: windows-2019
    steps:
      - name: Check out repo
        uses: actions/checkout@v3
      - name: Set up Matlab
        uses: matlab-actions/setup-matlab@v1
        with:
          release: ${{ env.MATLAB_VER }}
      - name: Run script
        uses: matlab-actions/run-command@v1
        with:
          command: wonder_script
      - name: Print PWD
        run: pwd
      - name: Print results
        run: type .\data\results.txt

  # macOS job
  Run-MacOS:
    name: Run on macOS (12 Monterey)
    runs-on: macos-12
    steps:
      - name: Check out repo
        uses: actions/checkout@v3
      - name: Set up Matlab
        uses: matlab-actions/setup-matlab@v1
        with:
          release: ${{ env.MATLAB_VER }}
      - name: Run script
        uses: matlab-actions/run-command@v1
        with:
          command: wonder_script
      - name: Print PWD
        run: pwd
      - name: Print results
        run: cat ./data/results.txt

2. Matlab script

The wonder_script.m Matlab script gets information of the computer where the code is running and writes such information on a text file Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function wonder_script()
% Finds the test.txt file in the dataDir directory
% Read the content of the test.txt file and 
% concatenate it with extra information, e.g, hostname, OS, Matlab version, etc.
% Saves new content as results.txt in dataDir

% Read test.txt
testFilename = 'test.txt';
dataDir = 'data';
testFilename = fullfile(dataDir, testFilename);
content = fileread(testFilename);

% Date
dateStr = ['DateTime: ' char(datetime, "yyyy-MM-dd HH:mm:ss")];

% OS information
if strncmp(computer,'PC',2)
    [~, system_info] = system('systeminfo');
    osName = regexp(system_info, '(?<=OS Name:)(.*?)(?=\n)', 'match');
    osName = strtrim(osName{1});
    osVer = regexp(system_info, '(?<=OS Version:)(.*?)(?=\n)', 'match');
    osVer = strtrim(osVer{1});
    osInfo = [osName, ' (', osVer, ')'];

elseif strncmp(computer,'MAC',3)
    % OS info
    [~, sw_vers] = system('sw_vers');
    osName = regexp(sw_vers, '(?<=ProductName:)(.*?)(?=\n)', 'match');
    osName = strtrim(osName{1});
    osVer = regexp(sw_vers, '(?<=ProductVersion:)(.*?)(?=\n)', 'match');
    osVer = strtrim(osVer{1});
    [~, osHw] = system('uname -m');
    osHw = strtrim(osHw);
    osInfo = [osName, ' ' osVer, ' (', osHw, ')'];

elseif strncmp(computer,'GLNX',4)
    % OS info
    os_release = fileread('/etc/os-release');
    osName = regexp(os_release, '(?<=PRETTY_NAME=")(.*?)(?=")', 'match');
    osName = osName{1};
    [~, kernelVer] = system('uname -r');
    kernelVer = strtrim(kernelVer);
    osInfo = [osName, ' (' kernelVer, ')'];

else
    osInfo = 'Strange host';
end
osInfo = ['OS: ' osInfo];

% Hostname
[~, hostName] = system('hostname');
hostName = ['hostname: '  strtrim(hostName)];

% Get Matlab version
matlabVer = ['Matlab:', ' ', version('-release')];

% New content
contentNew = [content 10, dateStr, 10, osInfo, 10, hostName, 10, matlabVer];

% Save results
resultFileName = 'results.txt';
resultFileName = fullfile(dataDir, resultFileName);
fileID = fopen(resultFileName,'w');
fprintf(fileID, contentNew);
fclose(fileID);

Comments

comments powered by Disqus