How to pin revision number of Jenkins build? - maven

I have a Jenkins multiphase job that
gets an update from version control (Subversion)
does a Maven build. Couldn't be more cut and dry.
The 1. above, svn update, does return a latest revision number. I would like to fetch that into 2. so that the build and its associated artifacts do have that number pinned to them, if any way possible in the artifact name itself but, if not, in the build history. Is there a way to do it and how (e.g. using the subversion or another plugin)?
I am using the buildnumber plugin to fetch the build number and generate a small text file that is contained inside my WAR artifacts, which makes it subsequently available via HTTP. But to see it, one must either first deploy the artifact or at least extract its contents. I would like it more readily visible in Jenkins.

You should take a look at How to get SVN revision number in Jenkins Workflow Plugin? - He solved the problem with:
def revision = 'svn info'.execute().in.text.split('\n').find { it.startsWith('Revision') }.split(':')[1].trim()
He's obviously using the latest Jenkins version, but if you're using one of the old Jenkins versions you can simply run the following command and parse the result to get the revision number you want:
svn info -r HEAD
BTW you can use a Jenkins constant too. In the browser you can open http://your-jenkins-host/env-vars.html/ and you will find a constant named SVN_REVISION. Each job build keep the SVN revision into that variable.

Related

Extracting Revision numbers in svn

I am trying to create a pipeline on jenkins which takes the code from a svn repository , builds it (using MSBuild) , tests it , creates a package from the build artifacts (using Maven) and then commits this package to another svn repository which belongs to another company.
This commit must have a specific commit message format which includes :
1- a short text
2- the url of our repository
3- the revision of some specific dlls what we used to build the solution
So as an example :
#Automatic Package Update
------
src_svn_url = "the source code URL"
src_svn_rev = 33568 , dll1 = 4654 , dll2 = 7657 , .....
------
I did some research and found out that it can be done through the pre-commit hooks. Are we able to make this pre-commit hook extract the revision number of different dlls inside my repo and write them automatically in the commit message every time we are committing? The goal for us is to know using which revision of the dlls , we produced the mentioned package.
This is not a task of any commit hook script.
You can run the svnversion command in your pipeline to get the revision number and use it to create a commit log message for your new commit into another repository.
For example, you can create the log message in a dedicated file log-message.txt with all the necessary information and then use the svn commit -F log-message.txt command to use the file's contents as the log message.

How I can access to a file saved in Gitlab CI/CD artifact in the next stages and increase it's version

I have a spring boot application and I deployed it in the first stage and now I have a jar file.
my question is how to access this jar file in the next stage and how I can run it.
second question is HOW can I increase its version number? for example jar file name is spring.0.0.1.jar and I want to increase version number after every push. is this possible?
First Question:
You can save the jar file as an artifact and access it in the second stages jobs' script area. For example
FirstJob:
stage: FirstStage
scrip:
- <your commands here>
artifacs:
paths:
- ./artifacts/myOutput.jar
Now your "myOutput.jar" is accessible in the artifacts folder for all following jobs. See here: https://docs.gitlab.com/ee/ci/pipelines/job_artifacts.html
Second Question:
As far as I know there is no way of handing down variables between pipelines in GitLab CI so this would not be possible if I'm right. Since artifacts don't get added to the repository, previous artifacts are not available to following pipelines either. Yet, if I had to come up with a solution on the spot, I'd try:
Saving the version number somewhere accessible to every pipeline (cloud, repo)
git push every artifact to actually add it to the repository, then check file name and increment version number
using the GitLab CI release option. The CI can create a release object for you, maybe this could help as well. See here: https://docs.gitlab.com/ee/ci/yaml/README.html#release

How to access values from changeSets of Jenkins build in ruby script

I'm writing a JenkinsFile in which I have to load a ruby script which adds labels and comments to Jira tickets which are fixed in the current build. To do this I need commit-msg from git which contains Jira ticket number. ChangeSets of Jenkins contains commit-msg as {entry.msg}. How can I pass this entry.msg to my ruby script?
don't rely on Jenkins Changeset, use a good old sh command. Changeset would behave weird when used with shared libraries

Create job in jenkins with calling svn and maven

