You are on page 1of 61

SCM/Build &

Release
Management

Chiranjeevi Patel
SCM/Build & Release Management 2012

Contents
Software Configuration Management .......................................................................................................... 4
Typical SCM Functions .............................................................................................................................. 4
Software Configuration Management Plan Purpose ................................................................................ 4
Source Code Management / Version Control System: ................................................................................. 5
Advantages of VCS .................................................................................................................................... 5
Without VCS what are all disadvantages: ................................................................................................. 6
Example tools for VCS: .......................................................................................................................... 6
What is a Repository? ........................................................................................................................... 6
Subversion (SVN):...................................................................................................................................... 6
History ................................................................................................................................................... 6
Edition ................................................................................................................................................... 7
SVN Repository ..................................................................................................................................... 7
Working Copy:....................................................................................................................................... 7
Checkout & Check-in: ............................................................................................................................ 7
Repository Vs Working copy: ................................................................................................................ 7
Revision number: .................................................................................................................................. 7
Atomic commits: ................................................................................................................................... 7
Subversion Architecture: ...................................................................................................................... 7
Installation: ........................................................................................................................................... 8
Configuring Subversion and Apache : ................................................................................................... 8
Subversion basic commands: .................................................................................................................... 9
All basic svn commands: ..................................................................................................................... 10
SVN Directory structure .......................................................................................................................... 12
svn copy .............................................................................................................................................. 12
svn switch ............................................................................................................................................ 12
svn blame ............................................................................................................................................ 12
svn merge ............................................................................................................................................ 12
Release Management: ................................................................................................................................ 12
An Agile Approach to Release Management .......................................................................................... 13
SVN Administration ..................................................................................................................................... 13

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 1


SCM/Build & Release Management 2012
SVN Authorization ................................................................................................................................... 13
SVN Commands................................................................................................................................... 15
SVN Hooks ............................................................................................................................................... 15
Pre-commit: ........................................................................................................................................ 15
Post-commit ........................................................................................................................................ 16
SVN UI ..................................................................................................................................................... 16
Features: ............................................................................................................................................. 16
Build Automation ........................................................................................................................................ 17
Ant scripting: ........................................................................................................................................... 17
Installation: ......................................................................................................................................... 17
Running Ant......................................................................................................................................... 18
Other tasks: ......................................................................................................................................... 20
Properties:........................................................................................................................................... 22
Fileset Type ......................................................................................................................................... 23
Archive tasks: ...................................................................................................................................... 24
AntCall : ............................................................................................................................................... 25
Ant inputs: ........................................................................................................................................... 26
Path & Classpath ................................................................................................................................. 26
Advanced tasks: .................................................................................................................................. 27
Ant Options: ........................................................................................................................................ 28
Sample Project demo .......................................................................................................................... 28
Review yourself: .................................................................................................................................. 29
Continuous Integration ............................................................................................................................... 30
Jenkins:.................................................................................................................................................... 30
Jenkins Architecture:........................................................................................................................... 31
Jenkins Installation .............................................................................................................................. 31
Build Agent: ......................................................................................................................................... 31
Project Job Creation: ........................................................................................................................... 32
Security: .............................................................................................................................................. 32
Plugins: ................................................................................................................................................ 33
Deployment Automation ............................................................................................................................ 33

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 2


SCM/Build & Release Management 2012
Linux basics: ............................................................................................................................................ 35
File Handling........................................................................................................................................ 35
Text Processing ................................................................................................................................... 36
Linux File Permissions ......................................................................................................................... 37
System Administration ........................................................................................................................ 37
Process Management ......................................................................................................................... 38
Archival ............................................................................................................................................... 38
Network .............................................................................................................................................. 39
File Systems ......................................................................................................................................... 39
Advanced Commands ......................................................................................................................... 40
Editor commands ................................................................................................................................ 40
Process Management ......................................................................................................................... 40
Exercise 1: ........................................................................................................................................... 41
Exercise 2: ........................................................................................................................................... 41
Shell Scripting:......................................................................................................................................... 44
Introduction ........................................................................................................................................ 44
Shell Scripting Basics ........................................................................................................................... 45
Variables.............................................................................................................................................. 46
Command Line Arguments ................................................................................................................. 47
Command Substitution ....................................................................................................................... 48
Arithmetic Expansion .......................................................................................................................... 48
Control Constructs .............................................................................................................................. 48
Functions ............................................................................................................................................. 53
Variable Scope..................................................................................................................................... 54
Exercise: .............................................................................................................................................. 57

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 3


SCM/Build & Release Management 2012

Software Configuration Management


Software Configuration Mgmt is a software development support and control function.

Specializing in software building, installation packaging, change management and

configuration management.

SCM ensures the integrity, reliability and reproducibility of software from conception to

release to retirement.

Typical SCM Functions


Software Builds

Source Code Administration

Install Packaging

Software Change Management

Configuration Management

Software Configuration Management Plan Purpose


The purpose of this plan is to provide basic guidelines for how source code and software

builds will be managed.

This document will cover source code administration, build environment standards and

define the process by which new components will be added to builds, and a common

understanding of how to manage broken builds.

The intent of Software Configuration Management Plan is to ensure consistent software

Quality across projects by ensuring that:

o All components are managed in a source control tool


o All components are compiled on an independent and controlled build machine
o Build machine specifications are controlled and documented

Typical SCM Plan Requirements

o Well Defined Set of Configuration Items to be Managed

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 4


SCM/Build & Release Management 2012
o Assign Roles and Responsibilities
o Define Software Configuration Management Standards and Procedures

Source Code Management / Version Control System:


Source Code is typically a file, or set of files, that contain human readable text that

when it is interpreted or compiled by a computer, it performs a task, or runs a computer

program.

The Internet browser you are using to read this web page is an example of a compiled

computer program.

This file, or set of files, contain a set of instruction that tell the computer what to do.

Most computer programs are made up of hundreds or thousands of files containing

source code.

These computer instructions are written in a computer language that can be interpreted

or compiled on the computer by tools known as an Interpreter or Compiler.

Examples of computer languages are C, C++, Java, Python, Perl and C#.

Version control is defined as series of processes, used to manage and maintain multiple
revisions of all the objects in a shared repository.

With version control, we can

Keep track of changes and process control


Investigate changes and revert
Several people can work on same project or file simultaneously
Merge changes
Provides audit report who modified, when modified, what are the
Changes made etc
Possible to recreate the source base from which software system was built

Advantages of VCS
A place to store your code
Historical record of what was done over time
Synchronization between the developers
Facilitates automated build and deploy
Enables the developers to work in parallel.
Standardized application lay out.

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 5


SCM/Build & Release Management 2012

Without VCS what are all disadvantages:


Needs to maintain the source code in a shared directory.
Every one overwrites the source code.
No revert
Not feasible to work in parallel.
No process control, no change control.
Not possible to get who modified the file, what are the changes he made etc
Not possible to resolve merger conflicts.

Example tools for VCS:


Commercial tools:

Perforce, IBM clearcase, VFS, TFS,

Open Source tools:

Subversion (SVN), CVS, Git, Mercurial ..etc

What is a Repository?
Repository is much like an ordinary file server except that it remembers everything that is
done to your files and directories.

Network-wide file system resource which stores version-controlled data. We can store all
source code and project related documents such Project Plan, Design Document, DSRS, etc.

Version tracking
Collaboration and sharing files
Historical information
Retrieve past versions
Manage branches

Subversion (SVN):
Subversion is a version control system, successor to CVS

Manages files and directories over time


Maintains files and directories in a
central repository
Can manage any collection of files,
including source code
Also known as SVN
Platform independent

History
From a CollabNet perspective, the SVN project started in 2000. After encountering

numerous problems with integrating their flagship product CollabNet EnterPrise

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 6


SCM/Build & Release Management 2012
Edition with CVS, they decided to create a new open source tool that fixed the CVS

limitations.

Hence, Subversion was born...

SVN Repository: The repository is where files are stored under Subversion on the server.

Working Copy: The contents of a project in the repository are usually copied into a local
directory. This is known as the working directory, and is separate from the repository

Checkout & Check-in: The operation of copying the project to the local directory is called
"check out" and the reverse is "check in".

Repository Vs Working copy:


Project code is stored in a server in a data store referred to as a repository.
Developers check out copies of the project code into their local environments.
These
Copies are referred to as working copies. After making changes to a working copy,
the
Developer commits changes to the repository.
Other developers get these changes by updating their working copies.

Revision number:
Global, Snapshot of the repository, unique id.
Revision numbers are global across the whole repository. Each commit increases the
version of the repository
Identify how the entire repository looks at that instant in time. A commit creates a
snapshot of the entire tree in the repository at that revision number
After committing to a repository with revision number n, the repository is changed to
version n+1.
After the repository is initially created, it is an emptyfolder and has revision number
0.

