【问题标题】:Trigger Hudson build on SVN commit在 SVN 提交上触发 Hudson 构建
【发布时间】:2011-01-25 10:39:54
【问题描述】:

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

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

我想实现以下目标:

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

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

我已经看到了一些关于 post-commit 钩子的建议,但直到现在都没有奏效。

有人可以就这两个问题提出建议吗?具体步骤将不胜感激。

【问题讨论】:

  • 我认为 hudson 需要对您的存储库进行读取访问。 ICBWT。
  • 为什么post-commit hook 不起作用?验证?您还可以将其设置为定期轮询存储库以进行更改。
  • 将示例代码提交到 svn 时,返回“'post-commit' hook failed with error output”。
  • @canadiangeo:您找到解决问题的方法了吗?我希望你已经做到了。

标签: svn build hudson


【解决方案1】:

根据"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 的访问控制更加严格,您可能需要指定用户名和密码,具体取决于您的身份验证配置方式。

【讨论】:

【解决方案2】:

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

第一部分相当简单...使钩子转到 /var/lib/svn//hooks 并将 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(是西班牙语)

【讨论】:

    【解决方案3】:

    以下是所需的步骤:

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

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

    【讨论】:

    • 感谢您的回答。也许你是对的,如果每个开发人员都在不稳定的构建中得到通知会更有效。但是,我认为您在第 3 步中的建议很简单。 :) 我希望在 svn 中提交某些内容时触发构建,而不是每分钟轮询存储库。我想在这种情况下需要很多时间,并且可能会出现冲突(当 2 个开发人员同时提交他们的代码时)。还是我错了?
    • 我仍然建议配置 Hudson 来轮询存储库。这样,您可以在一个地方控制和配置您的构建。如果您愿意,您可以将提交后挂钩添加到 SVN,但我在使用上面概述的步骤时没有任何问题。
    【解决方案4】:

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

    1。在 Jenkins 中允许读取访问权限

    使用 Jenkins 的 Web 界面,转到 Manage Jenkins → Configure Global Security 并检查 Allow anonymous read access:

    如果您跳过此步骤,您将在尝试使用 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 提交挂钩):

    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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 2010-11-12
      • 1970-01-01
      • 2011-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多