'Matlab script cannot produce and save a job artifact when ran from pipeline

I am running a simple job that aims to:

  1. create a file,
  2. write a random string into it,
  3. and save it in the project directory (CI_PROJECT_DIR) as a job artifact.

I am doing this by running two kinds of commands in powershell from the pipeline:

  1. :

    echo "test 1" >> ${CI_PROJECT_DIR}\output_1.txt
    
  2. :

    matlab -r "f = fopen(sprintf('%s\\output_2.txt', pwd),'w'); fprintf(f,'%d', 'test 2'); fclose(f); exit;"
    

Command (1) is using a regular powershell command, whereas command (2) is using a Matlab script.

My current issue is as follows: command (1) creates/writes/saves the file, whereas command (2) does not.

Here is the job output to the upload of artifacts:

Uploading artifacts...
Runtime platform                                    arch=amd64 os=windows pid=11596 revision=c6bb62f6 version=14.10.0
C:\GitLab-Runner\my\path\output_1.txt: found 1 matching files and directories 
WARNING: C:\GitLab-Runner\my\path\output_2.txt: no matching files 

If I run the Matlab script manually (outside the pipeline) from powershell or Matlab itself, it runs smoothly and saves the file properly. But when I run it from the .gitlab-ci.yml file it does not. Why is that? Should I run things from the MATLABROOT or something similar? EDIT: I tried the last one and it did not work, I ran things from MATLABROOT\R2021a\bin.

I also tried using the -batch option instead or the -r option but the job fails to finish and gives the following error:

ERROR: MATLAB error Exit Status: 0x00000001

Could it be that the batch mode does not activate somehow in Matlab on my set up? I have MATLAB 2021a.


Here is the .gitlab-ci.yml file:

stages:  
  - savefile

job1:
  stage: savefile
  tags:
    - my_runner
  script:
    - cd $CI_PROJECT_DIR
    - echo "test 1" >> ${CI_PROJECT_DIR}\output_1.txt
    - matlab -r "f = fopen(sprintf('%s\\output_2.txt', pwd),'w'); fprintf(f,'%d', 'test 2'); fclose(f);"
    - ls
  artifacts:
    paths:
      - ${CI_PROJECT_DIR}\output_1.txt
      - ${CI_PROJECT_DIR}\output_2.txt

Here the job output to the ls command:

$ ls
Mode                 LastWriteTime         Length Name                                                                 
----                 -------------         ------ ----                                                                                                                                
-a----        27/04/2022     09:45            394 .gitlab-ci.yml                                                       
-a----        27/04/2022     09:45             18 output_1.txt                                                         
-a----        25/04/2022     19:11           6373 README.md   


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source