Atomic commits:
A collection of modifications either goes into the repository completely, or not at all.
In other words, a commit will either altogether succeed, or it will altogether fail.
Data Integrity
Exception limits on http.

Subversion Architecture:
Subversion is client-server based disconnected architecture.

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 7


SCM/Build & Release Management 2012

Installation:
Require Subversion Installer and Apache server as subversion independently does not
support remote access and security

Apache download:

http://httpd.apache.org/download.cgi (Apache 2.2)

Subversion download:

http://subversion.apache.org/packages.html Version: version 1.7.6 (r1370777)

Configuring Subversion and Apache :


1. Install subversion and Apache

2. Create a Repository using svnadmin command

svnadmin create D:\MyProject

3. SVN & Apache configuration:

a) Copy C:\Program Files\Subversion\bin\mod_authz_svn.so and C:\Program


Files\Subversion\bin\mod_dav_svn.so from subversion instal path to Apache ( C:\Program
Files\Apache Software Foundation\Apache2.2\modules)

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 8


SCM/Build & Release Management 2012
b) Open C:\Program Files\Apache Software Foundation\Apache2.2\conf/httpd

Add below 3 lines at the end of the Load modules

LoadModule dav_module modules/mod_dav.so

LoadModule dav_svn_module modules/mod_dav_svn.so

LoadModule authz_svn_module modules/mod_authz_svn.so

c) At the end of the file (httpd file) add the below lines

<Location /svn>

DAV svn

SVNPath D:/MyProject

</Location>

4) Restart Apache

Subversion basic commands:


How to create a workspace ?

svn checkout http://localhost/svn

How to add files to workspace?

Enter into the workspace directory

svn add filename Adding single file

svn add R *.* Adding multiple files at a time

How do we send the modified files to svn repository server?

svn commit m file.txt adding files to sever adding single file

svn commit m adding files to sever by default commit pickups all changed files and
send to repository server

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 9


SCM/Build & Release Management 2012
All basic svn commands:

svn checkout/co
svn checkout or svn co. This command is used to pull an SVN tree such
as http://localhost/svn from the server. You should only need to do this once. If the
directory structure is changed (as is sometimes necessary), you may occasionally need to
delete your local sand box and re-check it out..

svn add
svn add. When you are creating a new file or directory, you need to tell the SVN server
about it. This command does that. Note that the file won't appear in the repository until you
do an svn commit (see below).

svn delete (del,rem,remove)


svn delete. This does what it says! When you do an svn commit the file will be deleted
from your local sand box immediately as well as from the repository after committing.

svn status (st, stat)


svn status. This command prints the status of working directories and files. If you have
made local changes, it'll show your locally modified items.

svn update/up
svn update or svn up. This command syncs your local sand box with the server. If you
have made local changes, it will try and merge any changes on the server with your
changes on your machine.

svn commit/ci
svn commit or svn ci. This command recursively sends your changes to the SVN server. It
will commit changed files, added files, and deleted files. Note that you can commit a change
to an individual file or changes to files in a specific directory path by adding the name of the
file/directory to the end of the command. The -m option should always be used to pass a
log message to the command. Please don't use empty log messages (see later in this
document the policy which governs the log messages).

svn diff (di)


svn diff. This is useful for two different purposes. To do this, simply edit the files in your
local sand box then run svn diff > FILE.patch from the root of your BLFS directory. The
second use is to find out what has changed between two revisions using: svn diff -r
revision1:revision2 FILENAME. For example: svn diff -r 168:169 index.xml will output
a diff showing the changes between revisions 168 and 169 of index.xml.

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 10


SCM/Build & Release Management 2012
svn move
svn move SRC DEST or svn mv SRC DEST or svn rename SRC DEST or svn ren SRC
DEST. This command moves a file from one directory to another or renames a file. The file
will be moved on your local sand box immediately as well as on the repository after
committing.

SVN Cat:
svn cat http://localhost/svn/test.txt shows the contents of a file on console.

SVN Cleanup:
Svn cleanup will cleans up if any conflicts happens on SVN workspace

SVN export:
Export a clean directory tree, which is not a workspace as we cannot perform any svn
operations on exported codebase.
svn export http://localhost/svn
Export complete.

SVN import
Svn import pushes the non workspace files to svn repository server
svn import file.txt http://localhost/svn/file.txt -m imported file.txt tory

SVN info
Which is a workspace command and provides information about SVN workspace URL,
revision no etc.

SVN list
Lists the files and directories inside a repository locations
svn list http://localhost/svn

SVN log
Displays the history of a file or a directory in SVN with all it previous revision numbers.
svn log http://localhot/svn

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 11


SCM/Build & Release Management 2012
SVN mkdir
Creates the directory inside workspace, no need to add a directory by creating on file
system.

SVN Directory structure


Depending on type release process the SVN structure will be decided. The conventional SVN structure is
shown below

svn\trunk,

svn\branches,

svn\tags

svn copy
We can create the branches and tags using svn copy comand

svn copy <source branch url> <destination branch url> -m "comment"

svn switch
This command is used to switch between the branches without checking out the codebase

svn switch http://localhost/branches/branch1

svn blame:

svn blame Show author and revision information inline for the specified files or URLs.

svn blame http://localhost/svn/test.txt

svn merge This comand used to merge code from one branch to other branch

svn merge http://localhost/release2

Release Management:
Release management is a software engineering process intended to oversee the development, testing,
deployment and support of software releases. The practice of release management combines the
general business emphasis of traditional project management with a detailed technical knowledge of
the systems development lifecycle (SDLC) .

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 12


SCM/Build & Release Management 2012
Release management usually begins in the development cycle with requests for changes or new
features. If the request is approved, the new release is planned and designed. The new design enters the
testing or quality assurance phase, in which the release is built, reviewed, tested and tweaked until it is
ultimately accepted as a release candidate. The release then enters the deployment phase, where it is
implemented and made available. Once deployed, the release enters a support phase, where bug
reports and other issues are collected; this leads to new requests for changes, and the cycle starts all
over again.

An Agile Approach to Release Management


Teams practicing Agile Software Development value working software over other artifacts. A feature
from the release plan is not complete until you can demonstrate it to your customer, ideally in a
shippable state. Agile teams strive to have a working system ("potentially shippable") ready at the end
of each iteration. Thus Release Management should be easy for an ideal agile team, as agile teams, in
theory, are ready to release at regular intervals, and the release management aspect is the customer
saying "ship it!."
Agile teams work under the same constraints as other software development teams, having to address
issues of maintenance and support, the need for external artifacts like documentation and training, and
the reality that not every customer can accept change at the rate that an agile team can (in theory)
deliver it. To attain the goal of a shippable product at the end of every iteration, an agile team must
manage the flow of change from a customer, and maintain high discipline and good engineering
practices.
Please refer the mentioned URL for detailed agile process
http://www.scmpatterns.com/pubs/crossroads-mirror/2008-05-CMCrossroads.pdf

SVN Administration
SVN Administration involves below activities

Creating repository
Creating authentication and authorization repository
Provide user credentials to the developers
Giving access to users on respective repository locations
Maintaining groups in svn access file
Creating branches for new releases
Help the developers while merging the code
Take the dump of SVN repository on frequent intervals

SVN Authorization setup


Create authentication file on SVN server using apache htpasswd file

Create D:\etc\svn_auth_file by using below command inside apache bin

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 13


SCM/Build & Release Management 2012
cd C:\Program Files (x86)\Apache Software Foundation\Apache2.2\bin>

htpasswd.exe -c D:\etc\svn-access harry

passwd: *****

Reconfirm: *****

Create authorization file to givie the proper access to the users created

Create another file svn-access

Provide access to the users which you created

---------------------------------------------------------

[groups]

devtemaf1 = seetaram, surech, Janardhan, santosh

[svnrepo:/branches/Feature1]

harry = rw

[svnrepo:/branches/Feature2]

sally = rw

---------------------------------------------------

Configure the above file in httpd.conf file where your repository added

<Location /svnrepo>

DAV svn

SVNPath D:/SVN_REPOSITORY/Project1

AuthType Basic

AuthName "Subversion Project1 repository"

AuthUserFile D:/etc/svn-users

Require valid-user

AuthzSVNAccessFile D:/etc/svn-access

</Location>

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 14


SCM/Build & Release Management 2012
SVN Commands
These commands works when you configure authorization on SVN

svn lock : which creates a lock on single file, so that no other developer can change this file

but still other can unlock this file and change it with below command

svn unlock
svn lock a.txt

svn unlock --force a.txt

SVN Hooks
A hook is a program triggered by some repository event, such as the creation of a new revision or the
modification of an unversioned property.

A SVN hook resides inside the SVN repository, which you create after SVN installation.

Types of hooks

Pre-commit:
Notification just prior to commit completion

The pre-commit hook is run just before a commit transaction is promoted to a new revision. Typically,
this hook is used to protect against commits that are disallowed due to content or location (e.g., your
site might require that all commits to a certain branch include a ticket number from the bug tracker, or
that the incoming log message is nonempty)

