Manipulating Jenkins build parameter with groovy script, used for sonar analysis - groovy

At the moment, i have Sourcetree making my branches, they will be called "feature/my-branch" and i can send that to my hg repo, and retrieve it from jenkins and build.
but, sonar does not accept "/" in the branch name, so i wanted to change "/" to "-" or something.. in a groovy script in the build phase.
Is something like that possible?
my solution so far:
parameter: BRANCH
default value: feature/my-branch
groovy scrips: def replaced = BRANCH.replaceAll(/\//, '-')
but it will not recognize my BRANCH, i've tried adding it in all sorts of ways..
any help to make my "feature/my-branch" be analyzed by sonar will be appreciated.

How are you invoking SonarQube on Jenkins? I assume it's via the SonarQube Jenkins Plugin. You could use the EnvInject Plugin to edit BRANCH. Here's an untested possibility for you to mull over:
SONAR_BRANCH=${BRANCH//\//-}
And in the SonarQube plugin's "Additional options":
sonar.branch=${env.SONAR_BRANCH}
If that won't work, or if you don't want EnvInject, you could do everything in one Execute Shell build step:
SONAR_BRANCH=${BRANCH//\//-}
mvn sonar:sonar -Dsonar.branch=${SONAR_BRANCH}

Related

How to publish to another git after build using jenkins

I have a private repo and want to publish the build artifacts to another public repo (it's the packaged application)
How can I do that in jenkins? I could only find publish on the git I've used to build.
Thanks
There are multiple ways to do this:
- Using a shell script that calls the GIT command line tool. This can be a post-build script, the same script compiling the code, etc.
- Same thing in groovy
- Call a downstream job to do that for you (probably the best solution IMHO)
The main problem would be the GIT credentials, but that is not an big issue...

How to make a gitlab changelog entry?

I really, really, don't understand gitlab docs...
I am trying to understand how to make a god damn changelog entry, I have a changelog.md file, here are the gitlab docs about this: https://docs.gitlab.com/ce/development/changelog.html
But I don't understand where I have to write that, for example
$ bin/changelog 'Hey DZ, I added a feature to GitLab!'
It must be a terminal but where? And what's that '$'...
It must be a terminal but where?
Where you have cloned the GitLab repo.
In that repo, you will find a bin/changelog script, which starts with #!/usr/bin/env ruby, meaning it will execute itself with ruby.
And $ is just a symbol for a shell prompt.
The documentation you are reading is for the GitLab development, not for your own project or repository.
Like said in this very similar question :
The bin/changelog script is not a GitLab feature, and does not describe how to generate changelogs for repositories hosted on GitLab
]1

Jenkins tfs plug-in and checkout source on remote node

First, I'm a Jenkins neophyte. I have made a free-style software project in Jenkins to perform my Linux build. The Jenkins server is running on Windows so there are slave nodes configured for doing this Linux build. The sources are kept in a TFS server.
I updated our TFS plugin to the latest of 4.0.0. This plugin says that it is no longer necessary for slave nodes to have the Team Explorer Everywhere package installed as it uses the Java API. However, when I kick off my build, I get this:
Started by user Andy Falanga (afalanga)
[EnvInject] - Loading node environment variables.
Building remotely on dmdevlnx64-01 (PY27-64 CENTOS6-64 LOG4CPLUS PY26-64) in workspace /home/builder/jenkins/workspace/Linux Autotools Build
Deleting project workspace... done
Querying for remote changeset at '$/Sources/Branches/Andy/AutotoolsMigration' as of 'D2015-10-05T18:26:27Z'...
Query result is: Changeset #4872 by 'WINNTDOM\afalanga' on '2015-09-25T23:36:24Z'.
Listing workspaces from http://ets-tfs:8080/tfs/SoftwareCollection...
... Long list of workspaces
Workspace Created by Team Build
Getting version 'C4872' to '/home/builder/jenkins/workspace/Linux Autotools Build'...
Finished getting version 'C4872'.
[Linux Autotools Build] $ /bin/bash /tmp/hudson7081873611439714406.sh
Bootstrapping autotools
/tmp/hudson7081873611439714406.sh: line 4: ./bootstrap: No such file or directory
Build step 'Execute shell' marked build as failure
Notifying upstream projects of job completion
Finished: FAILURE
I log into that system and look in the directory /home/builder/jenkins/workspace/Linux Autotools Build and sure enough, there's nothing there. My configuration is pretty simple.
I have discard old builds checked and a simple rotation (this is just me learning how to use it).
I have it set to "Restrict where the build is done" and a label which associates to the 3 slave nodes for doing this build.
All TFS credentials are input and correct.
No build triggers
A simple shell script for Build->Execute Shell which bootstraps the autotools and calls configure and then make.
What am I doing incorrectly?
I found the answer and am posting it here in case someone runs into this. This seems better than simply deleting the question. The TFS plugin doesn't seem to like spaces in the project name. The name before Linux Autotools Build which didn't work and the name now, LinuxAutotoolsBuild which does.
The errors provided by the Jenkins system didn't provide enough information for this to be apparent. After trying a few other things the thought occurred, "Perhaps the spaces are causing grief."
Hope this helps someone.

Run groovy scripts for jenkins in IDE

I wanted to run groovy scripts in my IDE instead of Jenkins Script console. is this possible? If so where i can download http://javadoc.jenkins-ci.org/ API so that i can put that in classpath.
I cannot refer hudson.model.* and other classes with jenkins CLI jar
There is a webpage that explains how to configure Eclipse on how to Write Groovy scripts for Jenkins with code completion
Short version:
create a new maven project,
add jenkins dependencies,
update settings.xml to include jenkins repo,
add Groovy support to Eclipse(optional),
convert project to groovy and you're good to go.
BTW, for Eclipse Mars, the Groovy plugin is here: http://dist.springsource.org/snapshot/GRECLIPSE/e4.5/

Jenkins to SCP a few folders and files from SVN to linux server?

I use jenkins to do auto deployment weekly to a tomcat server, and it is fairly simple to do using the "curl" with the tomcat manager. and since i am only uploading a .war file, so its very straight forward.
But when comes to a backend console application, Anyone has any idea how to use jenkins to upload an entire "set of folders with files" onto a linux box? The project that i have is built via ant and has all the folder inside the SVN.
A couple things come to mind.
Probably the most straightforward thing to do is use the ant scp task to push the directory / directories up to the server. You'll need the jsch jar on your Ant classpath to make it work, but that's not too bad to deal with. See the Ant docs for the scp task here. If you want to keep your main build script clean, just make another build script that Jenkins can run named 'deploy.xml' or similar. This has the added benefit that you can use it from places other than Jenkins.
Another idea is to check them out directly on the server from SVN. Again, ant can probably help you with this if you use the sshexec task, and run the subversion task inside of that. SSHexec docs here
Finally, Jenkins has a "Publish Over SSH" plugin you might try out. I've not used it personally, but it looks promising! Right over here!

Resources