-
Notifications
You must be signed in to change notification settings - Fork 2.1k
added example of github actions #915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,67 @@ | ||||||
import os | ||||||
from google import genai | ||||||
|
||||||
|
||||||
def convert_to_markdown(text_content,api_key): | ||||||
|
||||||
|
||||||
client = genai.Client(api_key = api_key) | ||||||
|
||||||
|
||||||
|
||||||
MODEL_ID = "gemini-2.5-flash" # @param ["gemini-2.5-flash-lite", "gemini-2.5-flash", "gemini-2.5-pro"] {"allow-input":true, isTemplate: true} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This
Suggested change
|
||||||
|
||||||
|
||||||
prompt = f""" | ||||||
Convert the following text into well-structured Markdown format suitable for GitHub documentation. | ||||||
Use appropriate headings (#, ##, ###), lists, code blocks, and bold/italic formatting. | ||||||
Ensure the output is clean and directly usable in a .md code don't include (```Markdown ````). | ||||||
|
||||||
Text to convert: | ||||||
{text_content} | ||||||
""" | ||||||
|
||||||
try: | ||||||
|
||||||
response = client.models.generate_content( | ||||||
model = MODEL_ID, | ||||||
contents = prompt | ||||||
) | ||||||
|
||||||
|
||||||
return response.text | ||||||
except Exception as e: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
print(f"An error occurred during content generation: {e}") | ||||||
return "" | ||||||
|
||||||
def main(): | ||||||
|
||||||
|
||||||
api_key = os.getenv("GEMINI_API_KEY") | ||||||
if not api_key: | ||||||
print("Error: GEMINI_API_KEY environment variable is not set.") | ||||||
exit(1) | ||||||
|
||||||
input_file = "source.txt" # You can change this to your input file | ||||||
output_file = "README.md" # You can change this to your desired output file | ||||||
Comment on lines
+60
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
|
||||||
|
||||||
if not os.path.exists(input_file): | ||||||
print(f"Error: Input file '{input_file}' not found.") | ||||||
exit(1) | ||||||
|
||||||
with open(input_file, "r") as f: | ||||||
text_content = f.read() | ||||||
|
||||||
markdown_content = convert_to_markdown(text_content, api_key) | ||||||
|
||||||
if markdown_content: | ||||||
|
||||||
with open(output_file, "w") as f: | ||||||
f.write(markdown_content) | ||||||
print(f"Successfully converted '{input_file}' to '{output_file}'.") | ||||||
else: | ||||||
print("Markdown conversion failed.") | ||||||
|
||||||
if __name__ == "__main__": | ||||||
main() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
|
||
name: Generate Markdown from Text | ||
|
||
|
||
on: | ||
push: | ||
paths: | ||
- 'source.txt' | ||
pull_request: | ||
paths: | ||
- 'source.txt' | ||
|
||
|
||
|
||
|
||
permissions: | ||
contents: write | ||
|
||
|
||
jobs: | ||
convert_to_md: | ||
runs-on: ubuntu-latest | ||
|
||
|
||
steps: | ||
- name: Checkout Repository | ||
uses: actions/checkout@v3 | ||
|
||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.10' | ||
|
||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install google-genai | ||
|
||
|
||
- name: Run Markdown Converter Script | ||
run: python convert_to_md.py | ||
env: | ||
|
||
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }} | ||
|
||
|
||
- name: Commit and Push Changes | ||
uses: stefanzweifel/git-auto-commit-action@v4 | ||
Comment on lines
+28
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Several of the GitHub Actions used in this workflow are outdated. It's recommended to update them to their latest major versions to benefit from new features, performance improvements, and security patches:
|
||
with: | ||
commit_message: 'Auto-generate README.md from source.txt' | ||
branch: main | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoding |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,62 @@ | ||||||||||||||||||
# AI Text to Markdown Converter GitHub Action | ||||||||||||||||||
|
||||||||||||||||||
This is a GitHub Action that automatically converts plain text into a well-structured Markdown document using the Gemini API. It's designed to streamline the process of creating and maintaining documentation, like `README.md` files, directly within your repository. | ||||||||||||||||||
|
||||||||||||||||||
## How It Works | ||||||||||||||||||
|
||||||||||||||||||
The workflow is simple and automated. You write your content in a plain text file, push your changes, and the GitHub Action handles the rest. | ||||||||||||||||||
|
||||||||||||||||||
1. **Write Content:** You write or paste your text into a designated source file (e.g., `source.txt`). | ||||||||||||||||||
2. **Commit & Push:** You commit and push the changes to your repository. | ||||||||||||||||||
3. **Workflow Trigger:** The GitHub Action automatically detects the change to the `source.txt` file and starts the conversion process. | ||||||||||||||||||
4. **AI Conversion:** A Python script uses the Gemini API to intelligently convert your text into Markdown, adding headings, lists, bold text, and other formatting as needed. | ||||||||||||||||||
5. **Auto-Commit:** The action then automatically commits the new or updated Markdown content to a destination file (e.g., `README.md`). | ||||||||||||||||||
|
||||||||||||||||||
## Getting Started | ||||||||||||||||||
|
||||||||||||||||||
To use this tool in your own repository, you need to set up two files and a secret. | ||||||||||||||||||
|
||||||||||||||||||
### Step 1: Add the Tool's Files | ||||||||||||||||||
|
||||||||||||||||||
Place the following two files in your repository: | ||||||||||||||||||
|
||||||||||||||||||
* **`convert_to_md.py`**: This script contains the logic for the AI conversion. | ||||||||||||||||||
* **`.github/workflows/generate-markdown.yml`**: This YAML file defines the GitHub Action workflow. | ||||||||||||||||||
|
||||||||||||||||||
* **`structure of the repository`**: | ||||||||||||||||||
Comment on lines
+23
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The formatting of this list is a bit confusing. The last item, 'structure of the repository', seems like it should be a sub-heading or regular text rather than a bullet point in the list of files.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
/your-repository | ||||||||||||||||||
├── .github | ||||||||||||||||||
│ └── workflows | ||||||||||||||||||
│ └── generate-markdown.yml # The GitHub Action workflow file | ||||||||||||||||||
├── convert_to_md.py # The Python script for conversion | ||||||||||||||||||
├── source.txt # The input text file | ||||||||||||||||||
└── README.md # The output Markdown file (created automatically) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
### Step 2: Add Your API Key | ||||||||||||||||||
|
||||||||||||||||||
To securely use the Gemini API, you must store your API key as a GitHub Secret. | ||||||||||||||||||
|
||||||||||||||||||
1. Go to your repository's **Settings > Secrets and variables > Actions**. | ||||||||||||||||||
2. Click **New repository secret**. | ||||||||||||||||||
3. Set the **Name** to `GEMINI_API_KEY`. | ||||||||||||||||||
4. Paste your personal Gemini API key into the **Secret** field. | ||||||||||||||||||
|
||||||||||||||||||
### Step 3: Ensure Read and Write Permissions | ||||||||||||||||||
|
||||||||||||||||||
For the GitHub Action to be able to create and push a new file to your repository, you must grant it permission. | ||||||||||||||||||
|
||||||||||||||||||
1. Go to your repository's **Settings > Actions > General**. | ||||||||||||||||||
2. Scroll down to the **Workflow permissions** section. | ||||||||||||||||||
3. Select **Read and write permissions**. | ||||||||||||||||||
4. Click **Save**. | ||||||||||||||||||
|
||||||||||||||||||
### Step 4: Use the Tool | ||||||||||||||||||
|
||||||||||||||||||
1. Create a file named `source.txt` in the root of your repository. | ||||||||||||||||||
2. Add your plain text content to `source.txt`. | ||||||||||||||||||
3. Commit and push your changes. | ||||||||||||||||||
|
||||||||||||||||||
The workflow will run automatically, and you will see a new `README.md` file created with the formatted Markdown content. | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a newline at the end of the file. It's a standard convention for text files.
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# GitHub Actions and the Gemini API | ||
|
||
This directory contains examples of using GitHub Actions to create automated workflows powered by the Gemini API. | ||
|
||
## What is a GitHub Action? | ||
A GitHub Action is a custom, automated task that you can run in your repository. You can use actions to automate a wide range of tasks, from running tests and building applications to creating documentation and performing content-related tasks. They are a core part of a developer's CI/CD (Continuous Integration/Continuous Delivery) workflow. | ||
|
||
## How They Integrate with the Gemini API | ||
GitHub Actions are perfect for automating AI-powered tasks. By creating a workflow that calls the Gemini API, you can: | ||
- **Generate content:** Automatically create blog posts, documentation, or code snippets. | ||
- **Analyze text:** Review new code, issues, or pull requests for summaries or key information. | ||
- **Format data:** Convert unstructured data into a clean, structured format, such as the text-to-Markdown example in this directory. | ||
|
||
Because the workflows run on GitHub's servers, they have direct access to your repository's files and can use secrets to securely handle your API keys. This creates a powerful, hands-off automation system. | ||
|
||
## Examples | ||
* [AI Text to Markdown Converter](./ai-markdown-converter/README.md) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the Google Python Style Guide, which this project follows, there should be no spaces around the
=
sign for keyword arguments.1Style Guide References
Footnotes
The project's style guide specifies the use of the Google Python Style Guide. (link) ↩