Example for pre-commit hook

Requirement: Users should be restricted if they commit anything without log message

Write a batch script to implement the pre-commit hook in windows.

@echo off
:: Stops commits that have empty log messages.
@echo off
setlocal
rem Subversion sends through the path to the repository and transaction id
set REPOS=%1
set TXN=%2

svnlook log %REPOS% -t %TXN% | findstr . > nul


if %errorlevel% gtr 0 (goto err) else exit 0

:err

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 15


SCM/Build & Release Management 2012
echo. 1>&2
echo Your commit has been blocked because you didn't enter a comment. 1>&2
echo Write a log message describing the changes made and try again. 1>&2
echo Thanks 1>&2
exit 1

Post-commit
post-commit Notification of a successful commit.

The post-commit hook is run after the transaction is committed and a new revision is created. Most
people use this hook to send out descriptive emails about the commit or to notify some other tool (such
as an issue tracker) that a commit has happened. Some configurations also use this hook to trigger
backup processes.

If the post-commit hook returns a nonzero exit status, the commit will not be aborted since it has
already completed. However, anything that the hook printed to stderr will be marshalled back to the
client, making it easier to diagnose hook failures.

SVN UI
All the SVN activities can be done from user end using tortoise svn which is a graphical user interface for
end users.

TortoiseSVN is an Apache Subversion (SVN) client, implemented as a windows shell extension. It's
intuitive and easy to use, since it doesn't require the Subversion command line client to run. Simply the
coolest Interface to (Sub) Version Control!

You can download and install the Tortoise SVN from below URL

http://tortoisesvn.net/downloads.html

Features:

Easy to use
All commands are available directly from the windows explorer.
Only commands that make sense for the selected file/folder are shown. You won't see any
commands that you can't use in your situation.
See the status of your files directly in the Windows explorer
descriptive dialogs, constantly improved due to user feedback
allows moving files by right-dragging them in the windows explorer

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 16


SCM/Build & Release Management 2012

Build Automation
Build automation is the act of scripting or automating a wide variety of tasks that software
developers do in their day-to-day activities including things like:

compiling computer source code into binary code


packaging binary code
running tests
deployment to production systems

We have different types of scripting available in the market to make the software builds as an
automated task.

Ex: Ant scripting, Maven, groovy etc

Ant scripting:
Ant originally an acronym for Another Neat Tool

Apache Ant is a software tool for automating software build processes


It is a powerful XML-based scripting tool which can automate your mundane tasks and allow you
to concentrate on your business rules and code development.
Ant specification is available at http://ant.apache.org/
It allows the developer to automate the repeated process involved in the development of J2EE
application.
Developers can easily write the script to automate the build process like compilation, archiving
and deployment.
It is platform independent tool
Ant is particularly good at automating complicated repetitive tasks and thus is well suited for
automating standardized build processes.
Ant accepts instructions in the form of XML documents thus is extensible and easy to maintain

Installation:
Download the ant from below URL and unzip it.
http://apache.techartifact.com/mirror//ant/binaries/apache-ant-1.8.4-bin.zip

After downloading add the Ant bin directory to your path (for Windows)

o set ANT_HOME = C:\ant\apache-ant-1.6.1


o set JAVA_HOME = C:\jdk1.4
o set PATH = %PATH%;%ANT_HOME%\bin

Test installation of Ant by running this command

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 17


SCM/Build & Release Management 2012
C:\Documents and Settings\user>ant -version

Apache Ant version 1.6.5 compiled on June 2 2005

Running Ant
Ant by default always looks for a file named build.xml. If a build file other than build.xml needs to be
used then ant should be run with options

ant buildfile anotherbuild.xml

ant file anotherbuild.xml

ant f anotherbuild.xml

Build file contains exactly only one project.

A typical build file looks as below:

<?xml version=1.0 ?>

<project>

<target>

task 1 .

task 2 .

</target>

</project>

Every build files can contain one Project.


Every project can have multiple Targets each one for a specific purpose
Targets can be dependent on one another
Every Target is made up of numerous Tasks bases on the Targets requirement

Project Tag defines the properties of the build project

Each project defines one or more targets. A target is a set of tasks you want to be executed. When
starting Ant, you can select which target(s) you want to have executed. When no target is given, the
project's default is used.

This is the root element of the all Ant build files.

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 18


SCM/Build & Release Management 2012
Attributes:

name Project Name

default The default target

basedir The base directory for all file and path references. The default value is the current
directory.

Target Tag defines the each target build

A target can depend on other targets. You might have a target for compiling, for example, and a target
for creating a distributable. You can only build a distributable when you have compiled first, so the
distribute target depends on the compile target. Ant resolves these dependencies.

Attributes

name target name

depends a comma separated list of targets on which this target depends on

description a short description about this target build

E.g.

<target name= ProgA depends= LibB, LibC>

<target name= LibB depends= LibC>

<target name= LibC >

When you set dependencies like this, before the target ProgA is built, Ant will check for targets,
LibB and LibC. If they have not already been built using the latest versions of their source codes,
they will be built before ProgA.

Tasks tag that represents the actions that has to be done during the build process

A task is a piece of code that can be executed. A task can have multiple attributes (or arguments, if you
prefer). The value of an attribute might contain references to a property. These references will be
resolved before the task is executed.

Tasks have a common structure:

<name attribute1="value1" attribute2="value2" ... />

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 19


SCM/Build & Release Management 2012
Example build file:

<?xml version="1.0"?>

<project name="Sample Buildfile" default="compile" basedir=".">

<property name = "src.dir" value="src"/>

<property name = "build.dir" value="build"/>

<property name = "build.classes" value="${build.dir}/classes"/>

<property name = "build.lib" value="${build.dir}/lib"/>

<target name="prepare">

<mkdir dir ="${build.dir}"/>

<mkdir dir ="${build.classes}"/>

<mkdir dir ="${build.lib}"/>

</target>

<target name="clean" description="Removes all generated files">

<delete dir="${build.dir}"/>

</target>

<target name="compile" depends="prepare" description="Compile all source code">

<javac srcdir="${src.dir}" destdir="${build.classes}"/>

</target>

<target name="jar" depends="compile" description="Prepares jar and placed in dist">

<jar jarfile="${build.lib}/sample.jar" basedir="${build.classes}" excludes="**/*Test.class"/>

</target>

<target name = "all" depends = "clean,jar" description"Clean,compiles, then bulds the JAR file"/>

</project>

Other tasks:

Echo task: Echoes a message to the current loggers and listeners which means System.out unless
overridden. A level can be specified, which controls at what logging level the message is filtered at.
<echo message="Hello, world"/>

Mkdir task: Creates a directory. Also non-existent parent directories are created, when necessary.
Does nothing if the directory already exist
<mkdir dir="build"/>

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 20


SCM/Build & Release Management 2012
Copy task: Copies a file or resource collection to a new file or directory. By default, files are only copied
if the source file is newer than the destination file, or when the destination file does not exist. However,
you can explicitly overwrite files with the overwrite attribute

Copy a single file

<copy file="myfile.txt" tofile="mycopy.txt"/>

Copy a single file to a directory

<copy file="myfile.txt" todir="../some/other/dir"/>

Copy a directory to another directory

<copy todir="../new/dir">
<fileset dir="src_dir"/>
</copy>

Move task: Moves a file to a new file or directory, or collections of files to a new directory. By default,
the destination file is overwritten if it already exists. When overwrite is turned off, then files are only
moved if the source file is newer than the destination file, or when the destination file does not exist

Move a single file (rename a file)

<move file="file.orig" tofile="file.moved"/>

Move a single file to a directory

<move file="file.orig" todir="dir/to/move/to"/>

Move a directory to a new directory

<move todir="new/dir/to/move/to">
<fileset dir="src/dir"/>
</move>

Javac task: Compiles a Java source tree. The source and destination directory will be recursively
scanned for Java source files to compile. Only Java files that have no corresponding .class file or where
the class file is older than the .java file will be compiled

<javac srcdir="src"
destdir="build/classes"
/>

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 21


SCM/Build & Release Management 2012
Sample build script for HelloWorld program

Please follow the below link to complete the exercise

http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html

How do we interpret the manual build steps into Ant build script

java-only Ant
md build\classes <mkdir dir="build/classes"/>
javac <javac
-sourcepath src srcdir="src"
-d build\classes destdir="build/classes"/>
src\oata\HelloWorld.java <!-- automatically detected -->
echo Main-Class: <!-- obsolete; done via manifest tag -->
oata.HelloWorld>mf <mkdir dir="build/jar"/>
md build\jar <jar
jar cfm destfile="build/jar/HelloWorld.jar"
build\jar\HelloWorld.jar
mf basedir="build/classes">
-C build\classes <manifest>
. <attribute name="Main-Class"
value="oata.HelloWorld"/>
</manifest>
</jar>
java -jar <java jar="build/jar/HelloWorld.jar"
build\jar\HelloWorld.jar fork="true"/>

