Integrating GitHub API Calls with Shell Scripts on AWS EC2

Integrating GitHub API Calls with Shell Scripts on AWS EC2

Introduction :

Automation is the most important step in Devops. In this blog , we’ll interact with the GitHub API to read the list of user’s public repositories and created date using shell scripts , running them on an AWS EC2 instance.

Step-1 : Create an ec-2 instance.

  • First login to AWS console.

  • Search ec2 instance.

  • Click on launch instance.

Configure ec2 instance :

  • Enter a name for your instance.

  • Select Ubuntu as an operating system.

  • Select Instance type as per your needs. Ex: t2.micro

  • Select a key-pair for secure connection.

    • If you had a pair select that from dropdown else ,

    • click on create new key pair.

  • Click on launch instance.

  • Once the instance is ready connect to instance by the below command on linux or if u had windows use MobaXterm.

      ssh -i /path-to-key-value-pair.pem Ubuntu@ec2-instance-ip-address
    

Step-2 : Create a GitHub API token.

  • Login to GitHub and open settings tab.

  • Select Developer Settings available on side bar.

  • Click on personal access token and then click on tokens classic.

  • Click on Generate new token for classic and give authentication password.

  • Select necessary checkboxes to give access and then click on generate token.

  • Copy token.

Step-3 : Create a shell script for GitHub API calls.

  • First create a scripting file.
vim filename.sh
#!/bin/bash

#############################
# Author : Vishal
#created at  : 06/01/2025
#############################


api=https://api.github.com #main api of github

list_repo_url=$api/users/$username/repos #entire url formation

echo "username $username" #printing the username given from export command
echo  "url is $list_repo_url" #printing the entire url .

arr=$(curl -L \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer $token" \  
  H "X-GitHub-Api-Version: 2022-11-28" \
  $list_repo_url | jq -c '.[]') #sending request and storing the json compact format in array


echo $arr | while ISR= read i ;do
        echo $i | jq -r '"Name : \(.name)     Created at : \(.created_at)"'  #printing repo name and created date.
done;
  • Save and exit the file.

Step-4 : Executing Script.

  • Insert username and token value by export command.
export username=github-username
export token=your-github-api-token
  • Change permission for file to execute by user.
chmod 777 filename.sh
  • Execute the script.
/filename.sh

Conclusion :

In this blog, we explored how to integrate the GitHub API with Shell scripts on an AWS EC2 instance, demonstrating a practical approach to automating tasks like fetching repositories data and just displayed name and created-date information.