Setting Up CI / CD with Jenkins, Blue Ocean, Github for a Rust Program
CI / CD or "Continuous Integration / Continuous Deployment" is the process of:
- taking a source code base
- running tests against it
- compiling it (if applicable)
- deploying it
CI / CD is an ongoing process that runs continuously and responds to pull requests on your source code repo.
In this blog post we are going to:
- Take a git repo with a Rust code base
- Run it thru CI / CD using Jenkins with the Blue Ocean plugin
- Compile it
- Deploy it
I'm not using Circle CI or another hosted CI / CD system as our overall deploy strategy is complex and Ansible based.
Step 1: Correct Git Work Flow
The git workflow we are using is:
- master branch
- develop branch
- feature branch
All work happens in feature branches. Feature branches are then merged into develop and develop is merged into master. The goal here is that the master branch is always kept pristine.
This means that you need to make some change to your github settings:
- Github Settings / Branches / Default branch – needs to be set to develop
- Github Settings / Branches / Branch protection rules – develop needs to be protected
Here's what you need to do:
- Create a git repo.
- Push the code base to it.
- Create a develop branch in the code base.
- Change into that branch.
- Push that branch also.
- Go to settings for the repo.
- Click on branches.
- Set develop as the default branch.
- Click on branch protection rules.
- Add develop as a protected branch. This will turn on require pull requests before merging and you can set the number of people who have to approve pull requests before the merge.
- Add master as a protected branch. For this one you don't want pull requests on; you just want master protected.
Step 2: Test the Git Work Flow
Now you want to go into your repo and:
- Create a feature branch, call it "readme", and create / update the readme for the repo.
- Then you want to push the branch
- Then you want to follow the pull request url on the branch and submit it by clicking Create Pull Request
Step 3: Get Jenkins Setup Traditionally with Blue Ocean
Setting up all of Jenkins with Blue Ocean is beyond the scope of this article but it is work that needs to be done. Here's the quick summary:
- Install Jenkins on a cloud instance.
- Install Nginx.
- Install let's encrypt.
- Set Jenkins up behind Nginx.
- Install Blue Ocean
Step 4: Add a Jenkinsfile to your Repo
A Jenkinsfile is a Blue Ocean construct that defines how the CI / CD process works. This is an ASCII file that looks something like this for a Rust project:
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'cargo build'
}
}
stage('Test') {
steps {
sh 'cargo test'
}
}
}
}
Ideally there would likely be a stage('Deploy') but we haven't built that with Blue Ocean yet so I might be wrong. Watch this blog post for an update.
Here's what to do:
- In develop create a new branch called jenkinsfile i.e. git branch jenkinsfile
- Change into this branch i.e. git co jenkinsfile
- Create the file i.e. touch Jenkinsfile
- Add the contents above to Jenkinsfile
- Do the add / commit / push dance
- Create the pull request on Github
- Go thru the code review / merge dance.
Step 5: Use Blue Ocean to Setup a CI / CD Pipeline
Note: I manually put the above Jenkinsfile in place before doing this. I am told that Blue Ocean will do this automatically if you don't do it yourself.
Blue Ocean is an open source toolkit that makes CI / CD drop dead easy for Jenkins. I'm an old school Jenkins guy and someone described Jenkins versus Blue Ocean as "Jenkins makes me tear my hair out" (thanks Don) and he was absolutely correct. Here are the steps needed to use Blue Ocean to setup a build / test pipeline.
- From the Jenkins sidebar, click Open Blue Ocean.
- Click the New Pipeline button on the upper right.
- Select github as your source code host (or whatever).
- Click the organization that owns the repo.
- Select the repo.
- Click the Create Pipeline button.
- Click on the latest branch.
- Click on the Pencil icon to make any changes.
- Click Save and Run.
This starts the build process. You may need to edit the Jenkinsfile to match the details of your code base if you didn't already create it.
Step 6: Setup Jenkins Traditionally
Note: If you are using Blue Ocean then you don't need to this. I wrote this before I was shown the magnificent awesomeness that is is Blue Ocean. I left it here though to illustrate just how much simpler Blue Ocean makes the CI / CD process.
Within Jenkins there is a dashboard of your items. An item is a project that you want to build. Here's what to do:
- In Jenkins select New Item.
- From the list item items, when you have Blue Ocean installed, then you have the Multi Branch Pipeline item type. Select that.
- Name your item and click Ok at the bottom.
- A very long form is displayed; this is normal for Jenkins.
- Set the Name and Description fields.
- Set the Repository HTTPS URL to: your github repo.
- Set the Discover Branches strategy to: All Branches.
- Set the Discover pull requests from forks strategy to: Merging the pull request with the current target branch revision
- Set the Trust to: From users with Admin or Write permission
- Set the Discover pull requests from origin strategy to: Merging the pull request with the current target branch revision
- Set the Property strategy to: All branches get the same strategy
- Set the Build Configuration to by Jenkinsfile
- Set the Script path to by Jenkinsfile
- Click Ok at the bottom to save the Jenkins project.
And at this point you still really need to go into Blue Ocean. My advice is don't do it this way and just follow Step 5 above.