nhoizey / github-action-feed-to-mastodon

A GitHub Action to create toots on Mastodon from a JSON Feed
MIT License
23 stars 3 forks source link

Use a composite action to ease usage? #10

Open nhoizey opened 1 year ago

nhoizey commented 1 year ago

A composite action could maybe take care of the checkout and commit/push steps, so this:

name: Create toots from JSON Feed items
on:
  schedule:
    # Run the action every Monday at 8am
    # See https://crontab.guru/#0_8_*_*_1
    - cron: "0 8 * * 1"
  workflow_dispatch:

jobs:
  JSONFeed2Mastodon:
    runs-on: ubuntu-latest

    steps:
      # Checkout the repository to restore previous cache
      - name: Checkout
        uses: actions/checkout@v3

      # Look for new toots to create from items in the JSON feed
      - name: JSON Feed to Mastodon
        uses: nhoizey/github-action-jsonfeed-to-mastodon@v1
        with:
          feedUrl: "https://example.com/feed.json"
          mastodonInstance: "https://mastodon.social"
          mastodonToken: ${{ secrets.MASTODON_TOKEN }}

      # Push changes in the cache files to the repository
      # See https://github.com/stefanzweifel/git-auto-commit-action#readme
      - name: Commit and push
        uses: stefanzweifel/git-auto-commit-action@v4

Could become this:

name: Create toots from JSON Feed items
on:
  schedule:
    # Run the action every Monday at 8am
    # See https://crontab.guru/#0_8_*_*_1
    - cron: "0 8 * * 1"
  workflow_dispatch:

jobs:
  JSONFeed2Mastodon:
    runs-on: ubuntu-latest

    steps:
      # Look for new toots to create from items in the JSON feed
      - name: JSON Feed to Mastodon
        uses: nhoizey/github-action-jsonfeed-to-mastodon@v1
        with:
          feedUrl: "https://example.com/feed.json"
          mastodonInstance: "https://mastodon.social"
          mastodonToken: ${{ secrets.MASTODON_TOKEN }}

But composite actions have some limits.

nhoizey commented 1 year ago

Previous attempt of developing a composite action:

name: "Composite JSON Feed to Mastodon"
description: "Create messages (toots) on Mastodon from a JSON Feed items"
author: "Nicolas Hoizey"
branding:
  icon: "cast"
  color: "green"
inputs:
  feedUrl:
    description: "URL of the JSON Feed to fetch"
    required: true
  mastodonInstance:
    description: "The root URL of the Mastodon instance where the toot should be created"
    required: true
  mastodonToken:
    description: "Your access token for the Mastodon API, get it from /settings/applications/new"
    required: true
  nbTootsPerItem:
    description: "Number of toots that can be created from the same item"
    default: 1
  globalDelayToots:
    description: "Delay (in minutes) between any toot from this feed (default: 1 day)"
    default: 1440
  delayTootsSameItem:
    description: "Delay (in minutes) between any toot from this feed for the same item (default: 90 days)"
    default: 129600
  cacheFile:
    description: "Path to the JSON file caching data from the feed and toots"
    default: ".cache/jsonfeed-to-mastodon.json"
  cacheTimestampFile:
    description: "Path to the JSON file caching the timestamp of the last toot"
    default: ".cache/jsonfeed-to-mastodon-timestamp.json"
outputs:
  tootUrl: # output will be available to future steps
    description: "URL to the latest toot created"
runs:
  using: "composite"
  steps:
    - name: Checkout 🛎️
      uses: actions/checkout@v3

    - name: Set up Node.js ⚙️
      uses: actions/setup-node@v3
      with:
        node-version-file: ".nvmrc"
        cache: "npm"

    - name: Install dependencies 📦
      run: npm ci
      shell: bash

    - name: JSON Feed to Mastodon 🦣
      env:
        RUNNER_TEMPORARY_DIRECTORY: ${{ runner.temp }}
      run: node ./dist/index.js
      shell: bash
      with:
        feedUrl: ${{ inputs.feedUrl }}
        mastodonInstance: ${{ inputs.mastodonInstance }}
        mastodonToken: ${{ inputs.mastodonToken }}
        nbTootsPerItem: ${{ inputs.nbTootsPerItem }}
        globalDelayToots: ${{ inputs.globalDelayToots }}
        delayTootsSameItem: ${{ inputs.delayTootsSameItem }}
        cacheFile: ${{ inputs.cacheFile }}
        cacheTimestampFile: ${{ inputs.cacheTimestampFile }}

    - name: Pull any changes 📥
      run: git pull
      shell: bash

    - name: Commit and push 📤
      uses: stefanzweifel/git-auto-commit-action@v4
      with:
        commit_message: "chore(cache): update Mastodon cache file (automated)"
        file_pattern: "*.json"
        skip_fetch: false
lwojcik commented 1 year ago

I solved this exact problem by running my JS code manually as a step and adding necessary inputs as environment variables formatted in a way getInput from @actions/core picks them up just fine (i.e. all uppercase letters prefixed by INPUT_) -- https://github.com/lwojcik/github-action-feed-to-social-media/blob/main/action.yml

I can contribute a relevant PR to your action, please let me know if you're interested.