配置钩子脚本

钩子脚本是SVN库中hooks/目录下的文件,通过配置钩子脚本可以实现SVN库中代码变更自动触发流水线任务。在使用SVN代码库前需执行本操作。

  1. 下载钩子脚本文件:

  1. 将鼠标悬浮于[产品与服务]图标之上,在右边选择[项目效能]菜单项,跳转到Devops云平台。

  1. 单击左侧导航树中的[项目]菜单项,单击对应的项目链接或新建项目进入项目管理页面。

  1. 单击<交付>按钮,进入项目交付页面。

  1. 单击<新建流水线>按钮,选择场景模板。

  1. 单击<下一步>按钮,编排流水线。单击,可以按需在模板已有任务基础上新增任务。

  1. 在初始配置页面的选择“监听设置”为自动触发,“代码库”选择对应的SVN代码库。

  1. 单击<下载钩子脚本并根据说明完成SVN服务配置>按钮下载钩子脚本文件post-commit

  1. 单击<查看脚本命令>按钮查看本流水线对应的curl脚本命令,并复制此curl脚本命令。

  1. SVN仓库所在的服务器上为post-commit文件授权,执行“chmod 700 post-commit”命令。

  1. 可选如下两种方式之一编辑钩子脚本:

说明

钩子脚本示例

#!/bin/sh

 

# POST-COMMIT HOOK

#

# The post-commit hook is invoked after a commit.  Subversion runs

# this hook by invoking a program (script, executable, binary, etc.)

# named 'post-commit' (for which this file is a template) with the

# following ordered arguments:

#

#   [1] REPOS-PATH   (the path to this repository)

#   [2] REV          (the number of the revision just committed)

#   [3] TXN-NAME     (the name of the transaction that has become REV)

#

# Because the commit has already completed and cannot be undone,

# the exit code of the hook program is ignored.  The hook program

# can use the 'svnlook' utility to help it examine the

# newly-committed tree.

#

# The default working directory for the invocation is undefined, so

# the program should set one explicitly if it cares.

#

# On a Unix system, the normal procedure is to have 'post-commit'

# invoke other programs to do the real work, though it may do the

# work itself too.

#

# Note that 'post-commit' must be executable by the user(s) who will

# invoke it (typically the user httpd runs as), and that user must

# have filesystem-level permission to access the repository.

#

# On a Windows system, you should name the hook program

# 'post-commit.bat' or 'post-commit.exe',

# but the basic idea is the same.

#

# The hook program runs in an empty environment, unless the server is

# explicitly configured otherwise.  For example, a common problem is for

# the PATH environment variable to not be set to its usual value, so

# that subprograms fail to launch unless invoked via absolute path.

# If you're having unexpected problems with a hook program, the

# culprit may be unusual (or missing) environment variables.

#

# CAUTION:

# For security reasons, you MUST always properly quote arguments when

# you use them, as those arguments could contain whitespace or other

# problematic characters. Additionally, you should delimit the list

# of options with "--" before passing the arguments, so malicious

# clients cannot bootleg unexpected options to the commands your

# script aims to execute.

# For similar reasons, you should also add a trailing @ to URLs which

# are passed to SVN commands accepting URLs with peg revisions.

#

# Here is an example hook script, for a Unix /bin/sh interpreter.

# For more examples and pre-written hooks, see those in

# the Subversion repository at

# http://svn.apache.org/repos/asf/subversion/trunk/tools/hook-scripts/ and

# http://svn.apache.org/repos/asf/subversion/trunk/contrib/hook-scripts/

#

# POST-COMMIT HOOK

#svn 钩子脚本配置:

#1.点击“下载钩子脚本” 下载文件(post-commit

#2.点击“查看脚本命令”:curl命令

注意:

#       1.下载钩子脚本(post-commit)名称和后缀不能修改,没有后缀

#       2.原来仓库下面/hooks/目录下面的其他文件不需要动,比如:post-commit.tmpl等不要动

#       3.服务器必须支持curl命令的运行

#       4.授权:chmod 700 post-commit

#3.将下载的post-commit文件放入仓库下面(仓库下面/hooks/目录下)

#    注意:

#       1.如果仓库下面/hooks/目录下已经有post-commit文件(不是post-commit.tmpl文件),不需要替换(已有数据),vi post-commit(文件看下内容:是否是下载的基础脚本和查看脚本命令curl

#       2.如果仓库下面/hooks/目录下没有post-commit文件,将下载的post-commit文件仓库下面/hooks/目录下

#4.编写脚本(post-commit

#       vi post-commit将第2步的curl命令复制到##流水线接口连接下面。保存退出。

#   注意:1.可以配置多个流水线,就可以编辑多个curl命令行

#        2.SVN存在多仓库,多项目,多流水线问题,所以这里需要自动触发的流水线,需要添加进来。

##

# 常见问题:

# 1.SVN提交代码时候报错:Error: post-commit hook failed (exit code 255) with no output.

#   解决方式:进入服务器授权:chmod 700 post-commit

 

 

REPOS="$1"

REV="$2"

TXN_NAME="$3"

 

$(> ${REPOS}/temp)

for a in `svnlook dirs-changed ${REPOS} -r ${REV}`

do

 echo -n ${a}, >> ${REPOS}/temp

done

dirsfiles=$(cat ${REPOS}/temp)

dirsfiles=${dirsfiles%*,}

 

##流水线接口连接

curl -i -X POST -H "content-type: application/json" -H "user_id:f1f7562f-ce80-44e6-bcea-b3c315b27c75"  -H "x-auth-token:43b64e61-347c-4123-abb2-34de4acdfff0"   http://10.51.93.1:34568/api/v1.0/fluxes?Action=AddExecution  -d '{"pipelineId":"1538740266939187200","parameters":{},"note":""}'

curl -i -X POST -H "content-type: application/json" -H "user_id:f1f7562f-ce80-44e6-bcea-b3c315b27c75"  -H "x-auth-token:43b64e61-347c-4123-abb2-34de4acdfff0"   http://10.51.93.1:34568/api/v1.0/fluxes?Action=AddExecution  -d '{"pipelineId":"1538740266939187200","parameters":{},"note":""}'