CD/CI: Working with Jenkins, Bitbucket and Heroku

Wajeeh Ahsan
3 min readNov 2, 2020

Step 1: Make sure you’ve Java installed. Run java -version to make sure Java is installed and what version it is. If not installed, run the following commands to install.

$ sudo apt-get update
$ sudo apt-get install openjdk-8-jdk openjdk-8-jre

Step 2: Then install the latest stable Jenkins version:

$ wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -
$ sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
$ sudo apt-get update
$ sudo apt-get install jenkins

Step 3: Start the Jenkins server. To start it, run:

$ sudo service jenkins start

Step 4: Setup Jenkins. Go to localhost:8080 . You should see the Unlock Jenkins screen, which displays the location of the initial password.

In the terminal window, use the cat command to display the password:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Copy the 32-character alphanumeric password from the terminal and paste it into the Administrator password field, then click Continue.

The next screen presents the option of installing suggested plugins or selecting specific plugins. We’ll click the Install suggested plugins option, which will immediately begin the installation process.

When the installation is complete, you will be prompted to set up the first administrative user. It’s possible to skip this step and continue as admin using the initial password we used above, but we’ll take a moment to create the user.

Then you will see an Instance Configuration page that will ask you to confirm the preferred URL for your Jenkins instance. Confirm either the domain name for your server or your server’s IP address. For local setup, it will be http://localhost:8080/ .

After confirming the appropriate information, click Save and Finish. You will see a confirmation page confirming that “Jenkins is Ready!”

Step 5: Integrate Jenkins with and Heroku.

Pre-requisite: Before one can integrate Jenkins & Heroku, following must be known-

  1. Heroku application creation.
  2. Creating Ruby on rails project.
  3. Jenkins Bitbucket plugin

Following steps are to be administered, to achieve the objective:

Install Git plugin to activate the ‘git publisher’ option in your configuration window.

Go to Jenkins dashboard –> Manage Jenkins –> Manage plugins –> Available –> Bitbucket.

Step 6: As it is a Rails Application, install following plugins too, with the same process as decribed above:

  • rvm
  • rake

Step 7: Set Heroku credentials in Jenkins

In order to authorize Jenkins and to build a trust between Heroku and Jenkins, some sort of credentials are required to be added.

You can use SSH as most of the other articles have shown. However, following is an easy way to do the same:

a). Go to Heroku Dashboard

b). On leftmost corner click on to the ninja image

c). Click on ‘Account settings’

d). You will see the ‘API key’ section. Copy it.

e). Go to Jenkins dashboard. Among the list of options in the right-hand side, choose ‘Credentials’ –> Click on Global –> Add credential –> Choose ‘Username with Password’ type from the drop-down list.

f). Provide any Username (like ‘loginwithapikey’ in this case).

Paste the API key as password and hit save.

Step 8: Create a freestyle project in jenkins. In the configuration window of the project:

a). SCM section: Choose two git repositories. One for fetching code from your bitbucket account and second for Heroku git repository.

Give the created credentials with Heroku git Repository URL.

  • Note: Click on Advanced section, beneath heroku git repository URL. Give any name to this (like ‘heroku’ in this case).

b). Build Enviornment:

  • Choose ‘ Run the buuild in a RVM managed enviornment’ and either type the verion (‘2.5.1’ in this case) or can also type a dot (.) in the implementation.

c). Build:

Add a build step –> ‘Execute shell’ and write the following:

heroku rake db:migrate db:seed --app <name of application>

d). Add a ‘Post-build Action’

  • Choose Git Publisher.
  • select the checkboxes (appropriately as per your need)
  • Select Branches and give

Branch to push : master

Target remote name: <name specified above with Heroku git repository URL>

Step 9: Save and Build Now.

--

--