For now I have a batch file with commands for update projects using svn and calling maven 'clean install'. How to create some job in Jenkins for similar actions?
Should I write it to ant file (sorry if it's stupid idea, I've just heard about it but I don't know what is it exactly and what can I do with this) or there is other way?
Thanks
Like arghtype suggested, you need to be using Jenkin's own Source Code Management by configuring SVN as SCM source and supplying credentials as part of Maven build job.
If you have to use your own local working copy, you are organizing it wrong, you will lose on all the benefits of having Jenkins manage SVN changes, and in the end, this organization will give you more unsolvable problems in the future. Think about the advice people are giving here and come with up a reason why you need to have a local workspace outside of Jenkins management on a Jenkins build machine. My only guess is: your Jenkins and Development machine are the same. That again is not how it should be organized. Jenkins is a CI-server, not a personal build "automator".
Regardless, if you still want to do what you say.
What you think you want
Create a new Freestyle job
Under Build Steps, click Add build step
Select Execute Windows batch command
Write your batch execute command in there. Your working directory will be Jenkins's $WORKSPACE, so change your path accordingly to where you want to run it.
But with the above configuration, you might have as well put the batch file under windows scheduler... You are not really using Jenkins with the above.
What you should do instead
Create a new maven2/3 build job
Under Source Code Management, select Subversion
Under Repository URL enter the remote SVN repo (i.e. http://your.svnsever.com/path/to/project)
Under Build, enter your Root POM location (this will be relative to the location of your SVN checkout, so if your POM is under http://your.svnserver.com/path/to/project/maven/pom.xml, then enter maven/pom.xml.
Under Goals and options, enter clean install
Click Save
The Source Code Management section will take care of setting up a local workspace and checkout the repository into that workspace. By default, every time a new build is triggered, it will run svn update on that workspace for you.
The Maven Build step will take care of running your Maven, however note that it is configured to use default ~/.m2/repository location. If your local maven repo needs to be different, change this under Jenkins Global Configuration
Create a new job.
In Source Management choose Subversion, specify your repo and credentials.
Add a new build step - maven build, specify your maven goals ('clean install').
Jenkins is a CI(contiounus integration) server. It can be used to generate scheduled builds of ant or maven based projects. It can also start building projects by some triggering event such as a commit to SCM (git, svn, mercurial,...)connected to it. You really have to read its documentation to get a better understanding. It has nice tutorials.

Is there a way to set the Maven version number dynamically?

I would like to use Maven to produce an artifact in zip format. To give you some background; my project includes an APS package (Application Packaging Standard, used to provision cloud applications on the Parallels platform). This package is a zip file that contains a combination of XML as well as PHP files. It is generated by an APS plugin from within Eclipse and its name always includes the version and release number of its contents.
What I am trying to do is generate a zip file with Maven that would be kind of a release candidate that will be eventually sent to customers and would include not only the actual APS package but also other files such as README, User Guide.pdf, etc;. I would like the name of this zip file to contain the version number of the version number of the APS package. Currently I can generate this manually by using something like "mvn -Dversion=1.2.3-4 package" but I would like to automate the process and ideally run this from Jenkins.
Basically, my strategy is to run a script that would extract the version number from the initial APS package, once that is done, my script can invoke Maven and can pass this parameter to it so it can generate the final zip with the proper version number. This is fine but again, I need to run this script manually and I am looking for an automated process.
My question is; is it possible to invoke this script from within Maven and use its return as a parameter to set the version name (or the name of the file that will be generated) at run time? As I mentioned, I would like eventually Jenkins to handle this. It can pick up the pom file but I am not sure how it could kind of "auto configure" itself to have the proper version number.
Thanks is advance.
From jenkins build you can use profile with ${BUILD_NUMBER}:
<profile>
<id>jenkins</id>
<build>
<finalName>${artifactId}-${version}-${BUILD_NUMBER}</finalName>
</build>
</profile>
Then run in jenkins:
clean install -Pjenkins
I use the SVN (or any source versioning system) version to identify the software builds.
By simply executing this
REVISION=`svn info | grep '^Revision:' | sed -e 's/^Revision: //'`
on the sourcers folder you get the right value in $REVISION, then you can use it for your maven build
mvn -Dversion=1.2.3-$REVISION package
easy and clean

Resources