Jenkins won't recognize local commands - ruby

I'm attempting to integrate my Ruby/Cucumber/Appium test framework with our Jenkins environment.
On the local machine I have installed RVM to utilize ruby 2.4, the newest appium, cucumber, and I'm attempting to use the already-installed ruby gems for my jenkins jobs.
How do I get the Jenkins shell to utilize my local RVM instance? I don't want to have to install rvm, cucumber, and appium every time I run a job.
I have already attempted to add environment variables in the Jenkins settings but it still does not recognize my commands.
Note my admin has deleted the automatically generated jenkins user.

you probably want something like this in "Execute shell":
#!/bin/bash
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session.
rvm use 2.4
bundle install
bundle exec rake
you mention that your jenkins user is gone. the above assumes that there is an rvm setup in the $HOME directory of the user running your builds, so if that's not certain, or you don't know what user is executing your builds, you probably want to start there, echo $USER, and so on, and see if rvm works for that user outside of a jenkins build.

Related

rbenv is not initialized properly from automatic deployment

I am writing app for deployment. I have working rbenv environment on Debian 10. For all users, rbenv is initalized properly, so in project directories proper ruby version is used.
But when I try to run from web app using Kernel.system or just ``, it fails, it is used Ruby version of my web-app. I am trying to initalize it but in $PATH is still bad version.
If I use /var/www/.rbenv/bin/rbenv local then ruby -v prints proper version, but mina deployment fails trying to load gems from local vendor directory of web app project.
When I do this from padrino console usning system():
/var/www/.rbenv/bin/rbenv local && ruby -v
It returns
"2.6.5\nruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]\n"
So rbenv local works fine but another command uses bad ruby version.
Please is there any way to initialize proper version?
EDIT: Answers to questions.
It is web app for deploying on local server of another ruby apps, we have a lots of versions. Web app is running (just this app) on Ruby 3.0.0. I am executing command inside directory of app which I want to deploy (Ruby 2.6.5) using mina, but it failed.
which ruby from padrino console
/var/www/.rbenv/versions/3.0.0/bin/ruby\n
And PATH
=> "/var/www/production/deploy/releases/77/vendor/bundle/ruby/3.0.0/bin:/var/www/.rbenv/versions/3.0.0/bin:/var/www/.rbenv/libexec:/var/www/.rbenv/plugins/ruby-build/bin:/var/www/.rbenv/plugins/rbenv-vars/bin:/var/www/.rbenv/shims:/var/www/.rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\n"
As you can see, it is 3.0.0.
When I try to run command in kernel system this
cd /var/www/deploy/build/project_to_be_deployed && mina deploy it fails because it tries to use ruby 3 instead of 2.6.5, if I try to switch using rbenv using rbenv local or changing value of RBENV_VERSION it shows me proper 2.6.5 but everyr command is executed under 3.0.0
Maybe I am explaining poorly. Every command is runned from ruby code, using system() call. On bash everything works fine! The problem is I think in the forking of bash or loading the environment during execution of command from ruby

Cannot find rvm.sh in /etc/profile.d/

I am new to RVM and looking to use it as I deploy my Rails app to a Digital Ocean server. I am following this tutorial and trying to get my Mina script to run (similar to Capistrano). The script includes a set up section with these lines:
source /etc/profile.d/rvm.sh
rvm use || exit 1
Unfortunately, rvm.sh does not exist in /etc/profile.d (or anywhere else on my server). rvm seems to be installed just fine (I can set list rubies, set my Ruby default version, etc). I'm not even sure what rvm.sh would contain if it existed.
Thanks in advance for any help or suggestions you can provide.
#mpapis got me on the right path here. I had installed rvm under one user, but not for this user. I wound up uninstalling rvm and starting from scratch, installing rvm for multiple users.

RVM $PATH variables to homeless user

