Integrating with Jenkins
There are variety of different ways you can integrate Beanstalk with Jenkins. Two most common ways would be to either notify Jenkins of Beanstalk changes via a webhook or have Jenkins poll Beanstalk for changes. These two options should cover most of the scenarios you would use for Continuous Integration. We will describe them in full detail.
Notifying Jenkins of Beanstalk changes via webhook
Beanstalk supports variety of ways to trigger actions via webhooks based on updates happening to your Beanstalk repository. For setting up Jenkins integration with Beanstalk, we will focus on two options:
- notifying Jenkins of changes via webhooks by using Modular Webhooks integration
- notifying Jenkins of changes by using Pre/Post deployment hooks
Notifying Jenkins will happen by using Jenkins webhook URL. Before describing both notification options, we will show you how to setup Jenkins webhook URL's.
Setting up the Jenkins webhook URL
Jenkins has an easy way to create a webhook URL's for each Jenkins job. This allows you to use the webhook URL's to trigger Jenkins jobs. To create the webhook, go to your Jenkins dashboard, then configure the Jenkins jobs you would like to notify of Beanstalk changes. In the configuration options for the job, you will see an option "trigger builds remotely". Check this option and select an auth token you would like to use as shown on the screenshot below:
Testing the Jenkins web hook
After setting up the Webhook URL for your Jenkins job, save the changes. The trigger hook URL should look like this:
[JENKINS URL]/job/[TEST JOB]/build?token=beanstalk
The Jenkins URL could be something like this: https://username:email@example.com:8080 if you are using https. TEST JOB would be the name of your test job. The username and password would be your login credentials to your Jenkins account.
If you use a tool like curl, you could check whether the Jenkins hook URL you created is valid and works by running the following command:
curl -X POST https://username:firstname.lastname@example.org:8080/job/testjob/build?token=beanstalk -k
We used sample credentials and the test job name (in our case testjob). This command will trigger a Jenkins job. If you see Jenkins job being executed, then the hook URL is correct.
Running Jenkins jobs after repository update
In order to run Jenkins job after Beanstalk repository update, we will use the Modular Webhooks integration in Beanstalk. In this example, we will setup Jenkins job to be triggered, after commit (for SVN) or push (for Git) is made to a repository.
Adding the Jenkins webhook to modular integration in Beanstalk
In order to integrate with Jenkins, all you need to do is add the webhook you created for Jenkins job to your Modular Webhook integration for your repository in Beanstalk. In order to do that, go to your Beanstalk account, your repository -> settings -> Modular Web hooks. Click add a webhook button and add the webhook you created for your Jenkins job. Then select one of the webhook triggers, in our case choose "commit" for SVN or "push" for Git and finish setup.
Now that you have setup modular webhook integration, you have integrated Jenkins and Beanstalk. You are ready to run your Jenkins jobs. Once you make a commit to your Beanstalk repository, Beanstalk will notify your Jenkins job. The workflow is the following:
- Make changes in your repository
- Commit/Push changes to Beanstalk
- Once changes are commited/pushed to repository, Jenkins jobs will be notified and executed
By using modular webhooks integration in Beanstalk, you can choose other repository actions too to notify Jenkins jobs, like deployments, managing branches, managing code reviews etc.
Running Jenkins tests after deployment
In order to run Jenkins jobs after successful deployment, we will use pre/post deployment hooks. Using post deployment hooks allows you to run the tests once a deployment to your test environment finishes successfully.
Adding the webhook to deployment server in Beanstalk
In order to integrate with Jenkins, all you need to do is add the webhook you created for Jenkins job to your Beanstalk deployment server. In order to do that, go to your Beanstalk account, your repository -> deployments section -> deployment environment. Click on one of your deployment servers to which you would like to add post deployment hook.
In Post-deployment URL field, add the web hook you just generated in Jenkins, for one of your Jenkins jobs. In our case the web hook URL would be: https://username:email@example.com:8080/job/testjob/build?token=beanstalk.
Now that you added the post deployment web hook URL, you are integrated with Jenkins and you are ready to run the tests. Once you have deployed changes to your test environment from Beanstalk, Beanstalk will trigger your Jenkins job. The workflow is the following:
- Make changes in your repository
- Deploy changes through Beanstalk to your test environment
- Once deployment is finished, notify/trigger Jenkins jobs
This setup is usually used by QA teams, which deploy changes through Beanstalk to staging environment, and then run Jenkins tests.
You can fine tune your setup to add email notifications to your deployments as well. This way your team will get an email when the deployment has passed and when the Jenkins job has passed/failed.
Jenkins polling Beanstalk changes
In order to execute Jenkins job based on Beanstalk repository updates, you will need to configure your Jenkins job. To do that, go to a newly or pre-created Jenkins job. In Source Code Management section select GIT or Subversion, depending on your Beanstalk repository.
Based on your repository preference, enter the appropriate repository URL. To make things simple, we chose HTTPS URL for GIT. In order to connect to repository, don't forget to include credentials too. If everything looks ok, you should not see any error messages.
Once you have setup connection to your Beanstalk repository, all we need to do is go to "Build Triggers" section and select "Poll SCM". In "Schedule" text field, write something like H/2 * * * * (the option here checks every 2 minutes):
This will make sure that if there are any changes in your repository, the Jenkins job is triggered.
Once there are changes in repository on specific branch, Jenkins job will be triggered. The workflow here is similar to Adding the webhooks to modular integration in Beanstalk, though in this case, you have more control on when you want Jenkins job to be triggered, which might not be each commit that made to repository.
There are many different ways for implementing CI into your own workflow and the possibilities we described here are only some of them. We tried to give you a quick glance on how you can integrate Jenkins with Beanstalk and do continuous integration fairly easy and quickly.