jenkins 使用 curl 触发 jenkins 编译 & 自动从ftp上下载更新程序库

来源:互联网 发布:淘宝香火符咒西极飞飞 编辑:程序博客网 时间:2024/06/04 20:02

关于在 git hooks 中使用 curl 触发编译:

假设项目名字叫 Two, 项目的 Authentication Token 为 ABC(同上).

方法1 . 在 Manage Jenkins -> Configure Global Security 中去掉 “Prevent Cross Site Request Forgery exploits” 选项,然后就可以使用下述命令触发编译了:

curl --user 'USER:PASSWD' -X POST "http://localhost:8080/job/Two/build?token=ABC"// orcurl --user 'USER:PASSWD' -X POST "http://localhost:8080/job/Two/build" --data token=ABC--data delay=0sec// 或者不使用密码,使用 user API token: 点击用户名-> Configure 显示 API Tokencurl -u guowei:173ey74ac39d284u610c83c6fd32847e -X POST http://localhost:8080/job/Two/build?token=ABC

方法2 . 如果选中了 “Prevent Cross Site Request Forgery exploits” 选项,则需要先获得一个 CSRF protection token,然后再将这个 token 作为HTTP请求的 header 发送过去:

curl -u guowei:38e2427ac39d5a5f810c83c6fd39ee80 http://localhost:8080/crumbIssuer/api/json// 你将获得一个返回json数据,例如:{"_class":"hudson.security.csrf.DefaultCrumbIssuer","crumb":"39v8495d439i36cbd93b928461u1fe15","crumbRequestField":"Jenkins-Crumb"}// 然后再这样触发编译:curl -u guowei:38e2427ac39d5a5f810c83c6fd39ee80  -H "Jenkins-Crumb:39v8495d439i36cbd93b928461u1fe15" -X POST http://localhost:8080/job/Two/build?token=ABC

如果觉得麻烦,可以写成这样一个脚本:

#!/bin/bashCRUMB=$(curl -s 'http://guowei:38e2427ac39d5a5f810c83c6fd39ee80  @localhost:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')curl -u guowei:38e2427ac39d5a5f810c83c6fd39ee80 -H "$CRUMB" -X POST "http://localhost:8080/job/Two/build" --data token=ABC --data delay=0sec

see link: http://zdk.github.io/jenkins-remote-build-trigger-with-bitbucket-hook

方法3 . 在新建 Job 的时候选择 Poll SCM, Schedule 可以为空. 使用这个方式只有在 git server 端代码有更新时才会触发build,没有更新不会触发。

在 git server 的 post-receive hook 中插入以下代码:

curl http://localhost:8080/git/notifyCommit?url=ssh://git@xxx.git&branches=master

Job 的界面中会出现一个 Git Polling Log 的选项,点进去可以看到日志信息。

see link: https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin

lftp 自动从服务器上下载更新程序包:

#!/bin/bashcd ~USER=guoweiPASSWD=xxxxxxIP=192.168.1.13REMOTEDIR="abc/efg"FILENAME=$(lftp -c "open -u $USER,$PASSWD $IP; cd $REMOTEDIR && cls myfile*")if [ "$FILENAME" != "" ]; thenlftp $USER:$PASSWD@$IP  << EOFcd $REMOTEDIRmget -E myfile* quitEOFecho "$FILENAME download successfully, extract? (y/n)"read ans# now check if $x is "y"if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then    tar -xvf $FILENAME    echo "update ok!"else        echo "update aborted!"fielse    echo "Cat not login to server: $USER@$IP"fi
0 0
原创粉丝点击