owner
owner

Reputation: 83

parameterized jenkins build with github webhooks

I configured my jenkins job (freestyle) with dev/test/prod environments using choice parameter with "This project is parameterised" option form jenkins job configuration.

I have setup webhook in my github soouce as https://myjenkins/github-webhook/ . when i commit a change to github repo, it is triggering build in jenkins always with "dev" environment option. how to make it to choose any of the other environment, based on my requirement?

I googled but didn't find correct answer. can someone help me?

Upvotes: 2

Views: 4830

Answers (2)

Jahangir Hussain
Jahangir Hussain

Reputation: 651

I don't know if I am late but this is for those who are seeking its answer by date.The answer is Just assign same name of your variable of build parameter in the post content parameter or any other section like headers or request. Like if I have set the branch name in DEPLOY_TO variable and it create build according to this name. But now we want to take branch name from webhook instead of manually selection. All we need to do is to set variable with same name like we will set DEPLOY_TO of value $.push.changes[0].new.name. It will work like a champ. here in post parameters and look at this here in the job parameter

Upvotes: 0

JRichardsz
JRichardsz

Reputation: 16505

Short answer

Use some branch strategy and in your job do something like:

IF BRANCH endsWith RELEASE
  deploy to testing
IF BRANCH endsWith SNAPSHOT
  deploy to dev
IF BRANCH == MASTER
  deploy to production
ETC ...

Detailed answer

When a developer perform a git push to Github, Bitbucket or Gitlab, these platforms send a Json to your continuous integration server(jenkis, travis,etc) with a lot of information related to the push event. Most important are:

  • repository name
  • target branch name: Branch who receive the git push
  • commit message
  • commit author

webhook

Then in your continuous integration server you must parse this Json to get important values. In jenkis there are several plugins like: Generic webhook, easy webhook plugin, github plugin, etc

After of values extraction you can apply simple or complex validations using branch name, commit message, commit author, etc. For instance:

  • only master branch could be deployed to production environment
  • only branch whose name ends with "snapshot" could be deployed to development environment: fix-issue-snapshot, feature-abc-snapshot, etc
  • if commit message contains "WIP" it will mean that it is still in progress or development, then no deployment will be made.
  • only push of team lead will be deployed
  • if build , unit test and other validations are passed in the source code of feature-100-development branch, a new branch called feature-100-release will be created, then this branch will be deployed to testing environment. This *release branch is candidate to be deployed in production if Q&A Team does not detect any functional issue.
  • any other automation flow from simple to complex.

Your imagination is your only limitation.

Useful links:

Upvotes: 4

Related Questions