Skip to content

Latest commit

 

History

History
254 lines (210 loc) · 9.44 KB

README.md

File metadata and controls

254 lines (210 loc) · 9.44 KB

Metadata post action

A GitHub action that creates a post from data files generated by read-action, bookmark-action, and spotify-to-yaml-action.

If you're including playlist data generated by the spotify-to-yaml-action, make sure it's schedule to run before this action.

Set up the workflow

To use this action, create a new workflow in .github/workflows and modify it as needed:

name: Create metadata post

# Grant the action permission to write to the repository
permissions:
  contents: write

on:
  workflow_dispatch:
    inputs:
      post-title:
        description: The title of the post. This can be set as an action input or workflow input.
      start-end:
        description: The start date for the post. The format is `YYYY-MM-DD`. This can be set as an action input or workflow input.
      end-date:
        description: The end date for the post. The format is `YYYY-MM-DD`. This can be set as an action input or workflow input.

jobs:
  metadata-post:
    runs-on: ubuntu-latest
    name: Write metadata post
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Write metadata post
        uses: library-pals/[email protected]
        with:
          github-username: library-pals
          github-repository: sample-site
          source-bookmarks: recipes|_data/recipes.json
        env:
          TOKEN: ${{ secrets.TOKEN }}
      - name: Commit files
        run: |
          git pull
          git config --local user.email "[email protected]"
          git config --local user.name "GitHub Action"
          git add -A && git commit -m "${{ env.title }}"
          git push

Additional example workflows

Seasonal metadata post
name: Seasonal metadata post

# Grant the action permission to write to the repository
permissions:
  contents: write

on:
  workflow_dispatch:
  schedule:
    - cron: "00 02 20 Mar,Jun,Sep,Dec *"

jobs:
  metadata-post:
    runs-on: ubuntu-latest
    name: Write metadata post
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Set post title and dates
        id: set-season
        run: |
          # Get the current month and year
          MONTH=$(date +%m)
          YEAR=$(date +%Y)

          # Define the seasons and corresponding emojis
          declare -A SEASONS=(
            ["03"]="Winter"
            ["06"]="Spring"
            ["09"]="Summer"
            ["12"]="Fall"
          )

          declare -A SEASON_EMOJI=(
            ["Winter"]="❄️"
            ["Spring"]="🌷"
            ["Summer"]="☀️"
            ["Fall"]="🍂"
          )

          # Function to set environment variables based on the season
          set_environment_variables_for_season() {
            local season=$1
            local start_date=$2
            local end_date=$3
            local post_title=""

            if [ "$season" = "Winter" ]; then
              post_title="$(($YEAR - 1))/${YEAR} ${season}"
            else
              post_title="${YEAR} ${season}"
            fi

            echo "POST_TITLE=${post_title}" >> $GITHUB_OUTPUT
            echo "START_DATE=${start_date}" >> $GITHUB_OUTPUT
            echo "END_DATE=${end_date}" >> $GITHUB_OUTPUT
            echo "SEASON_EMOJI=${SEASON_EMOJI[$season]}" >> $GITHUB_OUTPUT
          }

          # Set environment variables based on the current month
          case $MONTH in
            "03")
              set_environment_variables_for_season ${SEASONS[$MONTH]} "$(($YEAR - 1))-12-21" "${YEAR}-03-20"
              ;;
            "06")
              set_environment_variables_for_season ${SEASONS[$MONTH]} "${YEAR}-03-21" "${YEAR}-06-20"
              ;;
            "09")
              set_environment_variables_for_season ${SEASONS[$MONTH]} "${YEAR}-06-21" "${YEAR}-09-20"
              ;;
            "12")
              set_environment_variables_for_season ${SEASONS[$MONTH]} "${YEAR}-09-21" "${YEAR}-12-20"
              ;;
            *)
              echo "Invalid month: $MONTH" >&2
              exit 1
              ;;
          esac
      - name: Write metadata post
        uses: library-pals/[email protected]
        with:
          github-username: library-pals
          github-repository: sample-site
          source-bookmarks: recipes|_data/recipes.json
          book-tags: "recommend,skip"
          start-date: ${{ steps.set-season.outputs.START_DATE }}
          end-date: ${{ steps.set-season.outputs.END_DATE }}
          post-title: ${{ steps.set-season.outputs.POST_TITLE }}
        env:
          TOKEN: ${{ secrets.TOKEN }}
      - name: Commit files
        run: |
          git pull
          git config --local user.email "[email protected]"
          git config --local user.name "GitHub Action"
          git add -A && git commit -m "${{steps.set-season.outputs.SEASON_EMOJI}} ${{ steps.set-season.outputs.POST_TITLE }}"
          git push
