Using Jenkins to run CI on local laptop with VirtualBox VMs - powershell

I am doing local Chef dev on my Windows 8.1 laptop and use VirtualBox 5.1.24 for VMs. I want to use Jenkins (v2.73.2) to run jobs that will automate the restoring of VM snapshots and running of kitchen converge, test and repeat.
I have added the VirtualBox Plugin but it doesn't let me add a node (I recall seeing somewhere that v2.73.2 of Jenkins is not supported).
I wrote some Powershell to do what I want and so after installing PowerShell Plugin created a pipeline with following PowerShell to test it:
& "C:\Program Files\Oracle\VirtualBox\vboxmanage.exe" startvm "W2008_21_06_B"
(which works in PS ISE), but fails in Jenkins job:
Building on master in workspace C:\Program Files (x86)\Jenkins\workspace\vbox
[vbox] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\windows\TEMP\jenkins7025720492184266246.ps1'"
vboxmanage.exe: error: Could not find a registered machine named 'W2008_21_06_B'
vboxmanage.exe: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBoxWrap, interface IVirtualBox, callee IUnknown
vboxmanage.exe: error: Context: "FindMachine(Bstr(pszVM).raw(), machine.asOutParam())" at line 573 of file VBoxManageMisc.cpp
Build step 'Windows PowerShell' marked build as failure
Finished: FAILURE
What trick am I missing please?
Regards

Related

Execute powershell script with gitlab-runner on local windows machine

I do have following setup:
a win PC with gitlab-runner installed (working)
a powershell script running on the same PC is starting an application
a gitlab server to connect this local PC and starting the powershell script
Now when starting the powershell script directly from the local PC, the application starts and terminates after done - working as expected. When starting the same powershell script with the gitlab server (yml-file) then I can see that the application has been started (new process in taskmanager) but it is not running as well it never terminates.
When manually end the task I see that gitlab terminates again.
Question:
what could be the root cause?
is it possible to run the powershell script with gitlab-runner? I think there is a way with the command "exec". How does the command looks like when calling the powershell script?
is it possible to run the application not in the background in order to see whats going on?
others?
thanks in advance
I think there is a bug with the gitlab runner on windows.
No matter which shell you configure in the config.toml the runner
will always use cmd.exe for an exec local run.
Specify the --shell argument to override the default cmd.exe shell:
> gitlab-runner exec shell your_job --shell pwsh
If you run this locally in your project, it outputs to .builds/, so add this to your .gitignore because git will see it and think you might want to add a submodule.

Prevent Gitlab adding -NoProfile to PowerShell when starting a docker container

