在一个GitHub Action
中触发另一个GitHub Action
目录
- 在一个
GitHub Action
中触发另一个GitHub Action
简介
本文讲解如何在一个GitHub Action
中触发另一个GitHub Action
,也可以称为GitHub Action
套娃。
演示网站为
本文发表在
https://blog.csdn.net/hu_zhenghui/article/details/128741195
仓库
xuekaiyuan-com/xuekaiyuan-com.github.io
仓库
仓库位于https://github.com/xuekaiyuan-com/xuekaiyuan-com.github.io/
相关版本为
详见https://www.xuekaiyuan.com/posts/xuekaiyuan/readme/
huzhenghui/posts
仓库
仓库位于https://github.com/huzhenghui/posts/
相关版本为
版本
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.io
的 git 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.io
的GitHub 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/posts
的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:
+ repository_dispatch:
在jobs
的Checkout
之后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/posts
的GitHub 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/posts
的GitHub Action
文件中使用。
Secret
中为创建Personal access token
记录的值。
huzhenghui/posts
的GitHub 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}}'
其中on
为GitHub Action
响应的事件。
- 响应
master
分支的push
- 响应https://github.com/huzhenghui/posts/actions页面中手工触发
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}}'
其中
${{ secrets.TRIGGER_XUEKAIYUAN_COM }}
为设置Actions secrets
的Name
。- https://api.github.com/repos/xuekaiyuan-com/xuekaiyuan-com.github.io/dispatches为https://github.com/xuekaiyuan-com/xuekaiyuan-com.github.io/的
repository_dispatch
事件的接口。 "event_type":"trigger by https://github.com/huzhenghui/posts/"
将显示在https://github.com/xuekaiyuan-com/xuekaiyuan-com.github.io/actions表示触发来源。
运行后将触发xuekaiyuan-com/xuekaiyuan-com.github.io
的GitHub 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