During a recent project I needed to modify user interface (UI) files in a Subversion repository and then deploy them to a Tomcat server.  At first I recursively copied all of the files using a batch script, but because of the size of the repository this could take anywhere from 10 to 15 seconds.  That is long enough to interrupt cognitive flow and consequently decrease programmer productivity.  Because of this I decided to try copying only the locally-modified files to the Tomcat deployment folder.  I did this using a PowerShell script that uses the ‘svn status’ command to obtain a list of all repository files with local changes.  Due to the fact that I am only modifying a small number of UI files at a time, this script copies all changed files to the deployment folder in less than one second, a significant improvement over the “brute force” approach.

The PowerShell script that I created is provided below.  In order to use it you will need to change the values assigned to $REPOSITORY_LOCATION and $DEPLOYMENT_LOCATION.  You will also need to enable PowerShell script execution on your machine.  You can enable this by opening PowerShell and typing “set-executionpolicy remotesigned”.

I hope that you find this script useful.  Please feel free to contact me if you have any questions.


# Selective Deployment PowerShell Script
#
# This script copies locally-modified files in a Subversion repository to a
# specified deployment folder. Note that this script assumes that the
# deployment folder and all subdirectories exist.

# Set the repository location and the location to which to deploy the modified repository files.

$REPOSITORY_LOCATION=”C:pathtorepositorydirectory”
$DEPLOYMENT_LOCATION=”C:pathtodeploymentdirectory”

# Use ‘svn status’ to list all locally modified files in the specified repository.
# Copy each file to the correct folder in the deployment directory.
(svn status $REPOSITORY_LOCATION -q) | ForEach {
# Extract the filename from the line.
$FILENAME=[regex]::split($_, “Ms”)[1].Trim()
Write-Host $FILENAME

# Extract the location within the repository from the string.
$REPOSITORY_LOCATION_SUBSTRING=$FILENAME.substring($REPOSITORY_LOCATION.get_Length())

# Append this location to the end of the deployment location path. Remove
# the filename to get the directory to which we will copy the original file.
$COPY_TO_FILE=(Join-Path $DEPLOYMENT_LOCATION $REPOSITORY_LOCATION_SUBSTRING)
$COPY_TO_DIRECTORY=$COPY_TO_FILE.substring(0,$COPY_TO_FILE.LastIndexOf(“”))

# Copy the original file to the correct subdirectory in the deployment folder.
Copy-Item $FILENAME $COPY_TO_DIRECTORY
}

3 thoughts to “Selectively Deploying Files from a Subversion Repository

  • Avatar
    Daniel

    Hello jonathan I see the script you coded to deploy files from subversion, can it deploy the files out of subversion to a target server location? Does the script work in windows and unix? what platform do we need to run the script ? java?

    Where does the script need to be installed? in the same server where subversion is installed?

    Thank you much for your help

    Daniel

    Reply
  • Avatar
    Daniel

    Hi jonathan does your script deploy to target servers locations? or just locally on the same server where subversion lives?

    is your script open source to be used freely?

    Reply
    • Jonathan Kauffman
      Jonathan Kauffman

      Hi Daniel,

      (Responding to this comment and the one that you posted about an hour before this one.)

      This is a PowerShell script that I used in a Windows environment. It has been several years since I last ran the script, but from what I remember you only need to have Subversion and PowerShell installed to use it.

      If you wanted to do this in a Unix environment it would probably be best to re-write this script using a scripting language like Bash.

      The script is designed to work when both the source and target locations are on the same machine, but a remote deploy is technically possible, you would just need to use some sort of remote copy tool or command instead of the Copy-File command that I used.

      You are welcome to use the script, but please provide proper attribution when you do so.

      Hopefully this answers your questions — let me know if you have others.

      Jonathan

      Reply

Leave a comment

Your email address will not be published. Required fields are marked *

X