1. How can I prevent Gitlab from adding -NoProfile when creating the docker container?
2. What's the difference between using powershell with -NoProfile and without it?
3. Is there any way if I run powershell -NoProfile to somehow load/run the default profile and to revert the effect of setting -NoProfile flag?
And now the story behind these questions:
MSBuild (Visual Studio build tool command) fails to build Xamarin app when used in docker container started by Gitlab with Powershell -NoProfile.
I created a docker image for CI purspose and everything is working properly if I run the container manually, but when it is run during Gitlab runner job, it fails (more exactly msbuild /t:SignAndroidPackage fails, because of some file does not gets generated). I inspected this (put a sleep of 1 hour in gitlab-ci.yml and attached to the container on the runner machine) and found out that Gitlab starts the container with PowerShell -NoProfile...I tried that manually(start the container with -NoProfile) and I reproduced the issue.
the error is:
Could not find a part of the path 'C:\builds\gitlab\HMI.Framework\KDI\sub\HostApplicationToolkit\sub\KBle\KBle\sub\XamarinBluetooth\Source\Plugin.BLE.Android\Resources\Resource.Designer.cs'
Here the Resource.Designer.cs is missing (and it should be auto-generated during the build process)
This is the dockerFile:
# escape=`
# Use the latest Windows Server Core image with .NET Framework 4.8.
FROM mcr.microsoft.com/dotnet/framework/sdk:3.5-windowsservercore-ltsc2019
ENV VS_STUDIO_INSTALL_LINK=https://download.visualstudio.microsoft.com/download/pr/befdb1f9-8676-4693-b031-65ee44835915/c541feeaa77b97681f7693fc5bed2ff82b331b168c678af1a95bdb1138a99802/vs_Community.exe
ENV VS_INSTALLER_PATH=C:\TEMP\vs2019.exe
ENV ANDROID_COMPILE_SDK=29
# Restore the default Windows shell for correct batch processing.
SHELL ["cmd", "/S", "/C"]
ADD $VS_STUDIO_INSTALL_LINK $VS_INSTALLER_PATH
RUN %VS_INSTALLER_PATH% --quiet --wait --norestart --nocache --includeRecommended --includeOptional`
--add Microsoft.VisualStudio.Workload.NetCrossPlat `
--add Microsoft.VisualStudio.Workload.XamarinBuildTools `
|| IF "%ERRORLEVEL%"=="3010" EXIT 0
RUN del %VS_INSTALLER_PATH%
# set some util paths
RUN setx JAVA_HOME "c:\Program Files\Android\jdk\microsoft_dist_openjdk_1.8.0.25\"
RUN setx path "%path%;c:\Program Files (x86)\Android\android-sdk\tools\bin;c:\Program Files (x86)\Android\android-sdk\platform-tools"
# update android SDK with API 29
RUN echo y| sdkmanager "platforms;android-%ANDROID_COMPILE_SDK%"
#ENTRYPOINT ["powershell.exe", "-NoLogo", "-ExecutionPolicy", "Bypass"]
This is the gitlab-ci.yml
image: visualstudio2019-xamarin-ci:1.0-windowsservercore-ltsc2019
stages:
- build
- test
variables:
GIT_SUBMODULE_STRATEGY: 'recursive'
build_kdi:
stage: build
only:
- CI_CD
tags:
- docker-windows
script:
- '& "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe" ./src/KDI/KDI.sln /t:Restore /p:AndroidBuildApplicationPackage=true /p:Configuration=Release'
- '& "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe" /p:Configuration=Release /t:SignAndroidPackage /property:SolutionDir="C:\builds\gitlab\HMI.Framework\KDI\src\KDI\" /p:AndroidSigningKeyStore="C:\builds\gitlab\HMI.Framework\KDI\distribution\Android\KrohneAndroidKeystore.keystore" .\src\KDI\KDI\KDI.Android\KDI.Android.csproj'
run_bb_tests:
stage: test
only:
- CI_CD
tags:
- docker-windows
script:
- docker-ci/install_and_run_bb_tests.bat "phone_ip" "5555" arg1 arg2 || true
- adb pull /storage/emulated/0/Android/data/myApp/files/Exports/BBT_Exports/BBTests_CI C:\test_results
artifacts:
paths:
- c:\test_results\BBTests_CI #expire in 1 month by default
If I start this image using : docker run -it myImage powershell everything works well, but if I run it docker run -it myImage powershell -NoProfile, msbuild fails at some step in building the xamarin app
How can I prevent Gitlab from adding -NoProfile when creating the docker container?
Apparently you can't...yet but there is a feature request in place
What's the difference between using powershell with -NoProfile and without it?
A PowerShell profile is just a file that contains things, like custom functions, for a specific user. For instance, if I were to have Function Banana(){Write-Host "Awesome"} in my PowerShell profile e.g $Home[My ]Documents\PowerShell\Profile.ps1. Whenever I open up PowerShell, it loads that function automatically. So I can open PowerShell, type banana and have the string Awesome written to stdout. If I were to start powershell with -NoProfile, and enter banana, I'd get CommandNotFoundException.
source
Is there any way if I run powershell -NoProfile to somehow load/run the default profile and to revert the effect of setting -NoProfile flag?
Maybe. And I only say maybe because I haven't personally tested it, but in theory it should work. If the profile is in the container, all you have to do is dot source it.
. "C:\path\to\powershell\profile.ps1"
dot sourcing the .ps1 file will run anything within it, inside the current scope, which includes loading functions, if any are present.
dot source docs
My expertise on Gitlab is limited, but due to my overall experience with Windows containers, PowerShell, as well as the nature of PowerShell profiles, I have doubts that the profiling is the root cause of this issue, but I don't have enough information/evidence to definitively say. Hope this helps in some way!

SCCM2012 SP2 Scripted Deployment fails, running script manually succeeds though

I am installing a program through SCCM2012 SP2 with a PowerShell Script and it fails with an install shield ResponseCode -3, running the Powershell script manually from C:\windows\ccmcache#\ succeeds though...
This is my command line (it works for dozens of other scripts - including installshield packaged scripts)...
"%WINDIR%\sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy RemoteSigned -file .\InstallTigerpaw.ps1
If it's the system error code 3, it means 'File Not Found'. Running manually from cache is different with executing by CM agent. Below is something I will take into consideration when try to deploy a PowerShell Script using Program.
First,
To deploy a script using Configuration Manager, I always test it first by running it manually under System Context, which is the same context that CM deployments run under. I use PSExec tool to open a new command prompt under system context by running psexec /s cmd.exe in a common command prompt.
Second,
The command you use in the program. In your case, use Sysnative only when you intend to access system32 folder on a 64 bit operating system. If not, then don't need it.
Third,
The Execution Policy. Microsoft allows you to temporarily bypass the execution policy to get work done, that's a reason that I usually use Bypass. Example like:
PowerShell.exe -ExecutionPolicy Bypass -File ".\PowerShellScriptFileName.ps1"
Most of the time the deployment goes fine on clients. In case of problems, I will look at client side log, execmgr.log.
In some cases, we may need to set 'Only when a user is logged on', 'Run with administrative rights' for specific needs of running scripts.

Cannot run TopShelf install remotely using psexec

I am trying to install a windows service on a remote machine. The service is implemented using TopShelf. I am running the following psexec command:
psexec \\remoteServerName "C:\PathToExe\TopShelfServiceName.exe" "install"
The error I am receiving is:
ERROR - The service terminated abnormally
Topshelf.HostConfigurationException: The service was not properly configured:
[Failure] Command Line An unknown command-line option was found: ARGUMENT: "install"
It appears that I am not running the TopShelf installer, rather executing my exe and passing "install" which is not a valid argument.
Any help is appreciated!
I don't think you need quotes around "install":
psexec \\remoteServerName "C:\PathToExe\TopShelfServiceName.exe" install

Hudson cannot find powershell after update to powershell 3

One of my jobs had need for the ConvertTo-Json commandlet available in powershell 3.0 So I installed WMF 3.0 on our Windows Server 2008 R2 (SP1) box. After a restart all my powershell using jobs result in the error:
[workspace] $ powershell.exe "& 'C:\Users\HUDSON~1\AppData\Local\Temp\hudson1263303013566726397.ps1'"
The system cannot find the file specified
FATAL: command execution failed
java.io.IOException: Cannot run program "powershell.exe" (in directory "C:\hudson\jobs\MyProject\workspace"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
at hudson.Proc$LocalProc.<init>(Proc.java:192)
at hudson.Proc$LocalProc.<init>(Proc.java:164)
at hudson.Launcher$LocalLauncher.launch(Launcher.java:639)
at hudson.Launcher$ProcStarter.start(Launcher.java:274)
at hudson.Launcher$ProcStarter.join(Launcher.java:281)
at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:84)
at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:60)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:19)
at hudson.model.AbstractBuild$AbstractRunner.perform(AbstractBuild.java:630)
at hudson.model.Build$RunnerImpl.build(Build.java:175)
at hudson.model.Build$RunnerImpl.doRun(Build.java:137)
at hudson.model.AbstractBuild$AbstractRunner.run(AbstractBuild.java:429)
at hudson.model.Run.run(Run.java:1366)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
at hudson.model.ResourceController.execute(ResourceController.java:88)
at hudson.model.Executor.run(Executor.java:145)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:188)
at java.lang.ProcessImpl.start(ProcessImpl.java:132)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1021)
... 16 more
Running powershell.exe from the cmd prompt works just fine.
What happened? How do I fix this?
I am using the Powershell Plugin to launch the scripts and I cannot find any configuration area for it.
After a day's worth of hassle we found the answer. We're still not sure exactly why it happened but the powershell directory disappeared from Hudson's path. Then, although PS3 was installed and the machine had been restarted, Hudson for some reason had not picked up the new PATH variable.
Manually shutting down and restarting Hudson finally got it to pick up the correct PATH and fixed the issue.
George:
I had the exact same problem with the Task Scheduler, right after installing PowerShell 3.0. Running simply "powershell" or "powershell.exe" as a Scheduled Task failed. Rebooting the machine fixed it (I couldn't just restart the Task Scheduler on Windows Server 2008 R2).
My guess is what's happening is that after the reboot when installing PS 3.0, the path is set, and by that time all of your automatic services (like Task Scheduler or Hudson) have started.
So let this be a warning to friend and foe alike. When you install PowerShell 3.0, reboot twice!