Commit file from Jenkins workspace to SVN
Asked Answered
G

3

11

I have a saved project in Subversion repository and compiles it with Jenkins. When I run the build, Jenkins pulls project into workspace directory. I need commit one changed file from Jenkins workspace into Subversion. How can I do it??

Thanks for answers...

Gravel answered 23/1, 2015 at 7:36 Comment(1)
As part of your job, you can run a (bash) script which could run svn commit <yourfile>.Muncey
B
5

Can you give a few more details? Exactly what is this file, and why does it need to be committed as part of your Jenkins build? What are you building (Java? C++? .NET?) and how are you building it?

Normally, you shouldn't put anything under version control except source files. That is, if you can build it, you shouldn't put it into version control. A lot of people like to commit their built code into their version control system, but this is usually a big mistake. binary files are much bigger and rarely diff. This results in a source repository that's 90% compiled code -- almost all of it obsolete. This is especially problematic with Subversion which doesn't have a way to (easily) remove obsolete code.

Jenkins has a feature that allows you to archive your built files for easy access. We use this all the time. We build our code, and the program is available in Jenkins for downloads. Even better, Jenkins will delete older builds (you can save the last X builds or save only builds younger than X days). And if you have a release candidate build, you can lock that build to prevent it from being deleted.


Still, if you insist on doing this, there are several things you can do and things you have to watch out for:

  1. When you have Jenkins setup to automatically build each change, and you commit a change in your version control system, Jenkins will see that, and start a new build. Then, Jenkins saves the change, sees the change, and does another build. Make sure you exclude the files or directories from Jenkins' consideration when it should do a build. You can specify this when you specify the URL of the checkout.

  2. Unlike most version control systems, Subversion was made to be client independent. There's an actual API for clients. The standard Subversion command line client isn't needed in Jenkins, so make sure it's installed. Make sure you install a client compatible with the Jenkins built in SVNKit client. This is especially true since Subversion 1.6, 1.7, and 1.8 all take a different client format.

  3. You can add multiple build steps to your build in Jenkins. Just add a new shell script or batch script step, and add in a svn commit -m "comment of some sort" step. It's pretty simple to do once you've taken care of the first two points. However, please think carefully why you're doing this.

As I said before, 99.9999% of the time, you shouldn't be using Jenkins to commit changes it built. I'm sure that 0.0001% reason exists somewhere, but I've never seen it. If you want to make built files universally accessible to your other projects, use Jenkin's ability to archive built files.

If a product Jenkins is building is needed for another build, you can use the Copy Artifact Plugin to copy a build artifact to another job, and then fire off that job. Even better, use a release repository system. In Java, you can use a Maven release repository like Nexus or Artifactory. To use and deploy artifacts to that repository, you can use Ivy or Maven or Gradle. If you are building .NET, look at Nuget.

Beamon answered 23/1, 2015 at 14:40 Comment(4)
one reason I've seen to commit in a build is to increment the release version as part of an automated release. Certainly, there are other ways to do this and other tools, but it's more common than 1 in 10^6 :)Denverdeny
There's no reason to check in anything in that case. You have a file that contains a template string that gets replaced with some version number. In Subversion, you can use the Subversion revision number. Even better, we use the Jenkins job name and the build number, so we can go back to the Jenkins build which shows us things like testing data, and helps us trace the history. Otherwise, all you're doing is creating thousands of Subversion revisions that contain absolutely no useful history information. You do a svn log, and 50% of the changes listed are due to the build.Beamon
I'm certainly not suggesting committing with each and every CI build. Clearly, we're using different terminology, release build != build. In the case I describe, there is a public-facing "marketing" version number that increments with each release build.Denverdeny
I'm sure that question is valid for some use cases and all this teaching around is not necessary. Or might be for somebody, I'd put it as a side-note.Specimen
H
2

Please have a look to this answer: Jenkins svn commit post-build

It should be possible to commit with a new build step (or a post build step).

But be careful with the SVN layout of the Jenkins workspace. The subversion plugin is using the SVNKit to deal with the SVN commands. Your workspace maybe use the SVN layout 1.6.

If the SVN client is more recent on your machine, your SVN commit will fail with the following error: org.apache.subversion.javahl.ClientException: The working copy needs to be upgraded svn: Working copy 'C:.... is too old (format 10, created by Subversion 1.6)

Horwath answered 23/1, 2015 at 12:39 Comment(0)
J
1

You could carefully use:

svn commit -username someuser -password %injected_password% 

to inject password in Build Environment, this command injects passwords to the build as environment variables

Watch out: that password might be visible in the job trigger batch.

Jubilation answered 9/1, 2018 at 14:51 Comment(1)
Actually it should be svn commit --username someuser --password %injected_password% (= double dashes for options)Kasher

© 2022 - 2024 — McMap. All rights reserved.