Properties:
These are like variables in programming languages.

Properties are immutable: whoever sets a property first freezes it for the rest of the build; they are most
definitely not variables.

Ex:

<property name="source_dir" value="src"/>


Or

<property name="source_dir">src</property>

Properties can be placed in separate file and this file can be imported into the build script.

Ex:

<property file="build.properties"/>

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 22


SCM/Build & Release Management 2012
There few built in properties available with ant, please find them below

basedir the absolute path of the project's basedir (as set


with the basedir attribute of <project>).
ant.file the absolute path of the buildfile.
ant.version the version of Ant
ant.project.name the name of the project that is currently executing;
it is set in the name attribute of <project>.
ant.project.default-target
the name of the currently executing project's
default target; it is set via the default
attribute of <project>.
ant.project.invoked-targets
a comma separated list of the targets that have
been specified on the command line (the IDE,
an <ant> task ...) when invoking the current
project.
ant.java.version the JVM version Ant detected; currently it can hold
the values "1.2", "1.3",
"1.4", "1.5" and "1.6".
ant.core.lib the absolute path of the ant.jar file.
ant.home home directory of Ant

read the properties inside script using ${property_name} string.

Fileset Type
Fileset is a subtask used within the other tasks like copy,move ..etc

Attributes

dir base directory for the file set

This type is used to specify a list of files to be processed. This can be done using the nested tags given
below.

<include> tag

Attributes:

name name or the pattern of the file/files to be selected.

<includesfile> tag

Attributes:

name name of a text file, whose each line represents a pattern of files to be included.

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 23


SCM/Build & Release Management 2012
<exclude> tag

This is similar to include tag, but used to remove the files that have been included using the

Include and includesfile tags.

<excludesfile> tag

This is similar to includesfile tag, but used to remove the files that have been included using the
include and includesfile tag.

Ex:

<fileset basedir="bin">

<include name="*.java"/>

<include name=test.java"/>

</fileset>

Patterns:

* Any one or many characters

? Any one character

** Any directory or its sub directory

Ex:.

build/*.java all java files in build directory

build/** all the files in build directory and all its sub directories

**/mydir/* - all the files in the directory named mydir in any location in the directory tree

Archive tasks:
Jar : Jars a set of .class files.

The basedir attribute is the reference directory from where to jar.

Ex:

<jar destfile="lib/app.jar" basedir="${build}/classes"/>


Jar with manifest file

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 24


SCM/Build & Release Management 2012
<jar destfile="build/main/checksites.jar" basedir=build/classes>
<manifest>
<attribute name="Main-Class"
value="com.acme.checksites.Main"/>
</manifest>
</jar>

Zip : Creates a zipfile.

The basedir attribute is the reference directory from where to zip.

<zip destfile="${dist}/manual.zip"

basedir="htdocs/manual"

/>

Unzip: Unzips a zip-, war-, or jar file.

PatternSets are used to select files to extract from the archive. If no patternset is used, all files are
extracted.

<unzip src="${tomcat_src}/tools-src.zip" dest="${tools.home}"/>

War
An extension of the Jar task with special treatment for files that should end up in the WEB-INF/lib, WEB-
INF/classes or WEB-INF directories of the Web Application Archive.

Ex: <war destfile="dist/AntExample.war" webxml="WebContent/WEB-INF/web.xml">

<fileset dir="WebContent"/>

<lib dir="WebContent/WEB-INF/lib"/>

<classes dir="build/classes"/>

</war>

AntCall :
Call another target within the same buildfile optionally specifying some properties (params in this
context). This task must not be used outside of a target.

Ex:

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 25


SCM/Build & Release Management 2012
target name="default">
<antcall target="doSomethingElse">
<param name="param1" value="value"/>
</antcall>
</target>

<target name="doSomethingElse">
<echo message="param1=${param1}"/>
</target>

Ant inputs:
These are command line inputs passed to ant at run time.

Ex: ant f build.xml Dinput=value

${input} can be accessible from the build script and it contains the value as value

We can pass multiple inputs from command line

Ex: ant f build.xml Dinput=value Denv=QA Djdk=1.7

Path & Classpath variables:

Path Data type , is using to set the path.

Ex: <path>

<pathelement location=${libdir}/servlet.jar/>

<pathelement path=${builddir}/>

</path>

<classpath>

<pathelement path=${builddir}/>

<fileset dir=${libdir} includes=**/*.jar/>

</classpath>

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 26


SCM/Build & Release Management 2012
Advanced tasks:

Exec task:
Executes a system command. When the os attribute is specified, then the command is only executed
when Apache Ant is run on one of the specified operating systems.

Ex: <exec dir="${src}" executable="cmd.exe" os="Windows 2000" output="dir.txt">


<arg line="/c dir"/>
</exec>

If :

Perform some tasks based on whether a given condition holds true or not.This task is heavily based on
the Condition framework that can be found in Ant 1.4 and later, therefore it cannot be used in
conjunction with versions of Ant prior to 1.4. Due to numeruos bugs in Ant 1.4(.1) that affect this task,
we recommend to use Ant 1.5 or later.

Download ant-contrib.jar to run the below task and define it in your build.xml

Ex:

<if>
<equals arg1="${foo}" arg2="bar" />
<then>
<echo message="The value of property foo is bar" />
</then>
<else>
<echo message="The value of property foo is not bar" />
</else>
</if>

Ssh : Runs a command on a remote machine running SSH daemon

Ex: <sshexec host="somehost"


username="dude"
password="yo"
command="touch somefile"/>

scp : Copies a file or FileSet to or from a (remote) machine running an SSH daemon. FileSet only works
for copying files from the local machine to a remote machine.

Ex: <scp file="myfile.txt" todir="user@somehost:/home/chuck" password="password"/>

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 27


SCM/Build & Release Management 2012
External libraries:
Ant by default provides a list of tasks, apart from them if we need to use any external tasks then we
need to download the respective library and copy it to lib dir of ant installation.

Ant Options:
ant -help

Get help about the usage of Ant.

Ant

Search for a file with the extension .build. If one is found, use it to build its default target.

ant buildfile:project1.build

Build the default target of the build file project1.build.

ant clean

Build the target named clean.

ant -debug

Displays messages that Ant and task developers have flagged as debugging messages.

ant logger filename

Redirects logging output to the specified logger file.

ant logfile filename

Redirects logging output to the specified file.

ant version

Provides the version of ant.

Sample Project demo


Write a build script to check out the code from svn and build , make a war and deploy the same on
tomcat server

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 28


SCM/Build & Release Management 2012

Review yourself:
1) What is ant?

2) How do I get started to use ant? Can you give me a Hello World ant script?

3) How to delete files from a directory if it exists?

5) How does ant read properties? How to set my property system?

6) How to modify properties in ant?

7) How to use ant-contrib tasks?

9) Why do I get an exception when I use location=D:\\Code\\include as attribute of includepath?

10) How can I use ant to run a Java application?

11) How to use ant to run command line command? How to get perl script running result?

12) How to exclude multi directories in copy or delete task?

13) How to do conditional statement in ant?

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 29


SCM/Build & Release Management 2012

Continuous Integration
In software engineering, continuous integration (CI) is the practice of merging all developer workspaces
with a shared mainline several times a day.

Continuous integration the practice of frequently integrating one's new or changed code with the
existing code repository

he practicalities of doing this in a multi-developer environment of rapid commits are such that it's usual
to trigger a short timer after each commit, then to start a build when either this timer expires, or after a
rather longer interval since the last build. Automated tools such
as CruiseControl, Jenkins, Hudson, Bamboo, BuildMaster,AnthillPro or Teamcity offer this scheduling
automatically.

Jenkins:
Jenkins is an award-winning application that monitors executions of repeated jobs, such as building a
software project or jobs run by cron.

Features:

It is a platform independent tool


Can accommodate as many number of build agenets
Open source application
Browser based and easy UI to configure projects
Facilitates all the features of continuous integration

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 30


SCM/Build & Release Management 2012
Jenkins Architecture: It is client server based architecture

Jenkins server component will be installed in server and agents will be installed in clients

Jenkins Installation: Can be installed in two ways

Standalone installation

Tomcat server installation and deploy Jenkins war


JDK installation is mandatory to setup Jenkins.

Build Agent:
Is a light weight client installed in client build machines where actually the code build happens.

Steps to configure build agent:

One you start the Jenkins application click on Manage Jenkins then click on Manage nodes option

Here you can find a link for new node and fill the details with the help of help button beside the every
textbox.

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 31


SCM/Build & Release Management 2012
Project Job Creation:
Click on new Job and select build a free style software project and ok.

It opens a big configuration form to fill, which includes below fields

Assigning build to the build agent/slave


Integrate source code tool (SVN)
Integrate your build scripts (Ant)
Report email Ids for build status

