如何在提交后使用SVN触发Jenkins构建

如何在提交后使用SVN触发Jenkins构建

本文介绍了如何在提交后使用SVN触发Jenkins构建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写SVN提交后脚本,目的是意识到,只要开发人员之一提交SVN存储库,它就会触发Jenkins构建并自动部署项目.

I am trying to compose an SVN post-commit script, with the purpose of realizing that, whenever one of the developers commit to an SVN repository, it will trigger a Jenkins build and deploy the project automatically.

我按照 Subversion插件 ,而我的提交后结果就像:

I followed the instructions in Subversion Plugin, and my post-commit is like:

#!/bin/sh
#
# Jenkins SVN Build trigger script by Wessel de Roode Aug' 2011
#

# Please adjust
SERVER=localhost
PORT=8080
WGET=/usr/bin/wget
SVNLOOK=/usr/bin/svnlook

# Don't change below this point
###############################

REPOS="$1"
REV="$2"
UUID=`$SVNLOOK uuid $REPOS`

echo "--------------------------------">>${REPOS}/post-commit.log
#
# Check if "[X] Prevent Cross Site Request Forgery exploits" is activated
# so we can present a valid crum or a proper header
BREAD_URL='http://'${SERVER}:${PORT}'/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)'

CRUMP=`$WGET --append-output=${REPOS}/post-commit.log --output-document -${BREAD_URL}`
if [ "$CRUMP" == "" ]
then
    HEADER="Content-Type:text/plain;charset=UTF-8"
else
    HEADER=$CRUMP
fi

$WGET \
    --http-user=JENKINS_USER --http-password=JENKINS_PW \
    --header ${HEADER} \
    --post-data "`$SVNLOOK changed --revision $REV $REPOS`" \
    --append-output=${REPOS}/post-commit.log  \
    --output-document "-"\
    --timeout=2 \
    http://${SERVER}:${PORT}/jenkins/subversion/${UUID}/notifyCommit?rev=$REV\

# Uncomment line below for debug
echo $(date) HEADER=${HEADER} REPOS=$REPOS REV=$REV UUID=${UUID} http://${SERVER}:${PORT}/subversion/${UUID}/notifyCommit?rev=$REV >>${REPOS}/post-commit.log

当我从SVN客户端提交内容时,日志如下:

When I commit something from an SVN client, the log is as follows:

--2015-04-03 21:01:20--  http://localhost:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,%22:%22,//crumb)
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:8080... connected.
HTTP request sent, awaiting response... 404 Not Found
2015-04-03 21:01:20 ERROR 404: Not Found.

Fri Apr 3 21:01:20 KST 2015 HEADER=Content-Type:text/plain;charset=UTF-8 REPOS=/home/share/svn/myblog REV=30 UUID=d6922f4b-358e-4015-8fd3-a25217326040 http://localhost:8080/subversion/d6922f4b-358e-4015-8fd3-a25217326040/notifyCommit?rev=30
--2015-04-03 21:01:20--  http://localhost:8080/jenkins/subversion/d6922f4b-358e-4015-8fd3-a25217326040/notifyCommit?rev=30
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:8080... connected.
HTTP request sent, awaiting response... 403 Forbidden
2015-04-03 21:01:20 ERROR 403: Forbidden.

对于找不到404错误" ,我检查了Jenkins中的全局安全性配置:

For the "404 Not Found error", I checked the Global Security configuration in Jenkins:

我根本不知道为什么会发生错误.

I have no idea why the error occurs at all.

对于"403禁止的错误" ,参考上一个屏幕截图,我提供了一个用户/密码JENKINS_USER/JENKINS_PW(即使他们说我将使用API​​令牌代替密码的纯文本),为什么禁止该密码?

And for the "403 Forbidden error", referring to the previous screen capture, I have provided a user/password, JENKINS_USER/JENKINS_PW (even though they say I shall use the API token instead of the plain text of the password), why is it forbidden?

推荐答案

请尝试以下方法:

您只需要一个插件,即Subversion插件.然后简单地进入Jenkins→job_name→ Build Trigger 部分→(i)远程触发构建(即通过脚本)身份验证令牌:Token_name

You need to require only one plugin which is the Subversion plugin. Then simply, go into Jenkins → job_name → Build Trigger section → (i) Trigger build remotely (i.e from scripts) Authentication token: Token_name

转到SVN服务器的hooks目录,然后触发以下命令:

Go to THE SVN server's hooks directory and after fire the below commands:

  1. cp post-commit.tmpl post-commit
  2. chmod 777 post-commit
  3. chown -R www-data:www-data post-commit
  4. vi post-commit注意:所有行都应被注释掉.最后添加以下行.
  1. cp post-commit.tmpl post-commit
  2. chmod 777 post-commit
  3. chown -R www-data:www-data post-commit
  4. vi post-commit note: All lines should be commented out. Add the below line at last.

语法(对于Linux用户):

/usr/bin/curl http://username:API_token@localhost:8081/job/job_name/build?token=Token_name

语法(对于Windows用户):

C:/curl_for_win/curl http://username:API_token@localhost:8081/job/job_name/build?token=Token_name

这篇关于如何在提交后使用SVN触发Jenkins构建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 16:14