Gitlab CI Powershell with Write-Output - powershell

I have a powershell build script that I am executing from Gitlab CI Pipelines.
When run manually (on the build server) the build script runs fine, but when executed by the Gitlab CI runner it:
Times out after an hour (runs for about 20 mins if run manually)
Does not echo Write-Output statements into the build log
So there is something going wrong when executed from Gitlab CI. However, as the Write-Output statements aren't displayed in the Build Log there is no real way to troubleshoot this.
What do I need to do to get the Write-Output statements to display in the build log? I would have assumed any STDOUT messages would show there, but they're not coming through.

The answer here was to set PowerShell as the shell to use in the gitlab runner.
This is done by adding the following line to the gitlab runners config.toml file:
shell = "powershell"
Now the file executes correctly and Write-Output statements are echo'ed in the build log.

Related

Executing with Appium server in Azure Pipelines using yaml file and console

What is the best way to execute Appium server in Azure pipelines console using Microsoft hosted agent? Or is it even feasible? I´m using vmImage: 'vs2017-win2016'
I´ve managed to start the server with Cmdline and powershell script but I´m unsure if the server is actually running in the background.
So far I´ve tried -
Plain Cmdline script. "appium -p 4723". This starts the server but stays in the Cmdline job forever.
Plain Cmdline script with start call. "start appium -p 4723". I believe this would start Appium in another console, but I´m usure if the server keeps running in the background. Cmdline job passes to the next one. I don´t see the standard purple Appium server launch commands.
Powershell Start-Process command. "Start-Process appium -p 4723". Same impact as with 2.
Is there a way to verify that appium server exists?
I´m trying to achieve mobile app automated testing using Azure Repo&Pipelines, Robot Framework, Appium and Android studio emulator. For this purpose, I need Appium server running in the background. I´m doing the needed tool installations in my pipeline before entering to Appium running stage.
Is there a way to verify that appium server exists?
Try command like TASKLIST /FI "IMAGENAME eq cmd.exe" /V.
For me, I use three cmd scripts (CMD task) to do the test, task1 => npm install -g appium, task2 => start appium -p 4723, task3 => TASKLIST /FI "IMAGENAME eq cmd.exe" /V. And here's the output of task3:
Since the cmd.exe will be killed when the task is done, so cmd.exe(task1) and cmd.exe(task2) doesn't exist.
Till the third task, only the newly created console (appium -p 4723) and the cmd.exe(task3) do exist. And the appium -p 4723 is what you want, it do exist in background if we don't kill it.
Note:
1.I used the second command start appium -p 4723 to test. It's expected behavior that your first command would start the server but it stays in the Cmdline job forever. Azure Devops pipeline won't go to next task, if current task hasn't completed. So if the cmd task is one listener that keeps running, it stays in the Cmdline task forever till timeout.
Second command is better in Azure Devops pipeline environment.
2.What you use is microsoft-hosted agent, it will be recovered when the pipeline is completed. So your appium listener will be killed after the the job/pipeline completes.
3.About Appium test in Azure Devops, you can check this document.
with powershell command, I can start appium, try below code:
- powershell: Start-Process appium -PassThru 4723 displayName: "start appium process"
After starting the appium server in azure devops, it cant execute the next task. I want after starting the server, next task will be executed.

Jenkins SonarQube for .NET Core Fails, but powershell script works

I am trying to run a Jenkins pipeline for SonarQube analysis. I have written a powershell script that runs the analysis just fine; however it fails on the end step when run in the pipeline.
Here is the correct result from PowerShell:
https://pastebin.com/eCaDXgmi
Here is the failed result from Jenkins:
https://pastebin.com/mXgTiq5g
Here is the source of the PowerShell script:
& 'C:\Program Files (x86)\Jenkins\tools\hudson.plugins.sonar.MsBuildSQRunnerInstallation\SQScannerMSB\SonarQube.Scanner.MSBuild.exe' begin /k:HC /n:HC /v:1.0 /d:sonar.host.url=http://localhost:9000 /d:sonar.login=REDACTED
& 'C:\Program Files\dotnet\dotnet.exe' msbuild /t:Rebuild
& 'C:\Program Files (x86)\Jenkins\tools\hudson.plugins.sonar.MsBuildSQRunnerInstallation\SQScannerMSB\SonarQube.Scanner.MSBuild.exe' end /d:sonar.login=REDACTED
Here is the relevant Jenkins Pipeline Code:
stage('SonarQube') {
powershell(returnStdout: true, script: '.\\sonar.ps1')
}
From the logs it looks like it might be the same problem as in Sonarqube with dotnet core works fine on command line but not from Jenkins i.e. running as a non-local account.
From v4.1, the Scanner for MSBuild will give a specific warning in this case - see SonarMSBRU-374.