After filling the form click on save button at bottom which creates a build job for you.

Click on build link at left panel which starts the build and runs the scripts which you configured inside
project configuration.

Click on configure link to edit the build job configurations

Security:
Enable security in Jenkins to prevent from unauthorized access by going into Manage jenkins option

Click on configuration and select Enable security option.

You can create the users from Create User link in Manage jenkins

You can provide appropriate access in security tab for the users to edit the build jobs and read only
access etc..

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 32


SCM/Build & Release Management 2012
Plugins:
Jenkins by default provides few features like SVN and Ant integration with build job configurations.

If you wanted to configure other tools and scripting then we can make use of plugins and install then use
it as per your requirements

You can find the plugins in Manage Jenkins .

Internet should be enabled to install the plugins in Jenkins

Deployment Automation
Rapidly and confidently deploy applications without errors to improve business responsiveness.

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 33


SCM/Build & Release Management 2012
More areas of the software construction and release process have been automated. In fact, anybody
building serious software without automating their builds, their tests and without using continuous
integration is not considered to be a professional. A hot topic within the application lifecycle
management (ALM) space is deployment automation. This is driven by middleware environments
getting bigger and more complex, by the increasing number of application releases demanded by
modern businesses, and by the fact that application deployment needs to happen reliably to not disrupt
online services and businesses.

I will compare deployment automation to the most well-known form of automation in the software
development process: build automation. The simplest form of build automation is a shell script that
invokes the compiler, but builds automation systems have matured way beyond that point. Starting with
the previously mentioned Make, and then on to Ant and Maven, build automation tools have provided
more abstract ways for the user to specify his build details.

As such a deployment automation tool needs to satisfy an entirely different set of requirements:

Support for concepts central to deployment: packages, environments, bindings, etc.


Out-of-the-box support for common middleware systems with the possibility to extend the
support.
Out-of-the-box support for common deployment scenarios with the possibility to customize
those scenarios.
Support for different roles: developers deliver software, one group of administrators sets up
environments, another group deploys applications, etc.
Scalable to large environments with many applications and many users.
Cross-platform support: complex middleware environments can span multiple operating
systems and versions.
Existing build automation tools do not address these requirements and extending a build
automation tool to support these requirements, leads to an unfocused product that suits
neither set of requirements well. And that is why deployment automation tools are emerging as
a separate category of products that have outgrown their custom scripting ancestors.

Manual deployments are slow and error prone Deployment of a real world application is a complex and
all-too-often manual process. IT deployment instructions are typically found in multiple documents,
spread across several groups and may be incomplete, requiring tribal knowledge in order to be executed
successfully. Components of the application may need to be deployed in some specific order to any
number of environments, from early testing, user acceptance, and staging, to production, performance,
and disaster recovery. The manual deployment process must be repeated for each environment.
Manually-introduced errors are common and costly.

Another solution for deployment automation is script your requirements . If your server platform is
linux/unix then go ahead with shell scripting.

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 34


SCM/Build & Release Management 2012

Linux basics:
Basic Linux Commands

File Handling
Text Processing
System Administration
Process Management
Archival
Network
File Systems
Advanced Commands Cat

File Handling (contd...)

pwd - print name of current working directory

Usage: pwd

vim Vi Improved, a programmers text editor

Usage: vim [OPTION] [file]...

eg. vim file1.txtMkdir

cp copy files and directories

Usage: cp [OPTION]... SOURCE DEST

eg. cp sample.txt sample_copy.txt

cp sample_copy.txt target_dir

mv move (rename) files

Usage: mv [OPTION]... SOURCE DEST

eg. mv source.txt target_dir

mv old.txt new.txt

rm remove

files or directories

Usage: rm [OPTION]... FILE...

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 35


SCM/Build & Release Management 2012
eg. rm file1.txt , rm rf

some_dir

find search for files in a directory hierarchy

Usage: find [OPTION] [path] [pattern]

eg. find file1.txt, find name

file1.txt

history prints recently used commands

Usage: history

Pattern

A Pattern is an expression that describes a set of

strings which is used to give a concise description

of a set, without having to list all elements.

eg. ab*cd matches anything that starts with ab and

ends with cd etc.

ls *.txt prints all text files

Text Processing
cat concatenate files and print on the standard output

Usage: cat [OPTION] [FILE]...

eg. cat file1.txt file2.txt

cat n

file1.txt

echo display a line of text

Usage: echo [OPTION] [string] ...

eg. echo I love India

echo $HOME

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 36


SCM/Build & Release Management 2012
grep print lines matching a pattern

Usage: grep [OPTION] PATTERN [FILE]...

eg. grep i

apple sample.txt

wc print the number of newlines, words, and bytes in files

Usage: wc [OPTION]... [FILE]...

eg. wc file1.txt

wc L

file1.txt

sort sort lines of text files

Usage: sort [OPTION]... [FILE]...

eg. sort file1.txt

sort -r file1.txt

Linux File Permissions


3 types of file permissions read, write, execute
10 bit format from 'ls l' command

1 234 567 891

Filetype owner group others

eg. drwxrw- rmeans owner has all three permissions, group has read and write, others have
only read permission

read permission 4, write 2, execute 1

eg. rwxrwr=764

673 = rwrwxwx

System Administration
chmod change file access permissions

Usage: chmod [OPTION] [MODE] [FILE]

eg. chmod 744 calculate.sh


Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 37
SCM/Build & Release Management 2012
chown change file owner and group

Usage: chown [OPTION]... OWNER[:[GROUP]] FILE...

eg. chown remo myfile.txt

su change user ID or become superuser

Usage: su [OPTION] [LOGIN]

eg. su remo, su

passwd update a users authentication tokens(s)

Usage: passwd [OPTION]

eg. passwd

who show who is logged on

Usage: who [OPTION]

eg. who , who -b, who -q

Process Management
ps report a snapshot of the current processes

Usage: ps [OPTION]

eg. ps, ps -el

kill to kill a process(using signal mechanism)

Usage: kill [OPTION] pid

eg. kill -9 2275

Archival
tar to archive a file

Usage: tar [OPTION] DEST SOURCE

eg. tar cvf /home/archive.tar /home/original

tar xvf /home/archive.tar

zip package and compress (archive) files

Usage: zip [OPTION] DEST SOURSE

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 38


SCM/Build & Release Management 2012
eg. zip original.zip original

unzip list, test and extract compressed files in a ZIP archive

Usage: unzip filename

eg. unzip original.zip

Network
ssh SSH client (remote login program)

ssh is a program for logging into a remote machine and for

executing commands on a remote machine

Usage: ssh [options] [user]@hostname

eg. ssh X

guest@10.105.11.20

scp secure copy (remote file copy program)

scp copies files between hosts on a network

Usage: scp [options] [[user]@host1:file1] [[user]@host2:file2]

eg. scp file1.txt guest@10.105.11.20:~/Desktop/

File Systems
mount mount a file system

Usage: mount t

type device dir

eg. mount /dev/sda5 /media/target

umount unmount file systems

Usage: umount [OPTIONS] dir | device...

eg. umount /media/target

du estimate file space usage

Usage: du [OPTION]... [FILE]...

eg. du

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 39


SCM/Build & Release Management 2012
df report filesystem disk space usage

Usage: df [OPTION]... [FILE]...

eg. df

quota display disk usage and limits

Usage: quota [OPTION]

eg. quota v

Advanced Commands
reboot reboot the system

Usage: reboot [OPTION]

eg. reboot

poweroff power off the system

Usage: poweroff [OPTION]

eg. Poweroff

sed stream

editor for filtering and transforming text

Usage: sed [OPTION] [inputfile]...

eg. sed 's/love/hate/g' loveletter.txt

awk pattern

scanning and processing language

eg. awk F:

'{ print $1 }' sample_awk.txt

Editor commands
vim Vi Improved, a programmers text editor

Usage: vim [OPTION] [file]...

eg. vi hello.c

Process Management(contd...)

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 40


SCM/Build & Release Management 2012
bg make a foreground process to run in background

Usage: type 'ctrl+z' and then 'bg <job id>'

fg to make background process as foreground process

Usage: fg [jobid]

jobs displays the names and ids of background jobs

Usage: jobs

Exercise 1:
1. After you have logged in, create in your home directory, a practice directory called Exercises.
Remember that UNIX is case sensitive!
$ mkdir exercises

2. Change into that directory. Create several other directories called Question1, Question2, .... Bearing
in mind that leaving a space in a directory name is possible, but undesirable, and also that UNIX is
case-sensitive. $ mkdir question1
$ mkdir question2

$ mkdir question3

3. Create a .txt as name-profile.txt file using vi. Write a small essay about yourself in the file regarding
your background, hobbies and professional goals etc. Have at least two paragraphs and each
paragraph having at least 10 lines. $ vi name-profole.txt
4. Make a second copy of the file in the directory called Question2
$ cp name-profile.txt question2/details

