Sometimes I’m asked how I manage my own (PowerShell) code, in terms of version control, backups, portability etc. In this presentation I demonstrated how my PowerShell code is typically broken down into functions and then placed into modules. This allows me to make very generic code for granular tasks, typically either to plug a gap missing from the out-of-the-box cmdlets or maybe stringing a few of them together. As a consultant this enables me to build up a toolkit of functions for particular scenarios gained over various different experiences and use them in a modular fashion where needed for each particular project. However, once these number in the hundreds how do you manage them effectively? I need them to:
- be easily available depending on where I am working
- be backed up
- track changes via version control, useful even if you are not working in a team developing code together – mostly so I can remember how or why I changed something
So I’m going to run your through the system I have found that works for me. It uses the following components:
- A Dropbox account to sync the code between different machines, be available to download via a web browser and also store the code outside of my home lab. This means I can get access to my functions pretty much wherever I am and whether I am using my own or a customer machine.
- A Linux VM in my homelab to run Git and Atlassian Stash for version control – $10 for a 10 user license (free to try out for 30 days)
- Atlassian SourceTree. Free Git client for Mac or Windows
Stash I discovered Stash via a previous customer and found it to be a very useful and easy to use add-on to manage Git repositories. It’s possibly overkill for my current needs, but I have worked on shared code projects in the past and it could be useful to have an easy way to do this in the future. If you are thinking “why not use GitHub?”, there are reasons for this. While I share a lot of my code via this blog and possibly via GitHub in the future, there are some commercial and other reasons why I’m not able to share everything. GitHub has private repositories for this, but they start at $7 a month, so this approach with a home hosted Stash does me fine for now. Setting Up Stash I have a 1 vCPU, 1GB Ubuntu VM, installed with the Minimal Server option. The first thing to do is install Git. The below command will install Git and any dependencies.
sudo apt-get install git-core
Check the versions of Git and Perl. The version of Git should be 1.7.6 or higher. The version of Perl should be 5.8.8 or higher. (Note that out of the box RedHat / Centos currently do not include a version of Git that supports Stash.)
git --version perl --version
Check the version of Java and install if necessary. The version of Java should be 1.6.0 or higher. The below is the easiest way I found to do this.
java -version sudo apt-get purge openjdk* sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java7-installer java -version
sudo mkdir /var/stash sudo mkdir /var/stash/install sudo mkdir /var/stash/home cd /var/stash/install sudo wget http://www.atlassian.com/software/stash/downloads/binary/atlassian-stash-2.11.4.tar.gz sudo tar zxf atlassian-stash-2.11.4.tar.gz
Create a user account to run Stash under.
sudo /usr/sbin/useradd --create-home --home-dir /usr/local/stash --shell /bin/bash stash
Set STASH_HOME in setenv.sh to /var/stash/home.
sudo vi atlassian-stash-2.11.4/bin/setenv.sh
sudo chown -R stash /var/stash
Navigate to the Setup Wizard at http://hostname:7990 Take the option for an Internal database Enter your license key Enter Administrator credentials and Go to Stash (I am not using the accompanying Jira product) This will take you to the login page where you can authenticate with the credentials you just created. You should now see the Stash Welcome Page. Connecting the SourceTree Client to Stash Download the SourceTree Client for Mac or Windows and get it installed. Once installed, fire up the client. You may be prompted to install Git for your local client to issue Git commands, go with the embedded version if so: Say No to Mercurial (unless you wish to use that as well) Enter your information – if you have a GitHub account and wish to use repositories there it can be useful to add the associated email account here. Accept the recommendation to use Putty If you want to use an SSH key then enter that here, I’m not going to for this tutorial. Enter your credentials for the Stash website (and others if you wish to) Once complete, the SourceTree client will open. They are pretty regular at providing updates for it, so you may be prompted to install further updates before first use. Now we need to create a project and a repository. First of all through the web interface to Stash create a relevant project. (I liked the way it shortened this one to POW – reminds me of the old Batman series) Now create a repository Further down this page you will find instructions on how to upload existing code, which in my case is what I want to do. From the SourceTree client open up a Git terminal and run the above commands: At this point if you navigate back to your Stash website you should see your files have been uploaded. You can view inside the code too via the webbrowser. Now I need to configure the SourceTree client to be aware of this repository. Click the button for Clone / New and choose Add Working Copy. Enter the path to your working folder and Add. It is now available in the SourceTree client. So now when I either:
- Edit code directly on my laptop
- Or copy code edited elsewhere and pop it in the Dropbox folder
Following synchronisation with the Dropbox folder, the SourceTree client will show that files have changed and require committing. In the below example a couple of minor spelling mistakes have been corrected. SourceTree shows the file has been updated and also what has been changed since the previous version. Red line was removed, Green line was added. Right-click the file and choose Commit. Enter an explanation for what was changed in this version and it also makes sense to select Push commits immediately to origin so that you create the commit and also submit it in the same action. Assuming there were no errors then SourceTree should display no files to commit and navigating to the Log / History tab you will notice that a history of your changes will start to build up. Again, useful if you need to track back and see what changed and when.