Uses a custom markdown template with post-template and sets the posts-directory
name: Uses a custom markdown template with post-template and sets the posts-directory

# Grant the action permission to write to the repository
permissions:
  contents: write

on:
  workflow_dispatch:
    inputs:
      start-date:
        description: "The start date for the post in the format YYYY-MM-DD"
        type: string
        required: true
      end-date:
        description: "The end date for the post in the format YYYY-MM-DD"
        type: string
        required: true
      post-title:
        description: "The title of the post"
        type: string
        required: true

jobs:
  metadata-post:
    runs-on: ubuntu-latest
    name: Write metadata post
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Write metadata post
        uses: library-pals/[email protected]
        with:
          github-username: library-pals
          github-repository: sample-site
          post-template: .github/actions/post-template-basic.md
          posts-directory: books/
          source-bookmarks: recipes|_data/recipes.json
        env:
          TOKEN: ${{ secrets.TOKEN }}
      - name: Commit files
        run: |
          git pull
          git config --local user.email "[email protected]"
          git config --local user.name "GitHub Action"
          git add -A && git commit -m "${{ env.title }}"
          git push

Action options

  • github-username: Required. The GitHub username that owns the repository with the data files.

  • github-repository: Required. The Github repository that has the data files.

  • posts-directory: The path to where you want to save your metadata post files to in this repository. Default: notes/_posts/.

  • post-template: If you'd like to customize the markdown template, define a path to your own. Example: post-template: .github/actions/post-template.md. The markdown template shows all the available variables and an idea for how you may want to format this file. For now, the templating is simplistic and does not offer functionality outside of this action replacing variable names.

  • source-books: Define the label and file path for the books data generated by read-action. Separate the label and file path with a pipe (|). If you do not have books, set this value to false. Note: this value will not change the variable name in the markdown template, which is bookYaml and bookMarkdown. Default: books|_data/read.json.

  • source-bookmarks: Define the label and file path for the bookmarks data generated by bookmark-action. Separate the label and file path with a pipe (|). If you do not have bookmarks, set this value to false. Note: this value will not change the variable name in the markdown template, which is bookmarkYaml and bookmarkMarkdown. Default: bookmarks|_data/bookmarks.json.

  • source-playlist: Define the file path for the playlist data generated by spotify-to-yaml-action. If you do not have playlists, set this value to false. Default: _data/playlists.yml.

  • book-tags: Allow specific tags to be passed through. Separate each tag with a comma.

  • start-date: The start date for the post. The format is YYYY-MM-DD. This can be set as an action input or workflow input.

  • end-date: The end date for the post. The format is YYYY-MM-DD. This can be set as an action input or workflow input.

  • post-title: The title of the post. This can be set as an action input or workflow input.

Trigger the action

To trigger the action, create a workflow dispatch event with the following body parameters:

{
  "ref": "main", // Required. The git reference for the workflow, a branch or tag name.
  "inputs": {
    "post-title": "", // The title of the post. This can be set as an action input or workflow input.
    "start-end": "", // The start date for the post. The format is `YYYY-MM-DD`. This can be set as an action input or workflow input.
    "end-date": "", // The end date for the post. The format is `YYYY-MM-DD`. This can be set as an action input or workflow input.
  }
}