1

I am new to Jenkins and I want to configure a job that can continuously build and deploy to three different environments. I have three branches in my git repo(dev, test, prod). Whenever some change has pushed to one of the branches, I want jenkins to start the build for that specific branch and deploy it to it's own server. I know i can set a separate job for each branch but since the build task is the same for all branches, I want to use one build job for all of them and trigger each deployment jobs based on the branch.

Yoseph
  • 15
  • 1
  • 7
  • I am got confused at deploy to it's own server means?? – Chandra Sekhar Y Aug 09 '17 at 16:58
  • @ChandraSekhar what I try to say is that I have dev deployment environment for the dev build and test for test build and so on. In my jenkins setup I have a deployment job for each environment. So How can I invoke each of them based on the built branch? – Yoseph Aug 09 '17 at 20:34

3 Answers3

1

There are three components to what you want to do:

  • Define the git repository and branches that you want to pull from.
  • Decide how often you want to check or be told of changes on the remote repository.
  • What you want to build.

To define the git repository and branches you'll want to be using the git plugin. You probably have it installed already, if not install via the plugin manager page at http://your.jenkins/pluginManager.

source code management

In the image above I have defined the git repository, and specified the branches that I want to track.

Secondly you will want to decide how often you want to check for changes on the remote repository. This is managed within Build Triggers.

You could poll the remote but this is very inefficient.

Build Trigger Configuration

Instead I would recommend you look for a plugin that accepts webhooks from your git server. The main ones you may want to look at are

These plugins will allow you to configure a webhook on the git server/service and send a message to your Jenkins host when changes are made.

Finally, you will want to decide what (and where) you want to build

You can do this many ways - one such way is to use the ${GIT_BRANCH} environment variable to pass to a script that will then decide what and where to deploy.

Using a Shell build task as an example

Build task command

# run a script passing in the Git Branch that triggered this build
make build-the-things ${GIT_BRANCH}

You don't need to use a Makefile, you could pass it to a script stead.

python build-the-things.py ${GIT_BRANCH}

What you do in your script will determine your build and deploy steps.

Hammett
  • 375
  • 3
  • 11
  • In the above Build section, I have "Invoke top-level-Maven targets" section setup for packaging the code to a war file. Should I have to add the script after that? And also can you please explain what the script is doing? – Yoseph Aug 09 '17 at 18:48
  • The above Jenkins job was created as a Freestyle project. I'm not familiar with Maven Projects but the principles should be the same. Instead of passing `${GIT_BRANCH}` into your script you can read it at build time. https://stackoverflow.com/questions/10463077/how-to-refer-environment-variable-in-pom-xml/10463133#10463133 may help you to pull in environment variables from your pom.xml – Hammett Aug 09 '17 at 22:03
0

I've recently been learning this as well. You have to use "git hooks" to run scripts when a change is detected. Here is a tutorial: https://gist.github.com/Nilpo/8ed5e44be00d6cf21f22 With this, I successfully got my remote project updating anytime I pushed to the remote.

The Server-Side Hooks section in https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks is also very helpful for determining which hooks get called and when.

voxelv
  • 37
  • 1
  • 8
0

I have done this with similar example you can try and check whether it is working or not. enter image description here then you can add POLL SCM option and include time for checking your repository, it works for me, changes done to a specific branch was build leaving another one

  • Sekhar Thank you for your quick reply. I have configured like that but after the build is done how do you configure the deployment job? Let say the master build has just finished and how does it know that the build was from the master branch? In another way how do you set up the post build action to start your deployment build? – Yoseph Aug 09 '17 at 17:14
  • You need a different deployment process for each different branch build..sorry i have a little bit confusion about that point and also commented for that too. – Chandra Sekhar Y Aug 09 '17 at 17:17
  • These are the things I have done for the build process yet. – Yoseph Aug 09 '17 at 18:30
  • First, I setup the Source code Management and then next the pool SCM schedule and then in the Build section I added Invoke yop-level Maven targets to build and package my project. After that I don't really know how I can trigger my deploy jobs. Is there any way I can invoke my deploy jobs after that? – Yoseph Aug 09 '17 at 18:42
  • you can invoke deploy jobs after your maven build using a Post-build Actions – Hammett Aug 09 '17 at 22:05