|
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. |
0 commit comments