Jenkins Pipeline, PowerShell script hangs

I want to run a Powershell file from the Jenkins Pipeline, using the powershell command. Should be easy, right?
node()
{
stage ('Hello World') {
powershell returnStatus: true, script: 'C:\\HelloWorld.ps1'
}
}
C:\HelloWorld.ps1 is a one-liner:
Write-Host "Hello World"
But running the job causes the process to hang. Here is the console output:
Started by user Administrator
[Pipeline] node
Running on master in C:\Jenkins\workspace\HelloWorld
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello World)
[Pipeline] powershell
[HelloWorld] Running PowerShell script
And then it just hangs until I cancel the job.
What to do?
I had the same issue and managed to solve it with downgrading DurableTask plugin from 1.17 to 1.15. It seems to be part of JENKINS-46496 bug. The older plugin version is there 1.15.
You can update the plugin to a needed version in Manage Plugins->Advanced Tab->Upload Plugin button.
I had the same issue and the problem was that the job had spaces in its name.
This issue should be fixed in the next release: https://github.com/jenkinsci/durable-task-plugin/pull/51
I have the same problem in about 70 % of my cases. I have a pipeline script that kicks off about 20 parallel jobs spread out over different agents. Each of these tries to run a Powershell script initially. About 30 % of the jobs succeed to run the script, the rest hangs just like it does for Ola.
The build agents are configured exactly the way (clones in a VM cluster). Powershell v4 is used. Upon several tries one single build agent sometimes succeeds in running Powershell, sometimes don't.
I've been running this pipeline for 5-6 weeks and it's just the last week this behavior has been seen. Fortunately the script isn't in production yet :-)
I haven't had the time to do a full investigation. Could it be that some of the pipeline plugins have been updated and introduced this bug?

Setting up Build Pipeline for Azure Bot Service/Azure Functions solution in VSTS Build

