Can GitHub Actions still play like this?

Recently, a very interesting Workflow has been implemented, which is to automatically synchronize each newly published article to my GitHub home page through GitHub Actions.

Just like this, the latest blog posts are displayed on the home page.

To implement such a workflow, you need to understand the following:

  1. You need to create a personal warehouse with the same name as GitHub, and the README.md information of this warehouse will be displayed on the home page
  2. Get the latest blog posts automatically through GitHub Actions and update README.md
  3. The GitHub Action for automatically obtaining and updating articles is triggered only when a new article is published

The personal warehouse with the same name as GitHub is a special warehouse, that is, create a warehouse with the same name as your GitHub account, and the added README.md will be displayed on the GitHub personal home page.

For example: if your GitHub is called GeBiLaoWang, when you create a Git warehouse called GeBiLaoWang, it will be displayed on the home page after adding README.md.

For this function, there are many colorful personal introductions on GitHub (as follows). For more inspiration, see this link: https://awesomegithubprofile.tech/

Automatically get articles and update README.md

On GitHub, many developers develop new small functions for GitHub Actions. I use an open source project called blog post workflow, which can get the latest blog articles through RSS (subscription source).

It supports not only RSS, but also access to resources such as StackOverflow and Youtube Videos.

I just need to add such a workflow YML. GitHub / workflows / blog-post-workflow.yml under the warehouse with the same name as GitHub.

name: Latest blog post workflow
on:
  schedule:
    - cron: '* 2 * * *'
  workflow_dispatch:

jobs:
  update-readme-with-blog:
    name: Update this repo's README with latest blog posts
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: gautamkrishnar/blog-post-workflow@master
        with:
          # My blog RSS link
          feed_list: "https://shenxianpeng.github.io/atom.xml"
          # Get the latest 10 articles
          max_post_count: 10

At first, I need to make this Workflow work. Therefore, the timed trigger used is to automatically obtain the latest articles at two o'clock every morning and update this special warehouse README.md.

This approach is OK, but it is not enough to save resources and is not perfect. The best practice is to trigger the above Workflow update README.md only when a new article is published. This requires a Webhook to automatically trigger the Workflow here when an article update is detected.

Trigger another GitHub Action

GitHub Actions provides a Webhook event called repository_dispatch can do this.

Its principle: use GitHub API to trigger a Webhook event, which is called repository_dispatch, the type in this event can be customized, and repository needs to be used in the workflow to be triggered_ Dispatch event.

That is, there should be a Workflow in the warehouse where blog posts are stored by sending a repository_ The dispatch event triggers the Workflow in the special warehouse to update README.md.

Here I define the event type as special_repository, which only accepts repositories from GitHub API_ Dispatch event.

Adjust the above. GitHub / workflow / blog-post-workflow.yml file again as follows:

# special_repository.yml
name: Latest blog post workflow

on:
  repository_dispatch:
    # The type here can be customized. I call it special_repository
    types: [special_repository]
  workflow_dispatch:

jobs:
  update-readme-with-blog:
    name: Update this repo's README with latest blog posts
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: gautamkrishnar/blog-post-workflow@master
        with:
          feed_list: "https://shenxianpeng.github.io/atom.xml"
          max_post_count: 10

The Workflow that accepts the event has been modified. How to send special_repository of repository_ What about the dispatch event? I call the API directly through curl.

curl -XPOST -u "${{ secrets.PAT_USERNAME}}:${{secrets.PAT_TOKEN}}" \
    -H "Accept: application/vnd.github.everest-preview+json" \
    -H "Content-Type: application/json" https://api.github.com/repos/shenxianpeng/shenxianpeng/dispatches \
    --data '{"event_type": "special_repository"}'

Finally, send the event workflow YML. GitHub / workflows / send-dispatch.yml as follows:

name: Tigger special repository

on:
  push:
    # The workflow is triggered when the master branch is changed
    branches:
      - master
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Send repository dispatch event
        run: |
          curl -XPOST -u "${{ secrets.PAT_USERNAME}}:${{secrets.PAT_TOKEN}}" \
          -H "Accept: application/vnd.github.everest-preview+json" \
          -H "Content-Type: application/json" https://api.github.com/repos/shenxianpeng/shenxianpeng/dispatches \
          --data '{"event_type": "special_repository"}'

Note: PAT_USERNAME and PAT_TOKEN needs to be added in the current warehouse settings - > secrets, which will not be described in detail here. You can search by yourself.

The above is achieved through GitHub Actions. When the blog has a newly published article, it will automatically update all the contents of the GitHub home page.

If there are any interesting ways to play, please share them in the comment area.

Posted on Fri, 03 Dec 2021 05:33:52 -0500 by richei