Deploying Golang application to Heroku from github master branch after successfully running the test cases on Circleci.
In my previous blog I had explained how to achieve continuous integration using Circleci. We had developed a simple Golang rest API for maths functions and performed some automated tests using Circleci. This blog is in continuation to it and now we are going to deploy our application to Heroku automatically if all the test cases pass on Circleci. If you haven’t read the previous blog you ca read it here.
Lets get started
Setting up Heroku app
You will need a Heroku account, if you do not have one then you can create it here. Login to Heroku and create a new app from the dashboard as shown in below screenshots.
Once you create an app you can choose a deployment method, here we will choose Github as our goal is to automate deployment by pulling the source code from github. Add your github handle and select the repository and click connect button.
Now you will see that the app is connected with the github repository and now you need to select the branch to deploy, check the wait for CI to pass before deploy option and click Enable Automatic Deploys button
Now we need to login to Circleci dashboard to add some environment variables required for automatic deployment on Heroku. Click on Project Settings
Now add two variables HEROKU_APP_NAME which will be math-api-demo in our case and HEROKU_API_KEY which we will generate in the next step.
To generate the HEROKU_API_KEY we need Heroku CLI installed on our machine.
Once you install Heroku CLI you can open the terminal and type the below command to generate the token, copy and paste the token in Circleci environment variables
Now we need to make some changes to the config.yml file in our project to add the deployment steps. We will create a new branch heroku-deploy and make the further changes. Our updated config.yml will look like below.
Here we create a workflow named heroku_deploy with two jobs: build that is our previous testing job and heroku/deploy-via-git to deploy our application. The deployment job will only start if the test job succeeds and only master branch code will be used for deployment. So when we push our code to some other branch only the test cases will run and if we raise a PR to merge into master branch, after successful merge again test cases will run and the master branch code will be deployed to Heroku.
Also when we deploy an app to Heroku, it will run the app on any random port and not the port we specify, but how do we know that port number, its not that difficult as Heroku exports that port as an environment variable named PORT so now we need to make some change to our go code and read that port to run our server. Add a new folder called config and to it add a file config.go, the code is given below.
Also make some changes to the main.go file to read the PORT variable set by Heroku, you can replace previous code with below.
Now push this new branch to remote and you will see that the test cases will pass but the application won’t be deployed, as we have not merged our code to master.
So now create a PR to merge heroku_deploy branch to master.
Once you merge you will see that the workflow on Circleci will contain two jobs build (for testing) and heroku/deploy-via-git (for deployment).
Click on heroku/deploy-via-git to read the logs, it will print that the the app is deployed to Heroku.
Now you can open the app in the browser to check if the endpoint is working
Now whenever we create a new PR and merge our code to the master all the test cases will run on Circleci and the latest build of our application will be deployed on Heroku. This will save us a lot of time as all the builds and deploys are automated, we just have to ensure that the code runs successfully on our local machine and passes all the test cases.