I have an Azure bot service solution, which is in my VSTS Git repository.
I am using Task Runner in visual studio to compile, run and debug code on my local machine.
Similarly, I want to build and compile the code in my VSTS build pipeline, like how we do build using Visual Studio template for .Net applications
I am very new to Bot service projects where it having C# Script files.
I have seen msdn documents all are mentioned Continuous Integration where it will directly link to my Git repo branch. Whenever I commit code it automatically push to My Azure Bot Service, here I want to make sure the code I commit should be compile before push to Azure Bot service. For that I want to setup a Build pipeline.
Can anyone know how to setup a build pipeline for this kind of projects which having C# Script files?
UPDATE:
In my local PC I have installed Azure Functions CLI tools, and Command Task Runner extension to visual studio. I followed the below link to enable debugging locally
enter link description here
Task runner running debughost.cmd file which is in my Bot Service code, it contains the following code
#echo off
set size=0
call func settings list -data > %temp%\settings-list
call :filesize %temp%\settings-list
if NOT %size% == 0 goto show
#echo ----------------------------------------------------------------------
#echo To fetch your bot service settings run the following command:
#echo func azure functionapp fetch-app-settings [YOUR_BOT_SERVICE_NAME]
#echo func azure functionapp fetch-app-settings AthenaDevbvpn6xsu2tz6i
#echo ----------------------------------------------------------------------
goto start
:show
type %temp%\settings-list
erase %temp%\settings-list
:start
#func host start -p 3978
goto :eof
:filesize
set size=%~z1
exit /b 0
Output in task runner is
There isn't any out of box task to compile CSX file for now. Following is the workaround I can think for your scenario but which is not perfect:
Deploy your own build agent and then configure it by following the steps in the link you provided: Debugging C# bots built using the Azure Bot Service on Windows.
Create a powershell script to call the Azure Function CLI to compile the csx file like the "debughost.cmd" did and check if there is any error occur during the compilation.
Upload the powershell script into the source control.
Add a powershell script task in your build definition to call the powershell script you created.
Save and queue the build definition.
Here is the powershell script I created for your reference:
##Run Azure Func command to compile csx file
$compile = Start-Process 'func' -passthru -WorkingDirectory '.' -ArgumentList 'host start -p 3739' -RedirectStandardOutput 'output.txt'
##You need to set the sleep time base on the build time of your project
Start-Sleep -s 20
Stop-Process $compile -Force
Stop-Process -Name 'Func'
##Get the output from Func and check if there is error in the output
$boutput = Get-Content 'output.txt'
Write-Host 'Azure Function CLI Log:'
Write-Host '*****************************************************************************************************************************'
$boutput
Write-Host '*****************************************************************************************************************************'
$reg = "Function.compilation.error"
foreach($line in $boutput){
if($line -match $reg)
{
##Fail the task if function compilation error exist
Write-Host '##vso[task.logissue type=error]Error occur during function compilation'
Exit 1
}
}
Write-Host 'Function compilation success!'
And you will get this result if the compilation failed:
For Azure Bot Service, set continuous integration with master branch of your repository in VSTS, for repository in VSTS, you can create a new branch, such as Dev, then do work with Dev branch and merge to master. After that the code will be updated to azure.
Simple Steps:
Set continuous integration to your repository (master branch) in VSTS
Go to Code page of your repository in VSTS
Select Branches
Click New branch (e.g. dev)
Clone dev branch to your local and work with it (e.g. modify)
Push changes to remote Dev branch
Create a build definition
Enable Allow Scripts to Access OAuth Token option in Options tab.
Add a step to build app (e.g. gulp) according how do you build in local
Add Command Line step
Add Command Line step
Add Command Line step

PowerShell script called from within the Jenkins PowerShell build step hangs indefinitely

I have a Jenkins (1.493) project that uses the Jenkins PowerShell build step to execute a PowerShell script. Inside that script I want to invoke another script that is stored inside a file. I have now reduced it to the following:
Script inside Jenkins PowerShell build step:
& "\\stemmer.local\sidevelopment\cvdev\devbase\jenkins\PowerShell\Test.ps1"
Content of Test.ps1:
write-host 'Hello world!'
Whenever this Jenkins project executes, the PowerShell build step hangs indefinitely.
Things I have tried/verified so far:
Adding some output before the invocation of Test.ps1 shows me that the Jenkins PowerShell script is being execute normally up to the point where Test.ps1 is called.
The file Test.ps1 exists and is reachable from the build slave that executes the script. If I alter the file's name, I get the expected error message from PowerShell...
Exchanging the " for ' in the 1st script does not change anything. Also, using dot-sourcing rather than & does not help.
The file Test.ps1 can be executed properly from the powershell itself using the same command line that is being used in the Jenkins PowerShell script.
The execution policy for PowerShell has been set to unrestricted on my development host as well as on the Jenkins build slave.
I've tried replacing the PowerShell build step with a Windows batch command build step that looks like this:powershell.exe -InputFormat None -File "\\stemmer.local\sidevelopment\cvdev\devbase\jenkins\PowerShell\Test.ps1"and played around a little with the parameters of powershell.exe, but the results were - in those cases that were syntactically and otherwise correct as far as I can tell - always the same.
I only found few references to problems that sounded similar, but none of the approaches mentioned elsewhere did help me fix this. I am absolutely puzzled, and wondering whether someone encountered this issue before (and maybe even got a scenario like the one I have in mind running).
Thanks a lot for any input!
Volker
have you tried to set execution policy to bypass ?
Copy the script file locally, then invoke it from within the Jenkins PowerShell plugin - that way it works as expected.