How do I trigger Jenkins build with SVN post-commit?
Asked Answered
M

2

0

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.

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

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.

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

Jenkins security setting

I have no idea why the error occurs at all.

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?

Misology answered 3/4, 2015 at 12:18 Comment(2)
Is localhost correct? I.e., are your SVN server and Jenkins server the same machine?Woodall
@PatrickQuirk Thanks for you concerning. Yes, they are running in the same machine, I am doing this for learning purpose.Misology
S
2

Please try with the below method:

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

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 note: All lines should be commented out. Add the below line at last.

Syntax (for Linux users):

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

Syntax (for Windows users):

C:/curl_for_win/curl http://username:API_token@localhost:8081/job/job_name/build?token=Token_name
Safeguard answered 23/10, 2017 at 8:59 Comment(2)
Is it really supposed to be forward slashes for Windows (the first part, C:/curl_for_win/curl)?Playbill
You don't even need curl for that, the alternate way is by using wget.Piroshki
R
0

I solved the problem by a modification of the example script inside : https://plugins.jenkins.io/subversion/

1) Create a Jenkins user (the one you would like to execute the post-commit comand) 2) Create an API token for this user. 3) change the attached script with user-name and token:

    #!/bin/bash
REPOS="$1"
REV="$2"

# No environment is passed to svn hook scripts; set paths to external tools explicitly:
WGET=/usr/bin/wget
SVNLOOK=/usr/bin/svnlook

# If your server requires authentication, it is recommended that you set up a .netrc file to store your username and password
# Better yet, since Jenkins v. 1.426, use the generated API Token in place of the password
# See https://wiki.jenkins-ci.org/display/JENKINS/Authenticating+scripted+clients
# Since no environment is passed to hook scripts, you need to set $HOME (where your .netrc lives)
# By convention, this should be the home dir of whichever user is running the svn process (i.e. apache)
HOME=/var/www/

UUID=`$SVNLOOK uuid $REPOS`

# Password correcponds to the API token.
JENKINS_USER="user"
JENKINS_PASSWORD="1166085xxxxxxxxe50067fb91866"

NOTIFY_URL="subversion/${UUID}/notifyCommit?token=${JENKINS_PASSWORD}?rev=${REV}"
CRUMB_ISSUER_URL='crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)'

function notifyCI {
    # URL to Jenkins server application (with protocol, hostname, port and deployment descriptor if needed)
    CISERVER=$1

    # Check if "[X] Prevent Cross Site Request Forgery exploits" is activated
    # so we can present a valid crumb or a proper header

    HEADER="Content-Type:text/plain;charset=UTF-8"
    CRUMB=`$WGET --auth-no-challenge --output-document - ${CISERVER}/${CRUMB_ISSUER_URL}`
    if [ "$CRUMB" != "" ]; then HEADER=$CRUMB; fi

      echo "$WGET --auth-no-challenge  --http-user=${JENKINS_USER}  --http-password=${JENKINS_PASSWORD} --header $HEADER --post-data \"`$SVNLOOK changed --revision $REV $REPOS`\" --output-document \"-\" --timeout=2 ${CISERVER}/${NOTIFY_URL}"
      $WGET --auth-no-challenge  --http-user=${JENKINS_USER}  --http-password=${JENKINS_PASSWORD} --header $HEADER --post-data "`$SVNLOOK changed --revision $REV $REPOS`" --output-document "-" --timeout=2 ${CISERVER}/${NOTIFY_URL}

}
# The code above was placed in a function so you can easily notify multiple Jenkins servers:
notifyCI "<base-url>" 1>&2 >> /opt/svn/repo/rds/hooks/post-commit.log
Rarefied answered 4/3, 2020 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.