As developers, we often have to go through many steps to build, test and deploy our project. But what if we could automate these tasks and streamline the process? GitHub already offers some automation features, but they were not enough for me. Last year i was working on a SAAS platform for a client that used Laravel for the API, Vue for the front-end and Node.js for some parts to have CURL. It was very time-consuming to deploy all these components manually, so I decided to use CI/CD to automate the process.
Today, I will explain how to do it with AWS EC2 which has Jenkins installed and runs Laravel under apache.
This is a scalable architecture that can handle high traffic, but I will leave that for another time. Let’s focus on the CI/CD pipeline first to automate your coding and building process.
Lets start,
Jenkins is a popular open-source tool for continuous integration and delivery. It allows you to automate the building, testing, and deployment of your software projects. In this article, I will show you how to configure a private GitHub repo with Jenkins using the username and SSH keys.
Before we start, you need to have Jenkins installed and configured on your server. I am using an EC2 instance on AWS, but the installation process may vary depending on your platform. You also need to install all the default plugins that are available at the time of installation.
To install Jenkins on an EC2 instance, you can follow these steps:
sudo yum update -y
sudo yum install java-1.8.0-openjdk -yy
3. Download the latest Jenkins package from the official website:
wget -0 /tmp/jenkins.war http://mirrors.jenkins-ci.org/war/latest/jenkins.war
4. [Optional] for old versions
sudo systemctl daemon-reload
sudo systemctl enable jenkins.service
sudo systemctl start jenkins.service
5. Open your browser and go to http://:127.0.0.18080to see the Jenkins web interface. You will need to enter the initial admin password that is stored by Jenkins. You can use the following command to view it: [location can be different based on the machine type]
That's all for now, let's connect Github with Jenkins so that they can communicate and can share data.
Now that we have Jenkins up and running, we need to connect it with our private GitHub repo. To do this, we will use SSH keys to authenticate and authorize our communication.
ssh-keygen -t rsa -b 4096 -C “your rajneesh@grapdevs.com“
When prompted, enter a file name for your key pair
(e.g., /home/ec2-user/. ssh/) and a passphrase (optional).
You will see something like this:
We need to add our public key to our GitHub repo as a deploy key so that we can create a communication link in between GitHub and Jenkins. A deploy key is an SSH key that grants read-only or read-write access to a single repository. This way, we can ensure that only Jenkins can access our particular repo and no one else [Do not add to your main profiles settings for best practices].
To add our public key to GitHub, follow these steps:
cat /home/ec2-user/.ssh/id_rsa.pub
If you don’t want Jenkins to make any changes to your repo, uncheck the Allow write access option. This will grant read-only access to your repo. Click on the Add key and confirm your action. Click on the Add key and confirm your action.
so now let's move to next big and important step adding private key to Jenkins
We need to add our private key to Jenkins as an SSH username and private key credential. This will allow Jenkins to use our SSH key to authenticate with GitHub and clone our repo.
To add our private key to Jenkins, follow these steps:
cat /home/ec2-user/.ssh/id_rsa
You have successfully added your private key to Jenkins as an SSH username and private key credential.
😅 😅I understand that these configurations can be tedious, but let me share a song with you. You can follow the steps while listening to the song'KALEO - Way Down We Go'
Now that we have configured Jenkins and GitHub to communicate with each other using SSH keys, we can add our repo to Jenkins as a project and start building it.
You have successfully added your repo to Jenkins as a project.
If you still get an error like “no access to GitHub” or something similar to it after adding all the keys and configurations, you can try this solution: Open Jenkins, click on Manage Jenkins and then Security. Scroll down and find “Host Key Verification Strategy”. From the dropdown menu, select “Accept first connection”. Save and apply the changes. See the following image for reference:
To test if everything is working properly, you can trigger a build manually or automatically. To trigger a build manually, follow these steps:
To trigger a build automatically, you can use GitHub webhooks. A webhook is a way for GitHub to notify Jenkins when something happens in your repo, such as a push, or a pull request.
You can use GitHub webhooks to trigger a build automatically. A webhook is a method for GitHub to inform Jenkins when an event occurs in your repo, such as a push or a pull request. I will explain how to set up webhooks in a future article or post. It’s quite easy if you do some research, but if you need help, let me know.
This is only a glimpse(30%) of what Jenkins can do. There are thousands of plugins that you can use to automate, notify developers, test, manage and approve your code. I will surely cover them in more detail later.
By joining my newsletter, you'll receive updates on the latest technology news and have the opportunity to learn more about coding.