Jenkins Configuration(3)Shell Script
JenkinsConfiguration(3)ShellScript
Jenkins,wecanmakeSVN,GIT,MAVEN,ANTworkingonthat.ButweneedtorestartJBOSSserver,copythepackagetoworkingdirectory,remoteaccessandsyncfilestootherservers.SoweneedthehelpfromShellScript.
1.BasicShellscriptandbasicexpectscript
Herearetheshellscript,server-deploy.sh
#!/bin/sh
#copythefiletopushdirectory
STORE_DIST=/android/work_space/push/projectname.war
rm-fr$STORE_DIST;
BUILD_TARGET=war
BUILD_WAR=$(ls$BUILD_TARGET/projectname-*-SNAPSHOT.war)
TMP=${BUILD_WAR%%.war}
unzip-q-d${TMP}"$BUILD_WAR";
cp-r${TMP}$STORE_DIST;
./server-autopush.exp
Weneedaexpfiletoremoteaccessotherserver,becauseserver-deploy.shisrunningonthejenkinsbuildserver.Weneedtosyncorrathersaypushthechangestoourtestingserver,productionserver.Justnotethemassomeotherlinuxmachine,andweneedtoremotelyrestarttheirJBOSS.
#!/usr/bin/expect-f
#December262012
#Thisscriptcanautomaticallypushthefiles
#to/opt/jboss-as/server/default/deploy/ontestingserver.
#TIMEOUT
settimeout20
#Loginparameters
setservertesting.server.com
setport22
setuserdeploy
setrootpasswd111111
#Logfile
log_fileserver-autopush-run.log
##EnablethisandDisablethe"spawnssh..."aboveifyouareusingssh-key.
#spawnssh-p$port$user@$server
spawnssh-p$port-i/home/build/.ssh/id_rsa$user@$server
#LoginasRoot
expect"*]$\"{send"su-root\r"}
expect"*assword:\"{send"$rootpasswd\r"}
#KillJbossontestingserver
expect"*]#\"{send"kill-9\`psaux|grep/opt/jboss-as/bin/run.jar|grep-vgrep|awk\'{print\$2}\'\`\r"}
#Syncthefilesfromtestingserverto
expect"*]#\"{send"rsync-arz--delete--force-e\"ssh-i/home/rsync/.ssh/rsync_servers.ppk\"[email protected]:/android/work_space/push/opt/build/buildserver/\r"}
#Copythefilesfrom/opt/build/buildserver/pushto/opt/jboss-as/server/default/deployontestingserver
expect"*]#\"{send"\\cp-rpa/opt/build/buildserver/push/*/opt/jboss-as/server/default/deploy/\r"}
#Removedirecotriesdata,log,work,tmp
expect"*]#\"{send"\\rm-rf/opt/jboss-as/server/default/tmp\r"}
expect"*]#\"{send"\\rm-rf/opt/jboss-as/server/default/work\r"}
expect"*]#\"{send"\\rm-rf/opt/jboss-as/server/default/data\r"}
#StartJbossontestingserver
expect"*]#\"{send"nohup/etc/init.d/jbossstart>/tmp/nohup.out\r"}
#ExitRoot
expect"*]#\"{send"exit\r"}
#ExitUser
expect"*]$\"{send"exit\r"}
#ExitExpect
expecteof
ButwhenIrunthismethodonmyserver,Igottheseerrormessages
ErrorMessage:
>log_file:commandnotfound
>spawn:commandnotfound
>expect:commandnotfound
>expect:commandnotfound
>expect:commandnotfound
>expect:commandnotfound
>expect:commandnotfound
>expect:commandnotfound
ThatisonlybecauseIdidnothaveexpectonmymachine.
Solution:
>yumupdate
>yuminstalltcl
>yuminstallexpect
2.ImprovementonSVNcheckoutprocess
OneimportimprovementfortheworkspaceworkingwithSVN:
RepositoryURL:babababababalalalalalaal
Localmoduledirectory(optional)Thisisusuallybe./
ButImyprojectisasubproject,soIneedsomecorecodesstandingthere.SoImakethemsvnupdatetooneplace
/opt/work_space
/opt/jenkins
SoIchangethe./tomostlylikethis:
./../../../../work_space/branches/development/project/sub_projects/myproject
3.Takesomeparametersinshellscriptandexpectscript
3.1Shellparameters
Imadeatestshellfilelikethistest.sh
!/bin/sh
echoThereare$#argumentsto$0:$*
echofirstargument:$1
echosecondargument:$2
echothirdargument:$3
echofourthargument:$4
echoheretheyareagain:$@
WhenIexecuteitwithparameters
>./test.sh1234
Thereare4argumentsto./test.sh:1234
firstargument:1
secondargument:2
thirdargument:3
fourthargument:4
heretheyareagain:1234
3.2Expectparameters
Andexampleintest.expworkingwithtest.sh
appendthislineintest.sh
./test.exph1h2
test.expcontentwillbelikethis>
#!/usr/bin/expect-f
setarg1[lindex$argv0]
setarg2[lindex$argv1]
send$arg1\n
send$arg2\n
Theresultwillbelikethis:
>./test.sh1234
Thereare4argumentsto./test.sh:1234
firstargument:1
secondargument:2
thirdargument:3
fourthargument:4
heretheyareagain:1234
h1
h2
4.SomeTipsaboutprivate/publickey
Addoneusernameddeploy
>/usr/sbin/useradddeploy
Givetheuseranpassword
>passwddeploy
Generatethekeypair
>ssh-keygen-trsa
Addthepublickeytoauthorize
>cd~/.ssh
>viauthorized_keys
>chmod711~/.ssh
>chmod644~/.ssh/authorized_keys
ClientServertriestologintotheremoteserver
>ssh-i/home/build/.ssh/[email protected]
Givetherightsofdirectorytooneuser
>chown-Rrsync:rsync/directory/name
rsyncistheusername,thesecondrsyncisthegroupname.
Checkthepublickeyfromprivatekey
>ssh-keygen-fid_rsa-y
references:
http://s3tools.org/s3cmd
http://www.garron.me/mac/install-s3cmd-mac-os-x.html
http://wangyan.org/blog/s3cmd-how-to-use.html
http://osr507doc.sco.com/en/OSUserG/_Passing_to_shell_script.html