Skip to content

Commit ea0d36f

Browse files
committed
Initial commit of contents for new action
1 parent 1c7c4e0 commit ea0d36f

File tree

6 files changed

+493
-2
lines changed

6 files changed

+493
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__pycache__/
2+
tests/__pycache__/
3+
*.pyc

Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM python:3.12-slim
2+
3+
WORKDIR /app
4+
5+
COPY requirements.txt requirements.txt
6+
RUN pip install -r requirements.txt
7+
8+
COPY . .
9+
10+
ENTRYPOINT ["python", "/app/publish_to_hashnode.py"]

README.md

Lines changed: 169 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,169 @@
1-
# publish-github-to-hashnode
2-
A GitHub Action for publishing Posts to a Publication in Hashnode
1+
# Publish GitHub to Hashnode GitHub Action
2+
3+
This GitHub Action publishes blog posts from a GitHub repository to a specific publication on Hashnode. It reads markdown files, processes the frontmatter and content, and uses the Hashnode API to create, update, or delete posts.
4+
5+
## Features
6+
7+
- Create new posts on Hashnode if they do not exist.
8+
- Update existing posts on Hashnode if they exist.
9+
- Delete posts on Hashnode if the corresponding markdown file is deleted.
10+
- Handles correct linking of cover images and inline images in the markdown content.
11+
12+
## Inputs
13+
14+
- `access-token` (required): Your Hashnode API Personal Access Token. See: [Hashnode Developer Settings](https://hashnode.com/settings/developer)
15+
- `posts-directory` (optional): The local directory containing the blog posts, if different from the root directory. Default: `posts`
16+
- `publication-host` (required): The publication host (e.g., `blog.mydomain.com`).
17+
18+
## Outputs
19+
20+
- `result_json`: Publish result as a JSON string.
21+
- `result_summary`: Publish result summary formatted as text.
22+
23+
## Usage
24+
25+
### 1. Create a `.github/workflows/publish.yml` file
26+
27+
Create a new workflow file in your repository to define the steps required to publish the posts.
28+
29+
```yaml
30+
name: Publish My Hashnode Blog Posts
31+
on:
32+
push:
33+
34+
jobs:
35+
publish:
36+
runs-on: ubuntu-latest
37+
steps:
38+
- uses: actions/checkout@v4
39+
with:
40+
fetch-depth: 0
41+
42+
- uses: tj-actions/changed-files@v44
43+
44+
- uses: actions/setup-python@v5
45+
with:
46+
python-version: '3.x'
47+
48+
- run: pip install -r requirements.txt
49+
50+
- name: Publish to Hashnode
51+
uses: actions/publish-github-to-hashnode@v1
52+
with:
53+
access-token: ${{ secrets.HASHNODE_ACCESS_TOKEN }}
54+
posts-directory: 'content/posts' # The directory within your repository containing the markdown files, if different from the root directory
55+
publication-host: 'blog.mydomain.com' # Your publication host
56+
```
57+
58+
### 2. Store your Hashnode API Access Token as a GitHub Secret
59+
60+
1. Obtain your Hashnode API Personal Access Token. See: [Hashnode Developer Settings](https://hashnode.com/settings/developer)
61+
2. Go to your repository on GitHub.
62+
3. Click on `Settings`.
63+
4. Scroll down to `Secrets and variables` and click on `Actions`.
64+
5. Click `New repository secret`.
65+
6. Add a new secret with the name `HASHNODE_ACCESS_TOKEN` and your Hashnode API token as the value.
66+
67+
### 3. Prepare your repository structure
68+
69+
Ensure your repository contains the markdown files you wish to publish in the specified directory (default is the root of the repository).
70+
71+
### 4. Markdown Post Frontmatter
72+
73+
#### Frontmatter Fields
74+
75+
Full list of frontmatter fields that can be used in the markdown files:
76+
77+
- `title` (required): Title of the post.
78+
- `subtitle` (optional): Subtitle of the post.
79+
- `slug` (required): Slug of the post.
80+
- `tags` (optional): Tags of the post (comma-separated).
81+
- `enableTableOfContents` (optional, default: false): Enable table of contents.
82+
- `publish` (optional, default: true): Should the post be published at this time.
83+
- `coverImage` (optional): Cover image relative path within the repository starting from `posts-directory` (as specified in pubish.yml) if provided.
84+
- `coverImageAttribution`: Information about the cover image attribution (optional)
85+
- `publishedAt`: Date and time when the post was published (optional)
86+
87+
#### Example Frontmatter
88+
89+
```markdown
90+
---
91+
title: Creating Spaghetti in Docker Compose
92+
slug: creating-spaghetti-in-docker-compose
93+
tags: docker,docker-compose
94+
enableTableOfContents: true
95+
coverImage: images/cover.jpg
96+
---
97+
98+
## Introduction
99+
100+
This is an introduction to creating spaghetti in Docker Compose.
101+
102+
## Ingredients
103+
104+
- Docker Engine
105+
- Spaghetti
106+
- Sauce
107+
- Cheese
108+
- Love
109+
110+
## Steps
111+
112+
1. ...
113+
2. ...
114+
3. ...
115+
```
116+
117+
### 5. Handling Image URLs
118+
119+
The action will automatically convert relative image URLs to absolute URLs that point to the raw content on GitHub. Ensure your image paths in the markdown are correct relative paths.
120+
121+
## Example Workflow Using `result_json`
122+
123+
You can utilize the `result_json` output in subsequent steps to get the result of the publish operation in json format. The approach below can also be used with `result_summary` for a text summary.
124+
125+
```yaml
126+
name: Publish My Hashnode Blog Posts
127+
on:
128+
push:
129+
130+
jobs:
131+
publish:
132+
runs-on: ubuntu-latest
133+
steps:
134+
- uses: actions/checkout@v4
135+
with:
136+
fetch-depth: 0
137+
138+
- uses: tj-actions/changed-files@v44
139+
140+
- uses: actions/setup-python@v5
141+
with:
142+
python-version: '3.x'
143+
144+
- run: pip install -r requirements.txt
145+
146+
- name: Publish to Hashnode
147+
id: publish
148+
uses: actions/publish-github-to-hashnode@v1
149+
with:
150+
access-token: ${{ secrets.HASHNODE_ACCESS_TOKEN }}
151+
posts-directory: 'content/posts'
152+
publication-host: 'blog.mydomain.com'
153+
154+
- name: Get the output JSON
155+
run: |
156+
echo "${{ steps.publish.outputs.result_json }}"
157+
```
158+
159+
## Development
160+
161+
To contribute to the development of this action, clone the repository and submit a pull request.
162+
163+
## License
164+
165+
This project is licensed under the MIT License.
166+
167+
## Questions and Support
168+
169+
If you have any questions or need support, please open an issue in the GitHub repository. I will do my best to help.

action.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: "publish-github-to-hashnode"
2+
author: "Jack Linke"
3+
description: "Publish Posts from a GitHub repository to a Hashnode Publication."
4+
branding:
5+
icon: "edit-3"
6+
color: "green"
7+
8+
inputs:
9+
access-token:
10+
description: "Your Hashnode API Personal Access Token. See: https://hashnode.com/settings/developer"
11+
required: true
12+
publication-host:
13+
description: "The publication host (e.g., blog.mydomain.com)."
14+
required: true
15+
posts-directory:
16+
description: "The local directory containing the blog posts, if different from the root directory."
17+
required: false
18+
default: "posts"
19+
20+
outputs:
21+
result_json:
22+
description: "Publish result as a JSON string"
23+
result_summary:
24+
description: "Publish result summary formatted as text"
25+
26+
runs:
27+
using: "docker"
28+
image: "Dockerfile"
29+
env:
30+
ACCESS_TOKEN: ${{ inputs.access-token }}
31+
POSTS_DIRECTORY: ${{ inputs.posts-directory }}
32+
PUBLICATION_HOST: ${{ inputs.publication-host }}
33+
CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
34+
args:
35+
- ${{ inputs.access-token }}
36+
- ${{ inputs.posts-directory }}
37+
- ${{ inputs.publication-host }}

0 commit comments

Comments
 (0)