Process
-
Install common software in provision script, such as JDK, Ant;
-
Write a shell script for a given build task;
-
Run this script in Jenkins;
Use Case
Vagrantfile
config.vm.box = "precise64"
config.vm.define "bs" do |bvtserver|
bvtserver.vm.hostname = "bvt-server"
bvtserver.vm.network "private_network", ip: "192.168.50.3",
virtualbox__intnet: "gcptest-network"
bvtserver.vm.provider :virtualbox do |vbox|
vbox.customize ["modifyvm", :id, "--natnet1", "10.3/16"]
end
bvtserver.vm.provision :shell, path: "bvtprov.sh", privileged: false
end
bvtprov.sh
This script install Subversion, zip, JDK and Ant on this VM.
1 2 3 4 5 6 7 8 9 10 11 |
|
A build task: build-gcp-web-6.1_P2_GZ.sh
This script checkout codes from Subersion server and compile, make zip for a specific project.
export PATH=$PATH:/home/vagrant/apps/jdk1.6.0_45/bin:/home/vagrant/apps/apache-ant-1.9.3/bin
export LC_ALL=en_US.UTF-8
projname="GCP6.1_P2_GZ"
svnprefix='svn co --non-interactive --no-auth-cache --username bvt --password mypwd'
svnroot='http://10.0.2.59:8118/svn/gcp/Development-Area'
$svnprefix ${svnroot}/7-Tools/deployment/packages/pubs /vagrant/packages
$svnprefix ${svnroot}/3-SCL/Branch/$projname /vagrant/codes/$projname
rm -rf /vagrant/codes/*.zip
web_build=/vagrant/codes/web_build
rm -rf $web_build; mkdir $web_build
tar zxf /vagrant/packages/apache-tomcat-6.0.39.tar.gz -C $web_build/
mv $web_build/apache-tomcat-6.0.39 $web_build/web_server
cp -r /vagrant/codes/$projname/WebRoot $web_build/web_server/webapps/$projname
src_dir=/vagrant/codes/$projname/src
class_dir=$web_build/web_server/webapps/$projname/WEB-INF/classes
lib_dir=/vagrant/codes/$projname/WebRoot/WEB-INF/lib
find $src_dir -type f -name "*.java" -exec javac -cp "$lib_dir/*:$src_dir" -d $class_dir {} \;
rsync -auC --exclude='*.java' $src_dir/ $class_dir/
cd /vagrant/codes/web_build; zip -q ../$projname.zip -r web_server -x "*/.svn/*"
pkg_prefix=/vagrant/codes/${projname}-
rm -rf ${pkg_prefix}*
pkgs=${pkg_prefix}$(date +%Y%m%dT%H%M)
mkdir $pkgs
cp /vagrant/codes/*.zip $pkgs
Run this task on Jenkins
Add the following shell commands in the "GCP-Web-6.1_P2_GZ" project in Jenkins:
cd ~/docs/bvtvm
vagrant ssh [vm-name] -c 'sh /vagrant/build-gcp-web-6.1_P2_GZ.sh'
The [vm-name] is the VM you want ssh to, "bs" in this case. If you have only one vm defined in Vagrantfile, you can omit the it, or you can get the value of it via vagrant status
;
Note: Try to avoid usage of relative path in scripts. If you have to(sush as make a tar file), use 'cd -' to jump back after operations.