Restore snapshot before build in jenkins using virtualbox plugin - jenkins

I'm using VirtualBox plugin on jenkins, to launch different builds retrieving sources from Git. I would like the environment on each VM to be clean by restoring a snapshot before starting them for a build.
I tried several things as https://wiki.jenkins-ci.org/display/JENKINS/Slave+Setup+Plugin.
Finally I don't think it it's a good solution as it is called after the git repository being clone... But I also tried to launch the slave "via the execution of a command on master", that takes as parameter the name of the vm and calls this script:
VBoxManage registervm "C:\Users\mfauvet\VirtualBox VMs\%1\%1.vbox
VBoxManage controlvm %1 poweroff
VBoxManage snapshot %1 restore SetEnvironment
VBoxManage startvm %1
ssh 192.168.120.1 -p 1310
But I have the following error:
C:\Program Files (x86)\Jenkins>"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" registervm "C:\Users\mfauvet\VirtualBox VMs\Jenkins-Ubuntu13.10\Jenkins-Ubuntu13.10.vbox"
VBoxManage.exe: error: Trying to open a VM config 'C:\Users\mfauvet\VirtualBox VMs\Jenkins-Ubuntu13.10\Jenkins-Ubuntu13.10.vbox' which has the same UUID as an existing virtual machine
VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component Machine, interface IMachine, callee IUnknown
VBoxManage.exe: error: Context: "OpenMachine(Bstr(a->argv[0]).raw(), machine.asOutParam())" at line 88 of file VBoxManageMisc.cpp
C:\Program Files (x86)\Jenkins>"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm Jenkins-Ubuntu13.10 poweroff
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
C:\Program Files (x86)\Jenkins>"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" snapshot Jenkins-Ubuntu13.10 restore SetEnvironment
0%...10%...20%...30%...40%...50%...
Progress object failure: RPC_S_SERVER_UNAVAILABLE 0x800706BA
Restoring snapshot 7f7cc1a3-7128-426a-bf74-9ffb0b3fbca8
C:\Program Files (x86)\Jenkins>"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" startvm Jenkins-Ubuntu13.10
VBoxManage.exe: error: Could not find a registered machine named 'Jenkins-Ubuntu13.10'
VBoxManage.exe: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBox, interface IVirtualBox, callee IUnknown
VBoxManage.exe: error: Context: "FindMachine(Bstr(pszVM).raw(), machine.asOutParam())" at line 575 of file VBoxManageMisc.cpp
Does someone know how I could solve this ? Or is there a better way to restore a snapshot on my vm before launching them via Jenkins ?
Thanks for reading

I got this working, admittedly with VMWare.
Steps I took
add swarm plugin to master
Setup virtual machine and add swarm client
make sure client connects - will be in nodes in master
take snapshot with the slave client running (or initiate it on startup)
create a job on master which runs on your swarm slave
add multijob plugin to master create multijob project with normal steps to revert the VM
add a multijob phase
add the swarm slave job into the multijob phase
When running the job, the swarm slave job will wait for the VM to start before submitting jobs to it

Related

unable to use zsh/bash in Jenkins Execute Shell

I am trying to run iOS builds in a remote Mac mini connected as an agent to Jenkins master running on Linux server. I have all the dependencies installed and env variables set up via .zshrc(zsh is the default shell in catalina and above).
When I run scripts(npm install, pod install and so on) through Execute Shell option in Jenkins Freestyle project, it throws npm command not found/ pod command not found. Even when I shebang with /bin/zsh at the start of script, it doesn't work.
Is there a way to switch shell to zsh/bash or import all configurations into sh in the jenkins build process?
Jenkins leverage the 'rc' file when it connects to the slave (via ssh) and load the env variable (This also means when you will change anything in the rc file you need to disconnect and reconnect the slave to load the changes). Now it depends on the default SHELL set for the User you used to connect the slave. You need to check that and align the env variable accordingly in the right rc file.
In the current situation, if you don't want to change anything you could also do a
source ~/.zshrc
at the start of your script. This will load the env vars defined in that file
One more way is to set the env variable for a node is under "Node Properties" in node configuration.

Jenkins throws "Executable not found in $PATH"

I have installed kompose in my Jenkins machine through CLI and it is successfully installed.
I am trying to build a job which uses this "kompose" executable but it fails with "Exectable not found in the $PATH" error.
Error:
+ skaffold init --compose-file docker-compose.yaml
time="2019-11-13T10:39:00Z" level=fatal msg="running kompose: exec: \"kompose\": executable file not found in $PATH"
Please let me know if I need to make anymore changes to communicate the executable to Jenkins
On your Jenkins job has executed, you can review its environment variables (on the left side of the job execution instance page)
Check the PATH value and see if it includes where you have installed kompose.
Check also which user is actually running Jenkins: its $PATH might differ from your current local user.

Jenkins user not in passwd on dynamic jnlp slave in kubernetes

