GitHub is the leading company for software development and version control using Git. That allows us to create public repositories as well as private ones. Both have their own benefits. The private repositories are secured with authentication, whereas the public repositories are accessible to everyone. Sometimes, we or our teams make repositories with public access that may contain sensitive data. It’s a good idea, to configure a script that notifies us of the repositories left public access.
In this tutorial, I have created a shell script that checks for all public repositories under a GitHub account and sends a notification email. The script can be scheduled under the cronjobs to automate this.
This script uses Curl and SendEmail command line utilities. So make sure you have installed both packages on your system.
sudo apt install curl sendemail
The curl command is used to call GitHub API and SendEmail is used for sending email via the SMTP server.
Create a Shell Script
I have already created a bash script for you. You can simply copy the script and write it in a file on your system.
- Create a shell script in your system and edit it in your favorite text editor:
- Copy the below content and paste it into the edited file.
- Update the “GITHUB_USER” id with your GitHub user or organization name. Also, update the SMTP details to get email notifications.
- Press “CTRL + O” to write the changes and then “CTRL + X” to quit the editor. Then make the shell script executable with the following command.
chmod +x check-github-public-repos.sh
- Now, your script is ready to run. You can simply execute your script directly on the terminal. To print the results on screen, you can uncomment the last line of the script.
On successful execution, if the script found any public repository in your GitHub account. You will get an email notification in your mailbox.
Schedule Script with Crontab
You can schedule this script to run daily, weekly, or twice a week. The following crontab example will run this script daily at 12:00 AM.
It is most important to keep our hard work safe. This script will alert you to the pubic repositories in your GitHub account. You can modify the script as per your requirements. You can also provide your enhancement via the comments. Also provide your feedback about this script, which encourages us to write more.