GitHub repo 文件夹中有以下工作流文件,.github/workflows
运行良好:
# .github/workflows/markdown-lint.yml
name: markdown-lint
run-name: learning github action from workflow
on:
push:
branches:
- main
- develop
jobs:
markdown-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Lint Markdown files
run: |
npx markdownlint-cli '**/*.md' \
--config '.github/workflows/.markdownlint.json' \
--ignore '.github/CONTRIBUTING.md' \
--ignore '.github/CODE_OF_CONDUCT.md'\
--ignore 'CHANGELOG.md'
现在我想分离run
命令部分以便我们可以重复使用,因此创建一个文件夹./github/actions/md-lint/
并放入以下代码:
# .github/actions/md-lint/action.yml
run: |
npx markdownlint-cli '**/*.md' \
--config '.github/workflows/.markdownlint.json' \
--ignore '.github/CONTRIBUTING.md' \
--ignore '.github/CODE_OF_CONDUCT.md'\
--ignore 'CHANGELOG.md'
并将其添加到markdown-lint.yml
文件如下:
- name: Lint Markdown files
uses: ././github/actions/md-lint/
但它不起作用,它会引发一个错误:
markdown-lint
Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under '/home/runner/work/template/template/.github/actions/md-lint/action.yml'. Did you forget to run actions/checkout before running your local action?
还composite
按照文档进行了尝试:
# .github/actions/md-lint/action.yml
runs:
using: 'composite'
run: |
npx markdownlint-cli '**/*.md' \
--config './github/actions/.markdownlint.json' \
--ignore '.github/CONTRIBUTING.md' \
--ignore '.github/CODE_OF_CONDUCT.md'\
--ignore 'CHANGELOG.md'
但它给出了以下错误
Error: /home/runner/work/template/template/./.github/actions/md-lint/action.yml (Line: 1, Col: 6): Unexpected value ''
Error: System.ArgumentException: Unexpected type 'NullToken' encountered while reading 'runs'. The type 'MappingToken' was expected.
at GitHub.DistributedTask.ObjectTemplating.Tokens.TemplateTokenExtensions.AssertMapping(TemplateToken value, String objectDescription)
at GitHub.Runner.Worker.ActionManifestManager.ConvertRuns(IExecutionContext executionContext, TemplateContext templateContext, TemplateToken inputsToken, String fileRelativePath, MappingToken outputs)
at GitHub.Runner.Worker.ActionManifestManager.Load(IExecutionContext executionContext, String manifestFile)
Error: Failed to load /home/runner/work/template/template/./.github/actions/md-lint/action.yml
我们如何通过将所有运行命令分别保存在actions/
文件夹中并在workflows/
文件中添加引用来实现这一点?
考虑到您正在检出您的存储库,并且该操作位于同一存储库中,因此可以通过相对路径引用您的操作。但是,需要进行一些更改才能定义和调用您的操作。
工作流程 - ././github 中的拼写错误:
操作 - 描述是必需的,外壳是必需的,并且缩进必须遵循操作模式:
您可能需要考虑将操作目录放在项目根目录下。这将简化相对路径: