简体   繁体   中英

Jenkins config versioning using Git

I have recently setup a Jenkins server, and after getting many projects configured I realized that being able to have versioning of all the config settings would be valuable.

Most all backup solutions and plugins I have come across involve scripts that basically just copy files to a backup folder, or a plugin that essentially does the same. However, I'm not just looking to make backups, I'd also like to have versioning available.

So after thinking about it I went ahead and set up a Git repository which lives at the root Jenkins install folder.

Here is the .gitignore file I am currently using:

# ignore everything to start
*

# except these
!**/
!*.xml
!.gitignore

# then ignore these
builds/
plugins/
war/
lastStable/
lastSuccessful/

This seems to work pretty well in capturing all changes to any XML file in the Jenkins installation folder. My only complaint is that it is a bit of a manual process, requiring a commit/push of the repo after any versionable set of changes.

I'm wondering if there is a better way to accomplish this, or if there are any potential issues to doing it this way?

You should check out the Job DSL Plugin . It allows to maintain the job and view configuration in script files like source code. And the script can be kept in source code management very well. The workflow is changing a Job DSL script, to a SCM check in, then run a job in Jenkins to checkout the scripts and adapt the config.

That keeps the parts of the config which change often in SCM. Other parts of the Jenkins config like the global settings can be kept in a daily backup and do not necessarily be kept in source code management. Those parts of the config do not change very often.

I ended up creating a Jenkins project that basically scripts a git add/commit/push on any changes to the Jenkins configs. It uses a defaulted parameter for the commit message, if empty then the script just runs a git status and terminates. I have also scheduled it to run every night to pick up any changes which may have not been pushed using this job with a custom commit message.

Jenkins is currently on Windows so here's the batch commands I'm using. Since Jenkins was originally setup to run under the SYSTEM account I had to set a couple environment vars to get it working as is.

@ECHO OFF

set HOMEDRIVE=C:
set HOMEPATH=Users/Jenkins
cd "C:\Program Files (x86)\Jenkins"

@ECHO ON

git status

@ECHO OFF

if "%commit_message%" == "" (
  EXIT 0
)

@ECHO ON

git add -A
git commit -m "%commit_message%"

@ECHO OFF

IF %ERRORLEVEL% NEQ 0 (
  EXIT 0
)

@ECHO ON

git push
git status
git log -n 3

Storing Git credentials is really the only thing not handled well. For now I just added the user:pass in the Git repo URL, but obviously that is not ideal. I tried using some of the credential saving methods for Windows in the past and they did not work. This still needs sorting out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM