这是我在提交到 SVN 仓库后,如何让 Jenkins 2.157 开始构建的方法。
1. 在 Jenkins 中允许读取权限
使用 Jenkins 的网页界面,进入 管理 Jenkins
→ 配置全局安全性
,勾选 允许匿名读取权限
:
![allow anonymous read access screenshot](https://istack.dev59.com/06bhd.webp)
如果您跳过此步骤,在尝试使用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 提交钩子):
![configure build trigger](https://istack.dev59.com/cXh1U.webp)
3. 创建 post-commit 钩子
最后,进入你的代码库的 hooks 目录,并添加一个名为 post-commit 的 shell 脚本(名称很重要,否则 SVN 在提交后不会执行它):
#!/bin/sh
yourJob="your_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
repo_path="$1"
rev="$2"
transaction_name="$3"
commit_author="$(svnlook author --revision $rev $repo_path)"
repo_uuid=$(svnlook uuid $repo_path)
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
yourJob="your_job"
build_token="yourSecretToken"
jenkins_address_with_port="localhost:8090"
curl $jenkins_address_with_port/job/$yourJob/build?token="$build_token"
要了解有关提交钩子的更多信息,请访问文档。