简体   繁体   English

使用Git的Jenkins配置版本控制

[英]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. 我最近安装了一个Jenkins服务器,在配置了许多项目之后,我意识到能够对所有配置设置进行版本控制将很有价值。

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. 因此,在考虑之后,我继续建立了一个Git存储库,该存储库位于Jenkins的根安装文件夹中。

Here is the .gitignore file I am currently using: 这是我当前正在使用的.gitignore文件:

# 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. 在捕获对Jenkins安装文件夹中的任何XML文件的所有更改时,这似乎非常有效。 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 . 您应该签出Job DSL插件 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. 工作流程是将Job DSL脚本更改为SCM签入,然后在Jenkins中运行作业以签出脚本并修改配置。

That keeps the parts of the config which change often in SCM. 这样可以保留配置中经常在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. Jenkins配置的其他部分(例如全局设置)可以保留在每日备份中,而不必保留在源代码管理中。 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. 我最终创建了一个Jenkins项目,该项目基本上对Jenkins配置的任何更改编写了git add / commit / push脚本。 It uses a defaulted parameter for the commit message, if empty then the script just runs a git status and terminates. 它为提交消息使用默认参数,如果为空,则脚本仅运行git状态并终止。 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. Jenkins当前在Windows上,因此这是我正在使用的批处理命令。 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. 由于Jenkins最初设置为在SYSTEM帐户下运行,因此我必须设置几个环境变量以使其按原样工作。

@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. 实际上,存储Git凭证是唯一处理不好的事情。 For now I just added the user:pass in the Git repo URL, but obviously that is not ideal. 现在,我只是在Git存储库URL中添加了user:pass,但是显然这并不理想。 I tried using some of the credential saving methods for Windows in the past and they did not work. 过去,我曾尝试使用Windows的某些凭据保存方法,但它们不起作用。 This still needs sorting out. 这仍然需要理清。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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