I am building a system to do c++ cmake builds primarily. I have Jenkins firing the dynamic pods, firing off shell scripts, etc. But, I can't get it to checkout the code. Now, my Jenkinsfile launches a container that the actual compile is supposed to be run in. That "sub" container is tuned to compile C++ code. Now, I have jenkins running scripts and such in that pod, but, when i try
checkout scm
im getting errors saying
ERROR: Error cloning remote repo 'origin'
hudson.plugins.git.GitException: Command "git fetch --tags --force --progress git#gitlab.com:mystuff/hello-world-cmake.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout:
stderr: No user exists for uid 1000080000
fatal: Could not read from remote repository.
my home folder is the standard /home/jenkins and the workspace folder is there, etc, etc. But, when I dump the /etc/passwd file, the jenkins user isn't listed in it.
Whats the appropriate way to add the jenkins user to that file?
What image are you using for Jenkins slave? Does it have user jenkins? If it has you need to specify this in your spec for Jenkins slave:
spec:
securityContext:
runAsUser: 1000
UPDATE:
You cannot run default Jenkins image in Openshift, because Openshift runs containers as random user. You should run Jenkins from builtin Jenkins template "Jenkins Persistent". If you don't have this template and don't have Jenkins image stream - you can try to use image openshift/jenkins-2-centos7. See details at:
https://github.com/openshift/jenkins/issues/168
https://github.com/openshift/jenkins

How do I start a Jenkins agent from the slave?

Jenkins v2.89.3.
I'm trying to start an agent but am getting the following error.
$ java -jar agent.jar -jnlpUrl http://localhost:8080/computer/testagent/slave-agent.jnlp
Error: Unable to access jarfile agent.jar
The documentation from the wiki that I'm following is from here:
Launch agent headlessly -
https://wiki.jenkins.io/display/JENKINS/Distributed+builds#Distributedbuilds-Differentwaysofstartingagents
The handbook section for managing nodes doesn't appear to have been written yet:
https://jenkins.io/doc/book/managing/nodes/
If anyone know what I'm doing wrong, or another way I can start the agent from the slave (not from the master), please let me know.
In the "Run from agent command line..." CLI instructions that were given by the Jenkins master: The word 'agent.jar' is a hyperlink.
Hover over that hyperlink to copy the target URL.
Use that URL with wget to retrieve agent.jar from the Jenkins master by pasting the URL into your terminal window. e.g.,
[root#Jenkins-Agent-1 ~]# wget http://jm0:8080/jnlpJars/agent.jar
--2022-03-15 01:13:45-- http://jm0:8080/jnlpJars/agent.jar
Resolving jm0 (jm0)... 192.168.0.174
Connecting to jm0 (jm0)|192.168.0.174|:8080... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1524968 (1.5M) [application/java-archive]
Saving to: ‘agent.jar’
100%[====================================================================================>] 1,524,968 --.-K/s in 0.04s
And then finish the installation by executing the command that was given by your Jenkins master. e.g.,
[root#Jenkins-Agent-1 ~]# java -jar agent.jar -jnlpUrl http://jm0:8080/computer/JA1/jenkins-agent.jnlp -secret 73393f441b43921357d959cd664d9e69d67ff7ee1f9876070ab76ee46b420e19
I found that the Swarm plugin for Jenkins did exactly what I needed.
Install plugin on Jenkins
Download swarm jar on slave
java -jar path/to/swarm-client.jar -home http://localhost:8080
Result: Slave agent registers on master as seen from the Manage Nodes section of the Jenkins UI
https://wiki.jenkins.io/display/JENKINS/Swarm+Plugin

Is there any way to start appium server silently?

Use case: I need to start appium server on CI Jenkins and run tests right after that. Tests don't start because appium server starting in debug mode and doesn't switch to another command.
So i have jenkins on Windows machine with the following build steps (as Windows batch command):
start /B node path_to_appium_server\appium.js --address 127.0.0.1 --port 4723
timeout 10
"path_to_tests_runner\vstest.console.exe" "path_to_dll\test.dll"
And in this case, my tests cannot started because jenkins terminate first process (with appium).
Basic issue was with permissions for '*.dll' file which contains tests and which cannot be ran with bat file without 'runas' command (which is waiting for password) from Jenkins.
So my Jenkins job contains 3 Build steps:
execute Windows batch command
start node path_to_appium_server\appium.js --address 127.0.0.1 --port 4723
Run unit tests with VSTest.console (to get this build option you need install VSTest Runner plugin)
specify path to dll and command line parameters
execute Windows batch command
taskkill /F /IM node.exe
Second step resolve permission issue for dll file.
It depends on how are you starting it. In most situations if on Jenkins you have 2 ways:
Start appium and tests in different build "shell execution" steps
If you want to do it in the same build step, just start it in a background with "START /B program".
Requirement
Installed Node.js 0.10 or greater.
At least an appium server instance installed via npm.
using javaclient 3.2.0
AppiumDriverLocalService service =AppiumDriverLocalService.buildDefaultService();
service.start();
service.stop();

Resources