Trigger Hudson构建于SVN提交之上

时间:2011-01-25 10:39:55

标签: svn build hudson

我已经设置了一个版本控制系统(Subversion),开发人员将使用它来提交和更新他们的代码(假设它的地址是https://server/svn/project)。只有经过身份验证的用户才能访问项目的SVN。

另一方面,我已经安装了Hudson作为项目的持续集成服务器(项目地址为server:8080/job/project)。

我想实现以下目标:

  • 当有SVN提交时,Hudson构建将自动触发。
  • 当他们的已提交代码未构建时,会将邮件发送给相应的开发人员(提交代码的开发人员),这意味着当用户A提交不构建的代码时,只有用户A才会收到包含通知的电子邮件。 / LI>

我为Hudson用户设置了基于矩阵的授权,因为我不想对任何人开放。

我已经看到了post-commit钩子的一些建议,但直到现在它们都没有工作。

有人可以建议对这两个问题做些什么吗?具体步骤将不胜感激。

4 个答案:

答案 0 :(得分:7)

根据"Building a software project # Builds by changes in Subversion/CVS",Hudson需要轮询您的SVN回购以检测更改并触发构建。

但是,这可以在SVN的每次提交时启动,例如in this thread 官方脚本位于Subversion Plugin页面。

REPOS="$1"
REV="$2"
UUID=`svnlook uuid $REPOS`
/usr/bin/wget \
  --header "Content-Type:text/plain;charset=UTF-8" \
  --post-data "`svnlook changed --revision $REV $REPOS`" \
  --output-document "-" \
  --timeout=2 \
  http://server/hudson/subversion/${UUID}/notifyCommit?rev=$REV

但是它被指定:

为此,您的Hudson必须允许对系统进行匿名读取访问
如果您对Hudson的访问控制更具限制性,您可能需要指定用户名和密码,具体取决于您的身份验证配置方式。

答案 1 :(得分:4)

要在SVN中提交时触发构建,您必须(1)将hudson作业设置为远程构建,(2)创建SVN钩子...

第一部分相当简单......使钩子转到/ var / lib / svn //钩子并将post-commit.tmpl重命名为post-commit 在那里你可以做类似

的事情
#!/bin/bash
# Este script comprueba si se han hecho cambios en un directorio concreto,
# y en tal caso lanza una build en Jenkins

REPOS="$1"
REV="$2"
JENKINS_JOB="$3"
JENKINS_USER=admin
JENKINS_PASSWORD=****
JENKINS_HOST=<hostname>

if [ -n $(svnlook dirs-changed $REPOS --revision $REV | fgrep "tags\/") ];then
 wget --quiet --auth-no-challenge --no-check-certificate --http-user=$JENKINS_USER --http-password=$JENKINS_PASSWORD http://$JENKINS_HOST/job/$JENKINS_JOB/build?token=TOKEN
fi

exit 0

看看这篇文章http://blogsyntagma.blogspot.com.ar/2012/04/hook-de-subversion-para-ejecutar-un-job.html(用西班牙语)

答案 2 :(得分:1)

以下是必需的步骤:

  • 创建一个Hudson可用于获取对存储库的只读访问权限的SVN用户
  • 配置Hudson在访问存储库时使用此SVN用户
  • 创建新作业以在指定地址(即特定分支)使用存储库
  • 将作业配置为每分钟至少轮询一次存储库以进行任何更改
  • 配置您的工作以构建所需的内容
  • 配置作业以发送有关构建失败的电子邮件

我建议通过电子邮件发送给所有开发人员,以便他们收到通知说构建不稳定,而不仅仅是罪魁祸首。这不仅提供了更多的可见性,而且还会激发罪魁祸首立即解决问题或以其他方式接受来自其他开发人员的责骂。相信我,这是有效的。

答案 3 :(得分:0)

这是我提交SVN存储库后让Jenkins 2.157开始构建的方式。

1。允许在詹金斯中进行读取访问

使用Jenkins的网络界面,转到Manage Jenkins-> Configure Global Security并选中Allow anonymous read access

allow anonymous read access screenshot

如果您跳过此步骤,则尝试使用HTTP请求触发构建时会收到以下响应(在第三步中进行了描述):

Authentication required
<!--
You are authenticated as: anonymous
Groups that you are in:

Permission you need to have (but didn't): hudson.model.Hudson.Read
... which is implied by: hudson.security.Permission.GenericRead
... which is implied by: hudson.model.Hudson.Administer
-->

2。配置构建触发器

仍然在Jenkins的Web界面中,转到构建作业并定义您要使用脚本来触发构建(下一步将是SVN commit钩子):

configure build trigger

3。创建post-commit钩子

最后,转到存储库的hooks目录并添加名为post-commit的shell脚本(名称很重要,否则SVN在提交后将不会执行它):

#!/bin/sh

# Name of the Jenkins build job
yourJob="your_job"

# You defined this in Jenkins' build job
build_token="yourSecretToken"

jenkins_address_with_port="localhost:8090"

curl $jenkins_address_with_port/job/$yourJob/build?token="$build_token"

使脚本可执行:chmod +x post-commit


这是post-commit的扩展版本,用于记录有关提交的数据,例如提交的作者。

#!/bin/sh

# The path to this repository
repo_path="$1"
# The number of the revision just committed
rev="$2"
# The name of the transaction that has become rev
transaction_name="$3"

# See http://svnbook.red-bean.com/en/1.7/svn.ref.svnlook.c.author.html
commit_author="$(svnlook author --revision $rev $repo_path)"
# The UUID of the repository, something like e3b3abdb-82c2-419e-a100-60b1d0727d12
repo_uuid=$(svnlook uuid $repo_path)

# Which files were changed, added, or deleted. For example:
# U   src/main/java/com/bullbytes/MyProgram.java
what_has_changed=$(svnlook changed --revision $rev $repo_path)

log_file=/tmp/post_commit.log

echo "Post-commit hook of revision $rev committed by $commit_author to repo at $repo_path with ID $repo_uuid was run on $(date). Transaction name: $transaction_name. User $(whoami) executed this script. This has changed: $what_has_changed" >> $log_file

# Name of the Jenkins build job
yourJob="your_job"

# You defined this in Jenkins' build job
build_token="yourSecretToken"

jenkins_address_with_port="localhost:8090"

curl $jenkins_address_with_port/job/$yourJob/build?token="$build_token"

要了解有关提交挂钩的更多信息,请前往docs