Getting RVM to work with Jenkins Pipeline - ruby

So I'm trying to setup a Jenkins declarative Pipeline to run an Xcode build job. I want to use xcpretty Ruby gem but will also need several other Ruby gems later for other jobs.
stage('Pre-Build')
{
steps
{
echo "Executing Pre-Build steps ..."
sh(returnStdout: true, script: "#!/bin/bash -xle && source ~/.rvm/scripts/rvm && rvm use 2.3.1 && cd ${WORKSPACE}/${env.PROJECT_PATH} && gem install xcpretty && set -o pipefail && xcpretty")
}
}
First off all, I get no echo for the sh in Pre-Build stage whatsoever. Neither returnStdout: true nor the hashbang seem to have any effect on getting any log output from the shell invocation.
That leaves me blind on what's going on here. When running the job, the Pre-Build stage passes and then it fails at my actual build stage when I want to use xcpretty.
Here's the log output from the Pre-Build stage:
Executing Pre-Build steps ...
[Pipeline] script
[Pipeline] {
[Pipeline] sh
[job] Running shell script
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] echo
If I run it in the bash manually, no problem! On Jenkins it seems that something isn't working with RVM but I'm tapping in the dark while trying to fix this for days and it drives me insane.
Any help is appreciated!

MichaƂ Knapik has a blog post covering one hand-rolled solution which defines a groovy wrapper which replicates rvm use semantics
See:
https://blog.knapik.me/how-to-use-rvm-with-jenkins-pipeline/
node {
withRvm('ruby-2.3.1') {
sh 'ruby --version'
sh 'gem install rake'
}
}
I don't want to copy any more than this example - its his code.
Note the caveats on specifying the version

Related

pre-commit hook does not echo on terminal

I am trying to run a script with pre-commit hook. Here is my script:
build_script.sh
#! /bin/bash
echo "Stage 1: Preparing CMake configuration"
.pre-commit-config.yaml
fail_fast: false
- repo: local
hooks:
- id: Generate build
name: Generate build
entry: sh build_script.sh
language: system
always_run: true
pass_filenames: false
I can see when I run command git commit -m "message", the hook calls the script and Generate build will Pass. However, I do not see the echo on the terminal. I would like to see the message, "Stage 1: Preparing CMake configuration". What is wrong with this setup?
pre-commit takes a bit from unix philosophy here -- it is silent unless there is a problem (by default).
if your script fails then the output will be displayed
you can also use --verbose to show the full output (for debugging) and optionally set verbose: true on your hook itself. note: these are both debugging options and are not intended for use outside that -- we've found that when hooks are noisy contributors tend to ignore all of the output
disclaimer: I wrote pre-commit

How to run a go binary as last stage of Jenkins pipeline locally?

