How to stop robocopy from exiting the build?
Asked Answered
B

2

11

I'm using Gitlab 8.15.4 and the latest runner for that build. Because of our firewall I can't run npm install so I'm copying the node-modules from another location into the build folder. The runner is on a Windows 7 machine.

My first attempt: (.gitlab-ci.yml)

before_script:
- robocopy S:\Storage\GitLab-Runner\Assets\node_modules .\node_modules /s
build:
  stage: build
  script:
    - echo starting
    - gulp
    - echo done
  artifacts:
    paths:
    - deploy.zip   

Fails the build with the error:

ERROR: Job failed: exit status 1

My second (nth) try puts the robocopy into a script file and executes it from there:

(.gitlab-ci.yml)

before_script:
- S:\Storage\GitLab-Runner\Scripts\CopyAssets.bat
build:
  stage: build
  script:
    - echo starting
    - gulp
    - echo done
  artifacts:
    paths:
    - deploy.zip   

(CopyAssets.bat)

robocopy S:\Storage\GitLab-Runner\Assets\node_modules .\node_modules /s
set/A errlev="%ERRORLEVEL% & 24"
exit/B %errlev%     

Passes but does not execute any other steps.

How can I prevent RoboCopy from exiting the build when it finishes?

Bewray answered 12/6, 2017 at 16:53 Comment(0)
A
12

You and a lot of other people have encountered this issue with robocopy in CI deployment. As I have found this question being unanswered for some time and the other answers being incompatible with continuing the script after robocopy, I want to share the solution here.

If you want robocopy to ignore all return codes under 8 (>= 8 means copy error), you need a condition that follows the command directly and changes the error level.

(robocopy src dst) ^& IF %ERRORLEVEL% LSS 8 SET ERRORLEVEL = 0
Aiello answered 12/1, 2018 at 12:53 Comment(4)
Successfully tested with Windows Server 2008 and gitlab-runner 10.4Lobate
It does not work for me. I get an error "Unexpected token '^' in expression or statement" using a docker image based on windows server core 2019.Morelli
The command is not powershell, but normal terminal / cmdAiello
Keep in mind that successful copying returns exit code 1. Exit codes 2/4/8/10 are warnings/errors.Unstop
O
8

For powershell users:

(robocopy src dst) ; if ($lastexitcode -lt 8) { $global:LASTEXITCODE = $null }

or

cmd /c (robocopy src dst) ^& IF %ERRORLEVEL% LEQ 1 exit 0

I have tested on GitLab 13.12 with GitLab Runner powershell and it worked well.

Oconner answered 22/7, 2021 at 6:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.