Windows PowerShell runner throws error due to badly formatted script.ps1
Hey guys, I'm trying to use the gitlab ci runner on Windows (Windows 10 Pro x64 German
) with Powershell 5.0.10240.16384
($PSVersionTable.PSVersion.tostring())as the runner shell. Here's what I've done:
-
download windows x64 runner and register it
PS C:\Projects> .\gitlab-ci-multi-runner-windows-amd64.exe -v C:\Projects\gitlab-ci-multi-runner-windows-amd64.exe version 0.6.0 (4c26bc5)
2. open config.toml and replace shell (https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/blob/master/docs/configuration/advanced-configuration.md#the-shells)
```
PS C:\Projects> cat .\config.toml
concurrent = 1
[[runners]]
url = "http://batman.mydomain.com:8888/ci"
token = "21b33c87263af05447d57e1a338..."
name = "UNB001"
executor = "shell"
shell = "powershell"
[runners.ssh]
[runners.docker]
image = ""
privileged = false
[runners.parallels]
base_name = ""
-
Create and commit .gitlab-ci.yml with a simple test:
ps_test: tags: - Windows script: - Get-Childitem
<br>
Unfortunately the test build fails on my Windows 10 runner. Here's the error from Gitlab-CI:
gitlab-ci-multi-runner 0.6.0 (4c26bc5) Using Shell executor... In C:\Users\Administrator.MNB003\AppData\Local\Temp\build_script511373254\script.ps1:20 Zeichen:36
- if($HasNTFSSecurityRemoveItem2 -and Test-Path "builds\21b33c87\0\seba ...
-
~
Sie m�ssen nach dem Operator "-and" einen Wertausdruck bereitstellen. In C:\Users\Administrator.MNB003\AppData\Local\Temp\build_script511373254\script.ps1:20 Zeichen:37
- if($HasNTFSSecurityRemoveItem2 -and Test-Path "builds\21b33c87\0\seba ...
-
~~~~~~~~~
Unerwartetes Token "Test-Path" in Ausdruck oder Anweisung. In C:\Users\Administrator.MNB003\AppData\Local\Temp\build_script511373254\script.ps1:20 Zeichen:37
- if($HasNTFSSecurityRemoveItem2 -and Test-Path "builds\21b33c87\0\seba ...
-
~~~~~~~~~
Schlie�ende ")" fehlt nach einem Ausdruck in der Anweisung "if". In C:\Users\Administrator.MNB003\AppData\Local\Temp\build_script511373254\script.ps1:16 Zeichen:8
- } else {
-
~
Die schlie�ende "}" fehlt im Anweisungsblock oder der Typdefinition. In C:\Users\Administrator.MNB003\AppData\Local\Temp\build_script511373254\script.ps1:20 Zeichen:96
- ... 2 -and Test-Path "builds\21b33c87\0\sebastiann\Powershell-CI-Test") {
-
~
Unerwartetes Token ")" in Ausdruck oder Anweisung. In C:\Users\Administrator.MNB003\AppData\Local\Temp\build_script511373254\script.ps1:23 Zeichen:3
- } elseif(Test-Path "%!s(MISSING)") {
-
Unerwartetes Token "elseif" in Ausdruck oder Anweisung. In C:\Users\Administrator.MNB003\AppData\Local\Temp\build_script511373254\script.ps1:23 Zeichen:36
- } elseif(Test-Path "%!s(MISSING)") {
-
~
Unerwartetes Token "{" in Ausdruck oder Anweisung. In C:\Users\Administrator.MNB003\AppData\Local\Temp\build_script511373254\script.ps1:33 Zeichen:1
- }
- ~
Unerwartetes Token "}" in Ausdruck oder Anweisung.
- CategoryInfo : ParserError: (:) [], ParseException
- FullyQualifiedErrorId : ExpectedValueExpression
ERROR: Build failed with: exit status 1
It's caused by a set of missing braces in the script.ps1 that the runner generates on my machine:
**if($HasNTFSSecurityRemoveItem2 -and `(`Test-Path "builds\21b33c87\0\username\Powershell-CI-Test"`)` ) {**
Here's the runner's script:
```powershell
$ErrorActionPreference = "Stop"
echo "Running on $env:computername..."
if(Test-Path "builds\21b33c87\0\username\Powershell-CI-Test\.git") {
echo "Fetching changes..."
cd "builds\21b33c87\0\username\Powershell-CI-Test"
if (!$?) { Exit $LASTEXITCODE }
git clean -ffdx
if (!$?) { Exit $LASTEXITCODE }
git reset --hard > $null
if (!$?) { Exit $LASTEXITCODE }
git remote set-url origin "http://gitlab-ci-token:94c121528e95be6fef3014e4cc...0@batman.mydomain.com:8888/username/Powershell-CI-Test.git"
if (!$?) { Exit $LASTEXITCODE }
git fetch origin --tags -p
if (!$?) { Exit $LASTEXITCODE }
} else {
echo "Cloning repository..."
$NTFSSecurityRemoveItem2 = Get-Command Remove-Item2 -ErrorAction SilentlyContinue
$HasNTFSSecurityRemoveItem2 = ($NTFSSecurityRemoveItem2 -ne $null) -and ($NTFSSecurityRemoveItem2.ModuleName -eq "NTFSSecurity")
if($HasNTFSSecurityRemoveItem2 -and Test-Path "builds\21b33c87\0\username\Powershell-CI-Test") {
Remove-Item2 -Force -Recurse "builds\21b33c87\0\username\Powershell-CI-Test"
if (!$?) { Exit $LASTEXITCODE }
} elseif(Test-Path "%!s(MISSING)") {
Remove-Item -Force -Recurse "builds\21b33c87\0\username\Powershell-CI-Test"
if (!$?) { Exit $LASTEXITCODE }
}
(Test-Path "builds\21b33c87\0\username\Powershell-CI-Test") -or (New-Item "builds\21b33c87\0\username\Powershell-CI-Test" -ItemType "directory" )
if (!$?) { Exit $LASTEXITCODE }
git clone "http://gitlab-ci-token:94c121528e95be6fef3014e4cc8...@batman.mydomain.com:8888/username/Powershell-CI-Test.git" "builds\21b33c87\0\username\Powershell-CI-Test"
if (!$?) { Exit $LASTEXITCODE }
cd "builds\21b33c87\0\username\Powershell-CI-Test"
if (!$?) { Exit $LASTEXITCODE }
}
echo "Checking out b138396f as master..."
git checkout -qf "b138396f27149ada5944803418091c112f61db1f"
if (!$?) { Exit $LASTEXITCODE }
echo ""
echo "Get-Childitem"
Get-Childitem
if (!$?) { Exit $LASTEXITCODE }
The lines below "Cloning repository" that clean up the remains of previous builds could be rewritten like this to fix the error while staying PSv2 compliant:
# the square brackets prevent pollution of the global error variable
Import-Module -Name NTFSSecurity -ErrorAction SilentlyContinue
if( (Get-Command -Name [R]emove-Item2 -Module NTFSSecurity) -and (Test-Path $BuildPath) ) {
# stuff
}
Can anyone reproduce the issue?