5. Change the name of the file in the Question2 directory to 2nd-name-profile.txt


$ mv name-profile.txt 2name-profile.txt

6. Using the chmod command, change the permissions on the file in the Question1 directory (and the
directory itself) so that anyone logged into Ranger can read the file (but not alter it). The
permissions for the directory will read: rwxr-xr-x and the file permissions will read rw-r--r-- when
you are finished with this. You may also need to change the permissions on files and directories
above the Question1 directory to accomplish this. You will almost certainly need to read the man
page for chmod in order to do this. $ chmod 744 quant

Exercise 2:

1. Check which directory you are currently in. If necessary, move to your home directory.
(Remember: cd will do this from anywhere).pwd
2. Move to the root directory. ("Move to..." means "change your current working directory to...". It
is useful to picture the process as movement around the tree structure.) cd

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 41


SCM/Build & Release Management 2012
3. Work your way down one directory at a time to your home directory. cd
4. Experiment with using relative and absolute pathnames; show how the two can produce the
same results. $ Cd test/har/put, ( $ cd test

$ cd har

$ cd put)

5. Explore your systems filestore. Try to get into the home directory of someone else yo know!
(You may not be able to view their files.) cd .. ls l
6. Display your current working directory using the pwd command.pwd
7. Make a directory called exercises. Mkdir exercises
8. Change your directory to the directory exercises. Display the current working directory. Cd
exercises
9. Return to your home directory. Cd
10. List the contents of your directory. Use the -l, -a and -F options and compare the output. Ls l,
ls a ls -F
11. Change your directory to the directory exercises. Create a file called example1 using the cat
command containing the following text:
12. water, water everywhere
13. and all the boards did shrink;
14. water, water everywhere,
15. Nor drop to drink $ mkdir exercises $ cd >example1
16. List the contents of your directory. Use the -l option to obtain a long listing. Ls -l
17. Display a list of logged on users. who
18. Obtain further information for a particular user using the finger command.

$ finger username

19. Use the man command to obtain further information on the finger command.

$ man finger

20. Use the man -k command to find what manual entries there are related to passwords. $ man k
passwd
21. Use the grep command to search the file example1 for occurrences of the string 'water'. $ grep
water example1
22. Use man and the keyword option to find out more information on communications and e-mail in
Unix. $ man mail
23. Print out a file on paper. $ lpr example1
24. Send a message to another user on your Unix system, and get them to reply.

$ talk username

25. Create a small text file and send it to another user.

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 42


