Code Monkey home page Code Monkey logo

jekyll-blog-archive-workflow's Introduction

Automated Archives in GitHub pages

Jekyll Archives Workflow

This workflow action helps in automatically creating archives for GitHub pages built with Jekyll.

Blog Configuration

Create a collection

  • Open _config.yml file to edit your configuration.
  • Add a collection to the configuration like below.
# Archives
collections:
  archives:
    output: true
    permalink: /archives/:path/

Setup _archives folder

  • Create a folder _archives in your GitHub pages root.
  • Create a text file archivedata.txt with the below code.
---
---
{
"categories": [
    {%- for category in site.categories -%}
    "{{ category[0]}}"{% unless forloop.last %},{% endunless %}
    {%- endfor -%}
],
"tags": [
    {%- for tag in site.tags -%}
    "{{ tag[0] }}"{% unless forloop.last %},{% endunless %}
    {%- endfor -%}
],
"years": [
    {%- for post in site.posts -%}
    "{{ post.date | date: "%Y" }}"{% unless forloop.last %},{% endunless %}
    {%- endfor -%}
]
}

_archives folder setup

Test the setup

  • Build your site and see if you can see the archive data by navigating to your site. (yoursite.com/archives/archivedata)

  • You should see a json structure like the below one.

{
    "categories": [
        "Software Testing",
        "Excel",
        "Blogging",
        "Programming",
        "Quiz",
        "Photography",
        "RPA"
    ],
    "tags": [
        "Automation Testing",
        "UFT",
        "QTP",
        "Excel VBA"
    ],
    "years": [
        "2020",
        "2020",
        "2019",
        "2018",
        "2017"
    ]
}

File was formatted for better reading. This will appear minified on your site.

Setup _layouts folder

  • Create 3 layouts in the _layouts folder.
    • archive-categories.html
    • archive-tags.html
    • archive-years.html

Layouts folder setup

Sample layouts and files are present in the folder blog-files of this repository. If you are using it make sure to include a file from _includes folder too.

Do not rename the files in the _layouts folder.


2. Setup a new action

  • Got to your blog repository.
  • Create a folder named .github and create workflows folder inside it if it doesn't exist.
  • Create a new file named add_archives.yml in the workflows folder. You can name it anything you want.
  • Add the following code inside the file.
name: Generate Jekyll Archives
# description: Generate categories, tags and years archive files.
on:
  workflow_dispatch:
  push:
    paths:
      - "_posts/**"

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Jekyll Blog Archive
        uses: kannansuresh/[email protected]
        with:
          archive_url: "https://yoursite.com/archives/archivedata"
          archive_folder_path: "_archives"

      - name: setup git config
        run: |
          git config user.name "GitHub Actions Bot"
          git config user.email "<>"

      - name: commit
        run: |
          git add --all
          git commit -m "Created and updated archive files." || echo "No changes to commit."
          git push origin master || echo "No changes to push."

Variables

Variable Name Description Required
archive_url Your blog's archive data URL. e.g. yoursite.com/archives/archivedata Yes
archive_folder_path Path to your _archives folder. Default value _archives Yes

In the code above, make sure to change the variable archive_url to your site's archive data URL.

By default, the code pushes changes to master branch. Change the code if you want the changes to be pushed to a different branch.

The action is set to run every time a commit happens in your _posts folder.

Archive files created by action

  • To trigger the action manually
    • Navigate to Actions tab.
    • Select Generate Jekyll Archives.
    • Select Run workflow and run it.
    • Wait for the run to complete.
    • After a successful run, navigate to _archives folder and you will see the archive files generated.

To view the archives on your site, use the following URLs.

  • For categories: yoursite.com/category/category_name
  • For tags: yoursite.com/tag/tag_name
  • For categories: yoursite.com/year/2020

See it in action

I have implemented this on my website Aneejian

See the below URLs.

jekyll-blog-archive-workflow's People

Contributors

kannansuresh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

jekyll-blog-archive-workflow's Issues

Action fails on cold start

I have set up a new blog in https://github.com/penguineer/gh-blog and am using this action to create archives. This works well in my personal blog (private repository), however, I have just experienced an issue that I attribute to the fact that the structure is not yet set up (cold start).

The action fails with the message:

Traceback (most recent call last):
  File "/generatearchiviefiles/dist/_create-archive-files.py", line 96, in <module>
    all_files = os.listdir(archive_folder_path + '/' + archive_type)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '_archives/tags'

The instructions do not call for creating these directories and it makes sense to only have to set up the bare minimum.
I propose a code fix that will create these directories if they do not already exist.

Not every tag is archived

https://thenewworldpost.com/archives/archivedata/
Tag "navel" is present in archivedata file however it is not created...

https://thenewworldpost.com/tag/navel
404 error

https://github.com/tblobaum/thenewworldpost.com/runs/5520033893?check_suite_focus=true
"navel" tag does not create a file

after making moving a few random characters the build process is run again and "navel" now appears
https://github.com/tblobaum/thenewworldpost.com/runs/5520107452?check_suite_focus=true

it looks like a lot of new tags were created on the second run
https://github.com/tblobaum/thenewworldpost.com/tree/gh-pages/_archives/tags

you would think since the archives build processed the tag "navel" that it would now be available on the website but it is not
https://thenewworldpost.com/tag/navel

thank you for an otherwise awesome project

Versioning "title" had written wrong in last release.

Hi,

Till version number 1.0.3, you have used convention as "v1.0.2", but your latest version is "1.0.3" without "v". So please, create new release as "v1.0.3" or modify "@v1.0.3" to "@1.0.3" in samples you provide.

Anyway, thank you for this action ๐Ÿ‘

JSON Error

I'm getting a json error I don't know how to fix. I tried following all the instructions, but I get this output during the action build:
JSON Error. Check input variable archive_url. Passed variable [(my website url).](my website url) Expecting value: line 1 column 1 (char 0)

How do I resolve this? Note: I replaced the url the console wrote with (my website url) for privacy.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.