Push Code to GIT and test with JenkinsApril 25, 2016
in previous posts we discussed how you can use Jenkins to test various pieces of code including Powershell. Jenkins is a neat way to test your code and have a log of the successes and failures but let’s face it, you were probably testing your code as you were writing it anyway right? Well, what if you could push your code to GIT and have that code tested each time a GIT push was executed? Then you can have several people working on the same code and when the code gets updated in your repositories, it will be tested and logged. This makes it really nice to see when the code stopped working and who published the code to GIT. Now we’re really starting to see the power of this CI/CD stuff.
Before we begin, you must make sure of a couple of things. First, ensure that you have the GIT plugin for Jenkins installed on your server. You’ll need this plugin to configure the additional settings for your project. Go to “Manage Jenkins” and then “Manage Plugins” to ensure your plugins are installed.
Second, any nodes that will run your code, will first need to download the code from GIT, so make sure that the GIT client is installed on them, and that they have access to your GIT repo as well.
Build a GIT Project
Now go and setup a new Jenkins project and give it a name and description like we’ve done in the past. Scroll down until you reach the “Source Code Management” section. (If you’re missing this section, make sure your plugins are installed.) Here, select Git and then enter the repository that will store your code. You’ll also need to add some credentials that will be used to connect to the repo. You can enter a username password, certificate, or SSH keys. Below that you’ll need to select which branch will be built from. I’ve chosen to build from master.
Next, scroll down until you see the build triggers. Here select “Poll SCM” and then enter a schedule. In my example below I’m checking for updated builds every two minutes. Don’t worry, if there are no new commits, the job won’t run anything. Also, I want to point out here that it is possible to setup a web hook from your Git repo to trigger a Jenkins build every time code is pushed into the repository. I’m doing this with Bitbucket in my lab, but it can be accomplished with Github Enterprise and many others as well.
Next, enter the code that should be run after the Git clone is executed on your node. In my case I’m executing a powershell script that calls my Host_Settings PowerCLI script.
Push Code to Git!
That’s it, you’ve setup your Jenkins job and it will run every time you push your code into your Git repository. Pretty neat huh? It’s really nice to be able to pull code down from a repo, update it, push it back to the repo and have it automatically tested. Maybe you go a few steps further and have Jenkins email your team on failed builds. Even better, maybe you test code in your branches and have Jenkins push it to your Master branch when it completes a successful test! Happy coding!