I am not going to describe what is CI/CD and GitHub action in book language. There are lot of resources for that .
According to me CI is checking the application is working or not and CD is deliver the application . Obviously both is in automatic way . But it can do a lots of things. This is just a scratch . GitHub action is a tool which will help us to achieve CI/CD easily and this is also free for certain time limit .
- Get public ssh key of EC2 instance
- Put the public ssh key in Git repository’s deploy keys
- Get the private key , host name and user name of AWS EC2 instance
- Create the .yml file in GitHub to build and deploy the application.
Lets start the description . First we need to get the ssh key .
[ec2 instance]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ubuntu/.ssh/id_rsa
Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub
If we do not provide any file name this will create two file named id_rsa and id_rsa.pub in .ssh folder. We will copy the key inside of id_rsa.pub and put it in GitHub deploy key . We will find deploy key in settings of our repository .
Give a tick in “Allow write access ”. We need this so that we do not need to give GitHub username and password every time we pull from EC2 instance . Now clone the repository using SSH .
[ec2 instance] $ git clone email@example.com:Farhandroid/CounterApp.git
To connect GitHub action and EC2 instance we need three things .
- AWS private key.
- EC2 instance’s username
- EC2 instance’s Hostname
There are several way to get AWS private key . But we will use the key.pem file of EC2 instance .
For my case ubuntu will be user name and hostname will be EC2’s Public IPv4 address .
we will keep all of this in our repository’s secret key .
Now we will write the CI_CD.yml file which will execute the whole process . This CI_CD..yml file will be written in repository’s .github/workflows folder .
This code is pretty simple . We want to perform CI/CD operation if there is any push in master branch or if a branch is merged in master branch . After deploying the app we can see the app in ec2’s_public_ip:8899 address .
It is a great app . Is not it 😃?.
That’s all for today . Any suggestions are greatly welcomed . Thank you .