-
Steve Xuereb authored
We are using if/else when we want to handle conditional for PowerShell, this can be an issue when the user set `ErrorActionPreference` to `Stop` the clone script will fails as described in #4188, the only way to do error handling in this case is using try/catch blocks. For example the clone script looks like this: ``` & "git" "remote" "add" "origin" "http://gitlab-ci-token:1sBznS3-vo5JCqzRFe1F@192.168.1.79:3000/root/ci-scratch-pad.git" 2>$null if($?) { echo "[32;1mCreated fresh repository.[0;m" } else { & "git" "remote" "set-url" "origin" "http://gitlab-ci-token:1sBznS3-vo5JCqzRFe1F@192.168.1.79:3000/root/ci-scratch-pad.git" if(!$?) { Exit $LASTEXITCODE } } ``` Now it looks like this: ``` Set-Variable -Name cmdErr -Value $false Try { & "git" "remote" "add" "origin" "https://gitlab.com/gitlab-org/ci-cd/tests/gitlab-test.git" 2>$null if(!$?) { throw $LASTEXITCODE } } Catch { Set-Variable -Name cmdErr -Value $true } if(!$cmdErr) { echo "[32;1mCreated fresh repository.[0;m" } else { & "git" "remote" "set-url" "origin" "https://gitlab.com/gitlab-org/ci-cd/tests/gitlab-test.git" if(!$?) { Exit $LASTEXITCODE } } ``` The only drawback of this is that we have to call `else` every time we use an `if`. If we put the `catch` line inside of the `ifCmd` we can on longer put more the 1 line inside of the `if` which is not ideal.
Steve Xuereb authoredWe are using if/else when we want to handle conditional for PowerShell, this can be an issue when the user set `ErrorActionPreference` to `Stop` the clone script will fails as described in #4188, the only way to do error handling in this case is using try/catch blocks. For example the clone script looks like this: ``` & "git" "remote" "add" "origin" "http://gitlab-ci-token:1sBznS3-vo5JCqzRFe1F@192.168.1.79:3000/root/ci-scratch-pad.git" 2>$null if($?) { echo "[32;1mCreated fresh repository.[0;m" } else { & "git" "remote" "set-url" "origin" "http://gitlab-ci-token:1sBznS3-vo5JCqzRFe1F@192.168.1.79:3000/root/ci-scratch-pad.git" if(!$?) { Exit $LASTEXITCODE } } ``` Now it looks like this: ``` Set-Variable -Name cmdErr -Value $false Try { & "git" "remote" "add" "origin" "https://gitlab.com/gitlab-org/ci-cd/tests/gitlab-test.git" 2>$null if(!$?) { throw $LASTEXITCODE } } Catch { Set-Variable -Name cmdErr -Value $true } if(!$cmdErr) { echo "[32;1mCreated fresh repository.[0;m" } else { & "git" "remote" "set-url" "origin" "https://gitlab.com/gitlab-org/ci-cd/tests/gitlab-test.git" if(!$?) { Exit $LASTEXITCODE } } ``` The only drawback of this is that we have to call `else` every time we use an `if`. If we put the `catch` line inside of the `ifCmd` we can on longer put more the 1 line inside of the `if` which is not ideal.
Loading