I have a staging server running Ubuntu 12.04 and I'm creating some post-receive scripts on git to automate deployment of some repositories we keep on that server.
I created a homeless user 'git' (for security reasons) to deal git's ssh connections.
I want to run compass compile (compass is installed as a ruby gem) when somebody pushes something on the repo.
So I used RVM to install and manage ruby on that server.
And I did all rvm installation process using the multi-user approach.
When I run ruby -v with the 'git' user, I get the right output, I mean, rvm multi-user instalation is almost ok.
The problem is when I try to run an executable gem.
I noticed my normal user's .bash_profile had this (added by rvm):
[[ -s "/usr/local/rvm/scripts/rvm" ]] && source "/usr/local/rvm/scripts/rvm" # Load RVM into a shell session *as a function*
This code add gems' bin directory to the $PATH.
[normaluser]$ echo $PATH
/usr/local/rvm/gems/ruby-1.9.2-p320/bin:/usr/local/rvm/gems/ruby-1.9.2-p320#global/bin:/usr/local/rvm/rubies/ruby-1.9.2-p320/bin:/usr/local/rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/rvm/gems/ruby-1.9.2-p320/bin/:/usr/local/rvm/bin
As 'git' user is homeless it doesn't have a .bash_profile so I added that code to a script on /etc/profile.d/
But it didn't worked because every time I try to execute /usr/local/rvm/scripts/rvm as git, it exit's the git's bash session and return to my normal user session. It goes something like:
[normaluser]$ sudo -u git bash
[git]$ exec /usr/local/rvm/scripts/rvm
[normaluser]$
No erros are printed...
Why is rvm script exiting from bash session?
How can I set rvm $PATH on a homeless user?
three issues:
you should not use /bin/sh it is not supported by RVM, use /bin/bash instead (but you got it already
as explained by #Casper => exec replaces current shell with the command you run, there is no return
there are docs describing how to use RVM in init.d => https://rvm.io/integration/init-d
you should not try to source rvm for the user in shell initialization files as they are not used by init.d scripts, use wrappers instead, it's the most proven way to make it work.

How to load the RVM part of user's .bashrc to run Ruby CGI scripts under Apache?

I've configured a new server on Ubuntu 12.04 and I started to use RVM.
I've installed RVM under my user (as myself, not as root with sudo) by following the Ryan Bigg's guide, with no previous system-wide installed Ruby. So, I didn't have any Ruby under /usr/bin. My first task then was to replace the shebang line of all my CGI scripts, from
!#/usr/bin/ruby
to
!#/usr/bin/env ruby
However my scripts didn't run under Apache. In the terminal I could run them (by typing ./index.cgi, for example), but not over a browser. A relevant note: in both the user is the same, i.e., the Apache user is the same as the one logged on terminal. Through php tests, I've checked the RVM enviroment (last lines of .bashrc) was not loaded under Apache.
I saw this tip for running CGI scripts with RVM, which suggests to put the complete path of specific version of Ruby in the shebang line. That can be useful if you have scripts which run on different versions of Ruby. But that solution doesn't work for me, because my scripts must run on different machines, with different users and different paths.
The solution which works for me is to put a symlink of the desired Ruby version under /usr/bin:
sudo ln -s /home/apache_user/.rvm/rubies/ruby-1.8.7-p370/bin/ruby /usr/bin/ruby
But I want to know if there's a better solution, because I guess that rvm --default use is better than sudo ln -s.
I am thinking about:
loading the RVM Environment on startup (but I don't know how to achieve that);
loading the RVM Environment for each web request (which can degrade performance, and I don't know how to configure Apache to do that);
maybe the RVM Environment is loaded and all I must do is to guess the name of relevant variables to pass with PassEnv directive. But I doubt that. (Why Apache would run the .bashrc instead of another shell like csh or ksh?)
you can source the ruby environment, I'm not sure if it's enough to source it in $HOME for apache or if you need to modify /etc/init.d/apache2, but the line is:
source /path/to/rvm/environments/<name>
where for <name> you can either use full ruby name or an alias name
You can create aliases with:
rvm alias create veve 1.9.3-p125#my-project
which for RVM installed in /home/app/.rvm will allow you to use:
source /home/app/.rvm/environments/veve
in .bashrc or /etc/init.d/apache2 (just near top, bellow shebang).
you could always do
sudo ln -s /usr/local/rvm/rubies/default/bin/ruby /usr/bin/ruby
That will link the default version of the rvm-managed Ruby to /usr/bin/ruby and you will never have to do anything. set it and forget it.

Can't run Jenkins Build - bundle: "command not found"

I am currently trying to run a jenkins build for some of my cucumber tasks. All of my gems have been installed by using the Bundler. The gems are stored in the vendor folder.
However, when I try and run bundle install --deployment in the execute shell build step, I get the following error:
Started by user anonymous
Building in workspace /Users/Shared/Jenkins/Home/jobs/cukes/workspace
[workspace] $ /bin/sh -xe/var/folders/zz/zyxvpxvq6csfxvn_n0000004000001/T/hudson4461284045505361460.sh
+ bundle install --deployment
/var/folders/zz/zyxvpxvq6csfxvn_n0000004000001/T/hudson4461284045505361460.sh: line 2: bundle: command not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE
This is driving me crazy! It runs on my local machine with no problems. Why can't Jenkins see my gems?
Any help would be great!
Cheers,
Jon
In my case, I added this line to the first of script:
#!/bin/bash -l
Depending on the way you installed Jenkins, it might be running as a separate user, typically as user jenkins. If you have installed bundle into a nonstandard directory which isn't in the default PATH, like /usr/local/bin, you need to:
Add /usr/local/bin to the PATH of the jenkins user (check ~jenkins/.bashrc) or
Configure PATH environment variable in Jenkins global configuration (or slave configuration if building on a slave) or
Modify the build script to refer to bundle using a full path name.
If bundle is installed in /usr/local/bin/bundle (determine this with which bundle) you could just add a symbolic link to bundle in /usr/bin like so:
ln -s /usr/local/bin/bundle /usr/bin/bundle
First things to verify:
Make sure bundle is installed on the machine where Jenkins runs.
If it installed, make sure it's on the path for the user under which Jenkins runs
(To verify path/environment: insert shell build step that runs env)
For me I had originally installed it via the war file as that's the way recommended on the Getting Started Guide. However, the user handbook makes it much more clear that there are probably better ways to install Jenkins.
I ended up uninstalling the WAR file of Jenkins on macOS by:
Deleting the .war file.
Deleting the ~/.jenkins directory.
I then reinstalled via home-brew, and bundle started working.
You could see where is bundle is installed by running which bundle and run the bundle command from there

Resources