套娃:在一个GitHub Action中触发另一个GitHub Action

在一个GitHub Action中触发另一个GitHub Action

目录

简介

本文讲解如何在一个GitHub Action中触发另一个GitHub Action,也可以称为GitHub Action套娃。 演示网站为

https://www.xuekaiyuan.com/

本文发表在

https://blog.csdn.net/hu_zhenghui/article/details/128741195

仓库

xuekaiyuan-com/xuekaiyuan-com.github.io仓库

仓库位于https://github.com/xuekaiyuan-com/xuekaiyuan-com.github.io/

相关版本为

v0.3

详见https://www.xuekaiyuan.com/posts/xuekaiyuan/readme/

huzhenghui/posts仓库

仓库位于https://github.com/huzhenghui/posts/

相关版本为

v0.1

版本

v0.1

初始版本,位置在

https://github.com/huzhenghui/posts/tree/v0.1

仓库结构

命令

tree -a -F -I '.git/'

xuekaiyuan-com/xuekaiyuan-com.github.io仓库结构

./
├── .github/
│   └── workflows/
│       └── hugo.yml
├── .gitmodules
└── content/
    └── huzhenghui/
        ├── .git
        ├── .github/
        │   └── workflows/
        │       └── main.yml
        ├── GitHub/
        │   └── README.md
        ├── README.md -> ./GitHub/README.md
        └── _index.md

huzhenghui/posts仓库结构

./
├── .github/
│   └── workflows/
│       └── main.yml
├── GitHub/
│   └── README.md
├── README.md -> ./GitHub/README.md
└── _index.md

3 directories, 4 files

xuekaiyuan-com/xuekaiyuan-com.github.io相关文件

xuekaiyuan-com/xuekaiyuan-com.github.iogit submodule(子模块)

https://github.com/xuekaiyuan-com/xuekaiyuan-com.github.io/中, 运行如下命令将https://github.com/huzhenghui/posts/添加为子模块

git submodule add https://github.com/huzhenghui/posts content/huzhenghui

命令中https://github.com/huzhenghui/posts为子模块仓库的位置, content/huzhenghui为添加的位置,添加后可以看到git submodule(子模块)的配置文件

https://github.com/xuekaiyuan-com/xuekaiyuan-com.github.io/blob/master/.gitmodules

[submodule "content/huzhenghui"]
 path = content/huzhenghui
 url = https://github.com/huzhenghui/posts

子模块添加在https://github.com/xuekaiyuan-com/xuekaiyuan-com.github.io/tree/master/content, 其中可以看到huzhenghui,单击即可进入https://github.com/huzhenghui/posts/

xuekaiyuan-com/xuekaiyuan-com.github.ioGitHub Action

基于GitHub自动创建的Hugo Action,位置在

https://github.com/xuekaiyuan-com/xuekaiyuan-com.github.io/blob/master/.github/workflows/hugo.yml

Since v0.1 使用GitHub自动创建的Hugo Action

https://github.com/xuekaiyuan-com/xuekaiyuan-com.github.io/blob/v0.1/.github/workflows/hugo.yml

v0.3 响应https://github.com/huzhenghui/posts/GitHub Action触发的事件,并增加git submodule(子模块)自动更新。

https://github.com/xuekaiyuan-com/xuekaiyuan-com.github.io/blob/v0.3/.github/workflows/hugo.yml

其中在on中增加响应repository_dispatch事件,repository_dispatch事件在huzhenghui/postsGitHub Action文件中触发。

 on:
   # Runs on pushes targeting the default branch
   push:
     branches: ["master"]
 
   # Allows you to run this workflow manually from the Actions tab
   workflow_dispatch:
+  repository_dispatch:

jobsCheckout之后Setup Pages之前增加Update module

       - name: Checkout
         uses: actions/checkout@v3
         with:
           submodules: recursive
+      - name: Update module
+        run: git submodule update --remote
       - name: Setup Pages
         id: pages
         uses: actions/configure-pages@v2

huzhenghui/postsGitHub Action相关设置与文件

创建Personal access token

访问https://github.com/settings/tokens,创建Personal access token

Note设置为易于记住的内容,例如

https://github.com/huzhenghui/posts trigger https://github.com/xuekaiyuan-com/xuekaiyuan-com.github.io GitHub Action

Select scopes中选中repo,创建后记录值,将在设置Actions secrets中使用。