I have a simple gin gonic microservice Golang project that I'm using to learn how to make a Jenkins pipeline. Every stage is successfully run, but the binary is not running after the pipelines finished. Can also tell the process is not running by using curl to hit the endpoint:
curl http://localhost:9191/users
This is the pipeline in question:
pipeline {
agent any
stages {
stage('git') {
steps {
echo "git"
git 'https://github.com/eduFDiaz/golang-microservices.git'
}
}
stage('clean') {
steps {
echo "clean"
sh "make clean"
}
}
stage('test') {
steps {
echo "test"
sh "make test"
}
}
stage('build') {
steps {
echo "build"
sh "make build"
}
}
stage('run') {
steps {
echo "run"
sh "make run"
}
}
}
}
The Makefile:
executableName=testApi
clean:
echo "stoping if running and cleaning"
rm -rf ./bin
killall $(executableName) || true
test:
echo "Testing..."
go test -coverprofile cp.out ./mvc/...
go tool cover -html=cp.out
build:
echo "Building..."
go build -o bin/$(executableName) mvc/main.go
run:
echo "Running..."
./bin/$(executableName) &
all: test build run
Everything runs perfectly when I do it by hand. What am I missing here?
Console Output:
Started by user Eduardo fernandez
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/golang pipeline test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (git)
[Pipeline] echo
git
[Pipeline] git
No credentials specified
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://github.com/eduFDiaz/golang-microservices.git # timeout=10
Fetching upstream changes from https://github.com/eduFDiaz/golang-microservices.git
> git --version # timeout=10
> git fetch --tags --force --progress -- https://github.com/eduFDiaz/golang-microservices.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision bfa434ff2aca9ea748182aa2b29094e1b9f442c6 (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f bfa434ff2aca9ea748182aa2b29094e1b9f442c6 # timeout=10
> git branch -a -v --no-abbrev # timeout=10
> git branch -D master # timeout=10
> git checkout -b master bfa434ff2aca9ea748182aa2b29094e1b9f442c6 # timeout=10
Commit message: "run reverted to previous state in Makefile"
> git rev-list --no-walk bfa434ff2aca9ea748182aa2b29094e1b9f442c6 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (clean)
[Pipeline] echo
clean
[Pipeline] sh
+ make clean
echo "stoping if running and cleaning"
stoping if running and cleaning
rm -rf ./bin
killall testApi || true
testApi: no process found
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] echo
test
[Pipeline] sh
+ make test
echo "Testing..."
Testing...
go test -coverprofile cp.out ./mvc/...
? github.com/golang-microservices/mvc [no test files]
? github.com/golang-microservices/mvc/app [no test files]
? github.com/golang-microservices/mvc/controllers [no test files]
ok github.com/golang-microservices/mvc/domain 0.004s coverage: 0.0% of statements
ok github.com/golang-microservices/mvc/services 0.003s coverage: 0.0% of statements [no tests to run]
? github.com/golang-microservices/mvc/utils [no test files]
go tool cover -html=cp.out
HTML output written to /tmp/cover914928629/coverage.html
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (build)
[Pipeline] echo
build
[Pipeline] sh
+ make build
echo "Building..."
Building...
go build -o bin/testApi mvc/main.go
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (run)
[Pipeline] echo
run
[Pipeline] sh (hide)
+ make run
echo "Running..."
Running...
./bin/testApi &
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
This problem is occurring because Jenkins is cleaning all the child process started during build.. i.e. make run is bringing up the application but Jenkins is killing the process as part of cleaning (For More details search for "ProcessTreeKiller").
To Resolve update your line as below
stage('run') {
steps {
echo "run"
sh "export JENKINS_NODE_COOKIE=dontKillMe; make run "
}

shell script returning not found on jenkins master using pipeline as code

I am new to jenkins and trying to write a pipeline. Everything is working when run with jobs, but facing issue with pipeline. My script which should run after checking out from github returns file not found. Could anyone help please. Attached is the image of the log.
https://i.stack.imgur.com/LuxGn.png
Below is the code sample I am trying to execute.
stage('puppet master config checkout') {
steps {
echo "cloning github"
git "https://github.com/rk280392/pipeline_scripts.git"
}
}
stage('puppet master config build') {
steps {
echo "running puppet master script"
sh "puppet_master.sh"
}
}
check the file script is here with command sh 'ls' just after the git step.
generally I would recommend not to use git step but checkout instead, it is more powerful and more reliable
checkout([
$class: 'GitSCM',
branches: scm.branches,
extensions: scm.extensions,
userRemoteConfigs: [[
url: 'https://github.com/rk280392/pipeline_scripts.git'
]]
])
is your script executable? you could use chmod +x puppet_master.sh before running it with dot slash as prefix ./puppet_master.sh
sh 'sh puppet_master.sh'

Not getting the output from shell script in Gitlab CI

I have set up a gitlab runner on my windows. I have a build.sh script in which I am just echoing "Hello world". I have provided these lines in my gitlab-ci.yml file:
build:
stage: build
script:
- ./build.sh
The runner executes this job but does not print the echo command which I have mentioned in the build.sh file. But if I changed the extension to .bat it works and shows me the output. The gitlab-runner is set up for shell. What can be the possible reason? or I am missing something?
GitLab will output for anything that ends up written to STDOUT or STDERR. It's hard to say what's happening without seeing your whole script, but I imagine somehow you're not actually echoing to STDOUT. This is why the output isn't ending up in the CI output.
To test this I created a test project on GitLab.com. One difference in my test is my CI YAML script command was sh build.sh. This is because the script wasn't executable so it couldn't be executed with ./build.sh.
builds.sh file:
#!/bin/bash
echo "This is output from build.sh"
.gitlab-ci.yml file:
build:
stage: build
script:
- sh build.sh
The build output:
Running with gitlab-runner 12.3.0-rc1 (afb9fab4)
on docker-auto-scale 72989761
...
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/dblessing/ci-output-test/.git/
Created fresh repository.
Checking out cfe8a4ee as master...
Skipping Git submodules setup
$ sh build.sh
This is output from build.sh
Job succeeded

No echos in bash script in Jenkins

I'm writing a Pipeline script for Jenkins on Mac that needs to execute a couple of sh steps. Some of them involve RVM and Bundler ...
sh '#!/bin/bash -xl' +
' && rvm list' +
' && rvm use 2.3.1' +
' && gem install bundler' +
' && which bundler'
As you can see I have to use the hashbang to make RVM and Bundler to work, i.e having to be in a Login Shell but the problem is I don't see any log output for this in Jenkins anymore, even with -xl flag.
Does somebody know why log output is omitted and how to enable it for this?
UPDATE:
sh returnStdout: true, script: '#!/bin/bash -xl && rvm list && rvm use 2.3.1 && gem install bundler && which bundler'
Log Output:
[Pipeline] sh
[app_ios_test_automation] Running shell script
[Pipeline] echo
The "sh" function has optional parameters. If you call it like you're doing, you won't get the standard output of the script.
If you go into your pipeline job definition, where you specify the pipeline script itself, you should see a link labeled "Pipeline Syntax". This allows you to experiment with the pipeline steps that are enabled in your Jenkins instance. If you select "sh" from the dropdown and then click the "Advanced" button, you'll see the additional options you can set, including the "returnStdout" flag.
To expand on David Karr's answer, you're probably looking for something like this:
sh(returnStdout: true, script: "your script here")

Resources