CI/CD your Github repo with Azure and Heroku in 10 minutes
Working on large projects you realize the need to set up CI/CD pipelines for your codebase as manually triggering test and pushing your application to production is less than ideal and requires extra bandwidth.
Azure has made setting up pipelines for your existing GitHub Repos super easy by using their offering ‘Azure pipelines’ available in the GitHub marketplace.
Let’s take CI and CD one by one.
Continuous Integration
Let’s say you have a project wherein multiple developers are checking code into, you might prefer some way of verifying the unit test and if the code changes compile before merging their code into production, for this use case we can use azure pipelines on Github.
Firstly, you need a GitHub repo, I will be using the following Repo for demo purposes.
Next, we need to use Azure pipelines from the marketplace.
For demo purposes, we will be setting up the free plan.
Once the plan has been set up, azure will ask you if you want to set up this pipeline for all the Repos or for a specific Repo.
Once authorization has been completed, you will be redirected to Azure DevOps, where you can select your repo and pipeline type which in our case is a Maven pipeline. Once that is done, you will have a barebones pipeline for your application.
The basic pipeline set up by Azure for maven will provide you with the capability to build and run tests for your codebase, by default the pipeline will be set up for the master branch only, as can be seen in the YAML.
Upon saving the pipeline, a new file azure-pipelines.yaml will be added to the branch, the file will have your saved configurations for your pipeline. Once saved, the pipeline will be triggered automatically.
You can see the job logs for it by clicking on the pipeline.
The final result of the pipeline can also be seen on the GitHub.
Now since we have a running pipeline that verifies each commit on the master branch, we might want to have some automated process of deploying the application to production as well.
Continuous Deployment
For continous deployment, we will be using Heroku. For that, we need to create a new app on Heroku.
Let’s name our new app jdf-service
Once the app is created we can see the following dashboard
To push our code to Heroku we need to have the API key of our account, which can be found under account settings on Heroku.
We also will be needing the Heroku git URL for our app, which can be found under the settings tab on the app dashboard.
We need our azure pipeline to deploy the code to Heroku, in order to do that we first need to save some variables on azure DevOps since checking in API keys in the code base ( azure-pipelines.yaml ) is an anti-pattern and compromises the security of your application.
If we go back to editing our pipeline on azure devops we can see the option for Variables.
Here we can add an environment variable for the Heroku API key.
Once the API key has been added, we need to add a task for running a PowerShell script to push to Heroku. To add a task, we can click on the show assistant button in the top right corner and search for the task.
Upon selection, the task will ask you to provide a few configurations, whether you want to provide a file path or an inline script to be run, for our use case we will be using an inline script.
The task will also ask you to provide it with the script to be run.
We will be using the following script
git checkout mastergit remote add heroku https://heroku:$(HEROKU_APP_KEY)@git.heroku.com/jdf-service.gitgit push heroku master
HEROKU_APP_KEY is the API key we saved in the environment variables on Azure DevOps.
Upon saving, your pipeline YAML should look like the following image
Save the pipeline and watch your code gets pushed to production automatically!
Heroku automatically identifies that it is a Maven Java project and builds it accordingly.
Once the application has been deployed successfully, it can be found on the domain associated with the Heroku app. The domain can be found under the settings tab on the app dashboard.
And we have set up a complete CI/CD pipeline for our Github repo!