这就是我在提交到 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。