SCM/Build & Release Management 2012
26. When you receive a message, save it to a file other than your mailbox. (Remember you can
always send yourself a message if you don't have one.)
27. Send a message to a user on a different computer system. $ write username
28. Send a note to your course tutor telling him that you can use mail now.
29. Try to move to the home directory of someone else in your group. There are several ways to do
this, and you may find that you are not permitted to enter certain directories. See what files
they have, and what the file permissions are. (Remember that you can protect your own files
from prying eyes, or from interference.)
30. Try to copy a file from another user's directory to your own.
31. Set permissions on all of your files and directories to those that you want. You may want to give
read permission on some of your files and directories to members of your group. $ chmod g+r
example1
32. Put a listing of the files in your directory into a file called filelist. (Then delete it!)$ ls | cat >filelist
33. Create a text file containing a short story, then use the spell program to check the spelling of the
words in the file. $ spell filename
34. Redirect the output of the spell program to a file called errors.

$ spell filename| cat >errors

35. Type the command ls -l and examine the format of the output. Pipe the output of the command
ls -l to the word count program wc to obtain a count of the number of files in your directory. 3.
Use cut to strip away the reference material and leave just the text field. $ ls l $ ls l | wc
l $ ls l | tr-s | cut d f 9
36. Use tr to strip away any tags that are actually in the text (e.g. attached to the words), so that
you are left with just the words. $ tr d % $ tr d @ < filename >> filename
37. Create a file containing some lines that you think would match the regular expression: (^[0-
9]{1,5}[a-zA-z ]+$)|none and some lines that you think would not match. Use egrep to see if
your intuition is correct. $ egrep ! filename

$ grep E ! filename

38. Type umask 000 and then create a file called world.txt containing the words "hello world". Look
at the permissions on the file. What's happened? Now type umask 022 and create a file called
world2.txt. When might this feature be useful?

$ umask 000 $ vi world.txt $ ls l $ umask 022 $ ls l

39. Create a file called "hello.txt" in your home directory using the command cat -u
> hello.txt. Ask your partner to change into your home directory and run tail -f hello.txt.
Now type several lines into hello.txt. What appears on your partner's screen? $ cat u >
hello.txt
40. Use find to display the names of all files in the /home subdirectory tree. Can you do this without
displaying errors for files you can't read? $ find exercises
41. What does the cut command do? Can you use it together with w to produce a list of login names
and CPU times corresponding to each active process? $ w | cut d
42. Use ps, w and top to show all processes that are executing. $ ps $ w $top
43. Type mount (with no parameters) and try to interpret the output. $ mount

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 43


SCM/Build & Release Management 2012
44. Startup a number of sleep 60 processes in the background, and terminate them all at
the same time using the pkill command. $ ps f

sleep number & $ pkill P processid

Use ps -aeH to display the process hierarchy. Look for the init process. See if you can identify important
system daemons. Can you also identify your shell and its subprocesses? $ ps aeH

Shell Scripting:
Introduction

Unix uses shells to accept commands given by the user, there are quite a few different shells available.
The most commonly used shells are SH(Bourne SHell) CSH(C SHell) and KSH(Korn SHell), most of the
other shells you encounter will be variants of these shells and will share the same syntax, KSH is based
on SH and so is BASH(Bourne again shell). TCSH(Extended C SHell) is based on CSH.

The various shells all have built in functions which allow for the creation of shell scripts, that is, the
stringing together of shell commands and constructs to automate what can be automated in order to
make life easier for the user.

With all these different shells available, what shell should we script in? That is debatable. For the
purpose of this tutorial we will be using SH because it is practically guaranteed to be available on most
Unix systems you will encounter or be supported by the SH based shells. Your default shell may not be
SH. Fortunately we do not have to be using a specific shell in order to exploit its features because we can
specify the shell we want to interpret our shell script within the script itself by including the following in
the first line.

#!/path/to/shell

Usually anything following (#) is interpreted as a comment and ignored but if it occurs on the first line
with a (!) following it is treated as being special and the filename following the (!) is considered to point
to the location of the shell that should interpret the script.

When a script is "executed" it is being interpreted by an invocation of the shell that is running it. Hence
the shell is said to be running non-interactively, when the shell is used "normally" it is said to be running
interactively.

Note

There are many variations on the basic commands and extra information which is too specific to
be mentioned in this short tutorial, you should read the man page for your shell to get a more
comprehensive idea of the options available to you. This tutorial will concentrate on highlighting
Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 44
SCM/Build & Release Management 2012
the most often used and useful commands and constructs.

Shell Scripting Basics

Command Redirection and Pipelines

By default a normal command accepts input from standard input, which we abbreviate to stdin,
standard input is the command line in the form of arguments passed to the command. By default a
normal command directs its output to standard output, which we abbreviate to stdout, standard output
is usually the console display. For some commands this may be the desired action but other times we
may wish to get our input for a command from somewhere other than stdin and direct our output to
somewhere other than stdout. This is done by redirection:

We use > to redirect stdout to a file, for instance, if we wanted to redirect a directory listing
generated by the ls we could do the following:

ls > file

We use < to specify that we want the command immediately before the redirection symbol to
get its input from the source specified immediately after the symbol, for instance, we could
redirect the input togrep(which searches for strings within files) so that it comes from a file like
this:

grep searchterm < file

We use >> to append stdout to a file, for instance, if we wanted to append the date to the end
of a file we could redirect the output from date like so:

date >> file

One can redirect standard error (stderr) to a file by using 2>, if we wanted to redirect the
standard error from commandA to a file we would use:

commmandA 2>

Pipelines are another form of redirection that are used to chain commands so that powerful composite
commands can be constructed, the pipe symbol '|' takes the stdout from the command preceding it and
redirects it to the command following it:

ls -l | grep searchword | sort -r

The example above firsts requests a long (-l directory listing of the current directory using
the ls command, the output from this is then piped to grep which filters out all the listings containing

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 45


SCM/Build & Release Management 2012
the searchword and then finally pipes this through to sort which then sorts the output in reverse (-
r, sort then passes the output on normally to stdout.

Variables

When a script starts all environment variables are turned into shell variables. New variables can be
instantiated like this:

name=value

You must do it exactly like that, with no spaces either side of the equals sign, the name must only be
made up of alphabetic characters, numeric characters and underscores, it cannot begin with a numeric
character. You should avoid using keywords like for or anything like that, the interpreter will let you use
them but doing so can lead to obfuscated code ;)

Variables are referenced like this: $name, here is an example:

#!/bin/sh
msg1=Hello
msg2=There!
echo $msg1 $msg2

This would echo "Hello There!" to the console display, if you want to assign a string to a variable and the
string contains spaces you should enclose the string in double quotes ("), the double quotes tell the shell
to take the contents literally and ignore keywords, however, a few keywords are still processed. You can
still use $ within a (") quoted string to include variables:

#!/bin/sh
msg1="one"
msg2="$msg1 two"
msg3="$msg2 three"
echo $msg3

Would echo "one two three" to the screen. The escape character can also be used within a double
quoted section to output special characters, the escape character is "\", it outputs the character
immediately following it literally so \\ would output \. A special case is when the escape character is
followed by a newline, the shell ignores the newline character which allows the spreading of long
commands that must be executed on a single line in reality over multiple lines within the script. The
escape character can be used anywhere else too. Except within single quotes.

Surrounding anything within single quotes causes it to be treated as literal text that is it will be passed
on exactly as intended, this can be useful for sending command sequences to other files in order to
create new scripts because the text between the single quotes will remain untouched. For example:

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 46


SCM/Build & Release Management 2012
#!/bin/sh
echo 'msg="Hello World!"' > hello
echo 'echo $msg' >> hello
chmod 700 hello
./hello

This would cause "msg="Hello World!" to be echoed and redirected to the file hello, "echo $msg" would
then be echoed and redirected to the file hello but this time appended to the end. The chmod line
changes the file permissions of hello so that we can execute it. The final line executes hello causing it
output "Hello World". If we had not used literal quotes we never would have had to use escape
characters to ensure that ($) and (") were echoed to the file, this makes the code a little clearer.

A variable may be referenced like so ${VARIABLENAME}, this allows one to place characters immediately
preceding the variable like ${VARIABLENAME}aaa without the shell interpreting aaa as being part of the
variable name.

Command Line Arguments

Command line arguments are treated as special variables within the script, the reason I am calling them
variables is because they can be changed with the shift command. The command line arguments are
enumerated in the following manner $0, $1, $2, $3, $4, $5, $6, $7, $8 and $9. $0 is special in that it
corresponds to the name of the script itself. $1 is the first argument, $2 is the second argument and so
on. To reference after the ninth argument you must enclose the number in brackets like this ${nn}. You
can use the shift command to shift the arguments 1 variable to the left so
that $2 becomes $1, $1 becomes $0 and so on, $0 gets scrapped because it has nowhere to go, this can
be useful to process all the arguments using a loop, using one variable to reference the first argument
and shifting until you have exhausted the arguments list.

As well as the commandline arguments there are some special builtin variables:

$# represents the parameter count. Useful for controlling loop constructs that need to process
each parameter.
$@ expands to all the parameters separated by spaces. Useful for passing all the parameters to
some other function or program.
$- expands to the flags(options) the shell was invoked with. Useful for controlling program flow
based on the flags set.
$$ expands to the process id of the shell innovated to run the script. Useful for creating unique
temporary filenames relative to this instantiation of the script.

Note

The commandline arguments will be referred to as parameters from now on, this is because SH
also allows the definition of functions which can take parameters and when called the $n family
will be redefined, hence these variables are always parameters, its just that in the case of the
parent script the parameters are passed via the command line. One exception is $0 which is

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 47


SCM/Build & Release Management 2012
always set to the name of the parent script regardless of whether it is inside a function or not.

Command Substitution

In the words of the SH manual Command substitution allows the output of a command to be
substituted in place of the command name itself. There are two ways this can be done. The first is to
enclose the command like this:

$(command)

The second is to enclose the command in back quotes like this:

`command`

The command will be executed in a sub-shell environment and the standard output of the shell will
replace the command substitution when the command completes.

Arithmetic Expansion

Arithmetic expansion is also allowed and comes in the form:

$((expression))

The value of the expression will replace the substitution. Eg:

!#/bin/sh
echo $((1 + 3 + 4))

Will echo "8" to stdout

Control Constructs

The flow of control within SH scripts is done via four main


constructs; if...then...elif..else, do...while, for and case.

If..Then..Elif..Else

This construct takes the following generic form, The parts enclosed within ([) and (]) are optional:

if list

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 48


SCM/Build & Release Management 2012
then list
[elif list
then list] ...
[else list]
fi

When a Unix command exits it exits with what is known as an exit status, this indicates to anyone who
wants to know the degree of success the command had in performing whatever task it was supposed to
do, usually when a command executes without error it terminates with an exit status of zero. An exit
status of some other value would indicate that some error had occurred, the details of which would be
specific to the command. The commands' manual pages detail the exit status messages that they
produce.

A list is defined in the SH as "a sequence of zero or more commands separated by newlines, semicolons,
or ampersands, and optionally terminated by one of these three characters.", hence in the generic
definition of the ifabove the list will determine which of the execution paths the script takes. For
example, there is a command called test on Unix which evaluates an expression and if it evaluates to
true will return zero and will return one otherwise, this is how we can test conditions in the list part(s) of
the if construct because test is a command.

We do not actually have to type the test command directly into the list to use it, it can be implied by
encasing the test case within ([) and (]) characters, as illustrated by the following (silly) example:

#!/bin/sh
if [ "$1" = "1" ]
then
echo "The first choice is nice"
elif [ "$1" = "2" ]
then
echo "The second choice is just as nice"
elif [ "$1" = "3" ]
then
echo "The third choice is excellent"
else
echo "I see you were wise enough not to choose"
echo "You win"
fi

What this example does is compare the first parameter (command line argument in this case) with the
strings "1", "2" and "3" using tests' (=) test which compares two strings for equality, if any of them
match it prints out the corresponding message. If none of them match it prints out the final case. OK the
example is silly and actually flawed (the user still wins even if they type in (4) or something) but it
illustrates how the if statement works.

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 49


SCM/Build & Release Management 2012
Notice that there are spaces between (if) and ([), ([) and the test and the test and (]), these spaces must
be present otherwise the shell will complain. There must also be spaces between the operator and
operands of the test otherwise it will not work properly. Notice how it starts with (if) and ends with (fi),
also, notice how (then) is on a separate line to the test above it and that (else) does not require a (then)
statement. You must construct this construct exactly like this for it to work properly.

It is also possible to integrate logical AND and OR into the testing, by using two tests separated by either
"&&" or "||" respectively. For example we could replace the third test case in the example above with:

elif [ "$1" = "3"] || [ "$1" = "4" ]


then echo "The third choi...

The script would print out "The third choice is excellent" if the first parameter was either "3" OR "4". To
illustrate the use of "&&" we could replace the third test case with:

elif [ "$1" = "3"] || [ "$2" = "4" ]


then echo "The third choi...

The script would print out "The third choice is excellent" if and only if the first parameter was
"3" AND the second parameter was "4".

"&&" and "||" are both lazily evaluating which means that in the case of "&&", if the first test fails it
wont bother evaluating the second because the list will only be true if they BOTH pass and since one has
already failed there is no point wasting time evaluating the second. In the case of "||" if the first test
passes it wont bother evaluating the second test because we only need ONE of the tests to pass for the
whole list to pass. See the test manual page for the list of tests possible (other than the string equality
test mentioned here).

Do...While

The Do...While takes the following generic form:

while list
do list
done

In the words of the SH manual "The two lists are executed repeatedly while the exit status of the first list
is zero." there is a variation on this that uses until in place of while which executes until the exit status of
the first list is zero. Here is an example use of the while statement:

#!/bin/sh
count=$1 # Initialise count to first parameter

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 50


SCM/Build & Release Management 2012
while [ $count -gt 0 ] # while count is greater than 10 do
do
echo $count seconds till supper time!
count=$(expr $count -1) # decrement count by 1
sleep 1 # sleep for a second using the Unix sleep command
done
echo Supper time!!, YEAH!! # were finished

If called from the commandline with an argument of 4 this script will output

4 seconds till supper time!


3 seconds till supper time!
2 seconds till supper time!
1 seconds till supper time!
Supper time!!, YEAH!!

You can see that this time we have used the -gt of the test command implicitly called via '[' and ']', which
stands for greater than. Pay careful attention to the formatting and spacing.

For

The syntax of the for command is:

for variable in word ...


do list
done

The SH manual states The words are expanded, and then the list is executed repeatedly with the
variable set to each word in turn.. A word is essentially some other variable that contains a list of values
of some sort, thefor construct assigns each of the values in the word to variable and then variable can
be used within the body of the construct, upon completion of the body variable will be assigned the next
value in word until there are no more values in word. An example should make this clearer:

#!/bin/sh
fruitlist="Apple Pear Tomato Peach Grape"
for fruit in $fruitlist
do
if [ "$fruit" = "Tomato" ] || [ "$fruit" = "Peach" ]
then
echo "I like ${fruit}es"
else
echo "I like ${fruit}s"

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 51


SCM/Build & Release Management 2012
fi
done

In this example, fruitlist is word, fruit is variable and the body of the statement outputs how much this
person loves various fruits but includes an if...then..else statement to deal with the correct addition of
letters to describe the plural version of the fruit, notice that the variable fruit was expressed
like ${fruit} because otherwise the shell would have interpreted the preceding letter(s) as being part of
the variable and echoed nothing because we have not defined the variables fruits and fruites When
executed this script will output:

I like Apples
I like Pears
I like Tomatoes
I like Peachs
I like Grapes

Within the for construct, do and done may be replaced by '{' and '}'. This is not allowed for while.

Case

The case construct has the following syntax:

case word in
pattern) list ;;
...

esac

An example of this should make things clearer:

!#/bin/sh
case $1
in
1) echo 'First Choice';;
2) echo 'Second Choice';;
*) echo 'Other Choice';;
esac

"1", "2" and "*" are patterns, word is compared to each pattern and if a match is found the body of the
corresponding pattern is executed, we have used "*" to represent everything, since this is checked last
we will still catch "1" and "2" because they are checked first. In our example word is "$1", the first

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 52


SCM/Build & Release Management 2012
parameter, hence if the script is ran with the argument "1" it will output "First Choice", "2" "Second
Choice" and anything else "Other Choice". In this example we compared against numbers (essentially
still a string comparison however) but the pattern can be more complex, see the SH man page for more
information.

Functions

The syntax of an SH function is defined as follows:

name ( ) command

It is usually laid out like this:

name() {
commands
}

A function will return with a default exit status of zero, one can return different exit status' by using the
notation return exit status. Variables can be defined locally within a function using local name=value.
The example below shows the use of a user defined increment function:

Increment Function Example

#!/bin/sh

inc() { # The increment is defined first so we can use it


echo $(($1 + $2)) # We echo the result of the first parameter plus the second
parameter
}

# We check to see that all the command line arguments are present
if [ "$1" "" ] || [ "$2" = "" ] || [ "$3" = "" ]
then
echo USAGE:
echo " counter startvalue incrementvalue endvalue"
else
count=$1 # Rename are variables with clearer names
value=$2
end=$3
while [ $count -lt $end ] # Loop while count is less than end
do
echo $count

count=$(inc $count $value) # Call increment with count and value as parameters

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 53


SCM/Build & Release Management 2012
done # so that count is incremented by value
fi

inc() {
echo $(($1 + $2))
}

The function is defined and opened with inc() {, the line echo $(($1 + $2)) uses the
notation for arithmetic expression substitution which is $((expression)) to enclose the
expression, $1 + $2 which adds the first and second parameters passed to the function
together, the echo bit at the start echoes them to standard output, we can catch this
value by assigning the function call to a variable, as is illustrated by the function call.
count=$(inc $count $value)

We use command substitution which substitutes the value of a command to substitute


the value of the function call whereupon it is assigned to the count variable. The
command within the command substitution block is inc $count $value, the last two
values being its parameters. Which are then referenced from within the function
using $1 and $2. We could have used the other command substitution notation to call
the function if we had wanted:

count=`inc $count $value`

We will show another quick example to illustrate the scope of variables:

Variable Scope, Example

#!/bin/sh
inc() {

local value=4
echo "value is $value within the function\\n"
echo "\\b\$1 is $1 within the function"
}

value=5
echo value is $value before the function
echo "\$1 is $1 before the function"
echo

echo -e $(inc $value)


echo

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 54


SCM/Build & Release Management 2012
echo value is $value after the function
echo "\$1 is $1 after the function"

inc() {
local value=4
echo "value is $value within the function\\n"
echo "\\b\$1 is $1 within the function"
}

We assign a local value to the variable value of 4. The next three lines construct the the
output we would like, remember that this is being echoed to some buffer and will be
replace the function call with all the stuff that was passed to stdout within the function
when the function exits. So the calling code will be replaced with whatever we direct to
standard output within the function. The function is called like this:

echo -e $(inc $value)

We have passed the option -e to the echo command which causes it to process C-style
backslash escape characters, so we can process any backslash escape characters which
the string generated by the function call contains.

If we just echo the lines we want to be returned by the function it will not pass the
newline character onto the buffer even if we explicitly include it with an escape
character reference so what we do is actually include the sequence of characters that
will produce a new line within the string so that when it is echoed by the calling code
with the -e the escape characters will be processed and the newlines will be placed
where we want them.

echo "value is $value within the function\\n"

Notice how the newline has been inserted with \\n, the first two backslashes indicate
that we want to echo a backslash because within double quotes a backslash indicates to
process the next character literally, we have to do this because we are only between
double quotes and not the literal-text single quotes. If we had used single quotes we
would had have to echo the bit with the newline in separately from the bit that
contains $value otherwise $value would not be expanded.

echo "\\b\$1 is $1 within the function"

This is our second line, and is contained within double quotes so that the
variable $1 will be expanded, \\b is included so that \b will be placed in the echoed line

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 55


SCM/Build & Release Management 2012
and our calling code will process this as a backspace character. We have to do this
because for some reason the shell prefixes a space to the second line if we do not, the
backspace removes this space.

The output from this script called with 2 as the first argument is:

value is 5 before the function


$1 is 2 before the function

value is 4 within the function


$1 is 5 within the function

value is 5 after the function


$1 is 2 after the function

Tip

You can use ". DIRECTORY/common.sh" to import functions from a script called common.sh in
DIRECTORY, a quick example is shown below, first is test.sh:

#!/bin/sh
. ./common.sh
if [ "$1" = "" ]; then
echo USAGE:
echo "sh test.sh type"
exit
fi

if `validtype $1`; then


echo Valid type
else
echo Invalid type
fi

Here is common.sh:

#!/bin/sh
validtype() {
if [ "$1" = "TYPEA" ] ||
[ "$1" = "TYPEB" ] ||
[ "$1" = "TYPEC" ] ||
[ "$1" = "TYPED" ] ||
[ "$1" = "TYPEE" ];

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 56


SCM/Build & Release Management 2012

then
exit 0
else
exit 1
fi
}

If

Exercise:
1. Write a shell script to find the sum of two numbers
2. Write a shell script to find the factorial of a given number.
3. Write a shell script to find whether two given strings are equal or not.
4. Write a shell script to accept a string which is less than 10 characters. If it is more than 10 reaccept
the string.
5. Write a shell script to remove blank lines from a file.
6. Write a Shell Script and accept a file name, find out whether the accepted entry is file or directory. If
it is a file display the contents. If it is a directory, use case structure to long listing files, creating and
changing the permissions of a specified file.
7. Display all the users information in a desired format. Accept a login name and display login name,
UID, GID, Current shell, Home directory.
8. Accept book information like book id, author name and date of publication. Write a shell script to
remove a particular info from database.
9. Write a shell script to find out the files accessed in last ten minutes and file modified in last ten
minutes.
10. Write a shell script to locate all lines longer than 15 characters in a file.

Introducing Jenkins

Introduction

Continuous Integration Fundamentals

Introducing Jenkins (originally Hudson)

From Hudson to JenkinsA Short History

Should I Use Jenkins or Hudson?

Introducing Continuous Integration into Your Organization

Where to Now?
Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 57
SCM/Build & Release Management 2012
Your First Steps with Jenkins

Introduction

Preparing Your Environment

Starting Up Jenkins

Configuring the Tools

Your First Jenkins Build Job

Your First Build Job in Action

More ReportingDisplaying Javadocs

Adding Code Coverage and Other Metrics

Conclusion

Installing Jenkins

Introduction

Downloading and Installing Jenkins

Preparing a Build Server for Jenkins

The Jenkins Home Directory

Running Jenkins as a Stand-Alone Application

Running Jenkins Behind an Apache Server

Running Jenkins on an Application Server

Memory Considerations

Installing Jenkins as a Windows Service

Whats in the Jenkins Home Directory

Backing Up Your Jenkins Data

Upgrading Your Jenkins Installation

Conclusion

Configuring Your Jenkins Server

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 58


SCM/Build & Release Management 2012
Introduction

The Configuration DashboardThe Manage Jenkins Screen

Configuring the System Environment

Configuring Global Properties

Configuring Your JDKs

Configuring Your Build Tools

Configuring Your Version Control Tools

Configuring the Mail Server

Configuring a Proxy

Conclusion

Setting Up Your Build Jobs

Introduction

Jenkins Build Jobs

Creating a Freestyle Build Job

Configuring Source Code Management

Build Triggers

Build Steps

Post-Build Actions

Running Your New Build Job

Using Jenkins with Other Languages

Conclusion

Automated Testing

Introduction

Automating Your Unit and Integration Tests

Configuring Test Reports in Jenkins

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 59


SCM/Build & Release Management 2012
Displaying Test Results

Ignoring Tests

Automated Acceptance Tests

Conclusion

More ReportingDisplaying Javadocs

Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 60

You might also like