Making a Readme file for a repository is one of the best ways to provide its summary. They are not dynamic because GitHub Readme files do not support JavaScript. So, in today’s article, we’ll demonstrate how to dynamically change the contents of the Readme file using Python and GitHub Actions. We’ll also use GitHub Secrets to keep our sensitive information safe.
Step Involved in the article:
- Creation of a Readme File.
- Use an API to get Random Quotes in Python.
- Update the Quotes and display them on the Readme File using Python Script.
- Use GitHub Actions to run the Python Script and schedule the task at regular intervals.
- Use GitHub Secrets to hide the Sensitive data.
Creation of a Readme File
Initially, we need to create a readme.md file in Markdown and include all the static material that has to be shown. If you are encountering issues or want to know more about the Basic writing and formatting syntax, please visit here.
While JavaScript files cannot be integrated into GitHub Readme files, we will use Python Script with GitHub Actions to directly manage our Readme file and change the quotes.
HTML
|
Output:
API to get Random Quotes
Several APIs are available that provide Random quotes. Here, we’re going to use API Ninjas. API Ninjas offers a variety of Quotations for free cost. By registering on API Ninjas, you can get the API Key.
Example
After getting the API we will test for Random Quotes. Here the Python script generates random quotes each time you run the script, a different quote will be generated.
Python3
|
Output:
Python script that modifies the Readme file
We’ll now create a Python script that modifies the Readme file and changes the old quote with the new fetched quote. The complete script which is given below should be placed in the index.py file.
Python3
|
Creation of requirements.txt File
Create a requirements.txt file and place the contents given below in it:
beautifulsoup4
Markdown
requests
Use GitHub Actions to run Python Scripts and Push it again:
To execute the Python script, we must now build a GitHub action.
Steps:
- Open the Repository.
- Go to Settings.
- Select “Pages” from the “Code and Automation” section in the sidebar.
- Select “GitHub Actions” from the “Source” dropdown menu.
- Click on the “Configure” Button of “GitHub Pages Jekyll”
Now we need to write a .yml script that executes the python code regularly, Here we have used the time interval of 12 hours for running the Python Script. If you wish to modify it, simply enter a different time in the schedule. To obtain the required corn task time, you can take help from the https://crontab.guru/ webpage. Now, Paste the code below:
name: Update Quotes
on: schedule: [cron: "0 */12 * * *"] workflow_dispatch: push: branches: ["master", "main"] jobs: build: runs-on: windows-latest steps: - uses: actions/checkout@v3 - name: Setup python uses: actions/setup-python@v3 with: python-version: "3.8" - name: Installing dependencies run: pip install -r requirements.txt - name : Running script file run: python index.py - name: Update the Quote run: | git config --global user.name 'GitHubUserName' git config --global user.email 'EmailID@gmail.com' git add . git commit -am "Daily quotes" git push origin main --force
Click on “Start Commit”. You may see a yml file in your repository after the file has been committed. That’s it; the quotes will be dynamically updated at the scheduled time.
Use of GitHub Secrets
You may have noticed that we pose a risk by making the API Key public. Thus, we must conceal the key without upsetting the surrounding area. We will make use of GitHub Secrets for this.
Steps to save the API Key as a secret variable:
- Open the Repository.
- Go to Settings.
- Click on “Secrets and Variables” from the Security Section and select “Actions”.
- Click on the “New repository secret” button.
- Enter the Secret Name and Value.
- Click on Add Secret.
We will set this secret key as an environment variable by editing the following code in the yml file. Now, The complete yml script will look as shown below:
env: api_key: $ secrets.API_KEY
Retrieve API from the Environment variables
Now, rather than entering the API key straight into the Python script, we will retrieve it from the environment variables by replacing entering the API with Getting the API key from the env variable. Below is the complete Python script:
# Getting API key from environment variable
API_KEY = os.environ.get('api_key')
Python3
|
The safest code that dynamically changes the quotes (data) of the Readme file is there once you push the script. In the Action section of the repository, details of all the scheduled tasks are visible.
Output:
Complete Guide to In Video:
#2023, #API, #APIs, #Article, #Automation, #Code, #Container, #Corn, #Data, #Details, #Display, #Dropdown, #Editing, #Email, #Environment, #Generator, #Git, #Github, #Global, #Gmail, #Headers, #How, #HowTo, #HTML, #Intelligence, #Issues, #It, #JavaScript, #Jobs, #Json, #Language, #Leadership, #Material, #Media, #Menu, #One, #Os, #Programming, #Python, #R, #Read, #Repository, #Responsive, #Risk, #Screenshot, #Secrets, #Security, #Sensitive, #SensitiveInformation, #Setup, #Sidebar, #String, #Structure, #Syntax, #Table, #Text, #Time, #Video, #Windows, #Writing
Published on The Digital Insider at https://bit.ly/41eB6zL.
Comments
Post a Comment
Comments are moderated.