设置Actions secrets

访问https://github.com/huzhenghui/posts/settings/secrets/actions, 创建repository secret

Name设置为便于在GitHub Action中使用的名字,例如TRIGGER_XUEKAIYUAN_COM, 将在huzhenghui/postsGitHub Action文件中使用。

Secret中为创建Personal access token记录的值。

huzhenghui/postsGitHub Action文件

手工创建,位置在

https://github.com/huzhenghui/posts/blob/master/.github/workflows/main.yml

Sincev0.1

https://github.com/huzhenghui/posts/blob/v0.1/.github/workflows/main.yml

name: Trigger https://github.com/xuekaiyuan-com/xuekaiyuan-com.github.io GitHub Action

on:
  # Runs on pushes targeting the default branch
  push:
    branches: ["master"]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# Default to bash
defaults:
  run:
    shell: bash

jobs:
  # Trigger job
  trigger:
    runs-on: ubuntu-latest
    steps:
      - name: POST https://api.github.com/repos/xuekaiyuan-com/xuekaiyuan-com.github.io/dispatches
        run: |
          curl \
            -X POST \
            -H "Accept: application/vnd.github+json" \
            -H "Authorization: Bearer ${{ secrets.TRIGGER_XUEKAIYUAN_COM }}"\
            -H "X-GitHub-Api-Version: 2022-11-28" \
            https://api.github.com/repos/xuekaiyuan-com/xuekaiyuan-com.github.io/dispatches \
            -d '{"event_type":"trigger by https://github.com/huzhenghui/posts/","client_payload":{"unit":false,"integration":true}}'          

其中onGitHub Action响应的事件。

on:
  # Runs on pushes targeting the default branch
  push:
    branches: ["master"]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

其中defaults设置了默认环境,这里默认使用bash运行。

# Default to bash
defaults:
  run:
    shell: bash

jobs中为运行的任务。

jobs:
  # Trigger job
  trigger:
    runs-on: ubuntu-latest
    steps:
      - name: POST https://api.github.com/repos/xuekaiyuan-com/xuekaiyuan-com.github.io/dispatches
        run: |
          curl \
            -X POST \
            -H "Accept: application/vnd.github+json" \
            -H "Authorization: Bearer ${{ secrets.TRIGGER_XUEKAIYUAN_COM }}"\
            -H "X-GitHub-Api-Version: 2022-11-28" \
            https://api.github.com/repos/xuekaiyuan-com/xuekaiyuan-com.github.io/dispatches \
            -d '{"event_type":"trigger by https://github.com/huzhenghui/posts/","client_payload":{"unit":false,"integration":true}}'          

其中只有一个任务trigger, 任务中只有一个步骤POST https://api.github.com/repos/xuekaiyuan-com/xuekaiyuan-com.github.io/dispatches, 运行的命令为

        run: |
          curl \
            -X POST \
            -H "Accept: application/vnd.github+json" \
            -H "Authorization: Bearer ${{ secrets.TRIGGER_XUEKAIYUAN_COM }}"\
            -H "X-GitHub-Api-Version: 2022-11-28" \
            https://api.github.com/repos/xuekaiyuan-com/xuekaiyuan-com.github.io/dispatches \
            -d '{"event_type":"trigger by https://github.com/huzhenghui/posts/","client_payload":{"unit":false,"integration":true}}'          

其中

运行后将触发xuekaiyuan-com/xuekaiyuan-com.github.ioGitHub Action

huzhenghui/posts的内容

/_index.md

索引页,内容显示在

https://www.xuekaiyuan.com/huzhenghui/

文件位置在

https://github.com/huzhenghui/posts/blob/master/_index.md

Since v0.1

https://github.com/huzhenghui/posts/blob/v0.1/_index.md

---
title: 胡争辉
date: 2023-01-20
publishdate: 2023-01-20
---

<!-- markdownlint-disable-next-line MD025 -->
# 胡争辉

<https://github.com/huzhenghui/posts/>

/GitHub/README.md

本文件,位置在

https://github.com/huzhenghui/posts/blob/master/GitHub/README.md

Since v0.1

https://github.com/huzhenghui/posts/blob/v0.1/GitHub/README.md

/README.md

软链接,链接到/GitHub/README.md

https://github.com/huzhenghui/posts/blob/master/README.md

Since v0.1

https://github.com/huzhenghui/posts/blob/v0.1/README.md