GitHub 操作“扩展”现有作业

时间:2021-01-01 18:57:30

标签: github github-actions

我想要一些 github 操作工作流,它应该检查 linting,检查构建代码是否成功,如果是,则运行测试。

我的第一个“工作”是安装依赖项。每项工作都需要完成,所以我在每项工作中都做到以下几点:

- uses: actions/checkout@v2

- name: Cache functions node_modules
  uses: actions/cache@v2
  with:
    path: node_modules
    key: ${{ runner.os }}-${{ hashFiles('package.json') }}

- name: Install dependencies
  run: npm ci

有没有办法将其定义为工作 dependencies,然后再拥有另一份工作 lint 扩展此工作?有缓存,所以不会重新安装,但感觉就像无缘无故的长代码重复。

enter image description here

1 个答案:

答案 0 :(得分:2)

如果您想在此处应用 DRY 规则,您应该选中 composite run steps

<块引用>

您现在可以使用 shell 脚本创建可重复使用的操作,甚至可以在同一个操作中混合使用多种 shell 语言。您可能有很多 shell 脚本来自动执行许多任务,现在您可以轻松地将它们转换为一个操作并在不同的工作流中重复使用它们。

您可以按如下方式使用它:

主文件:

jobs:
  build:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v2
    - uses: octocat/say-hello@v1
      with: 
        os: ${{ runner.os }}

octocat/say-hello/action.yml

inputs:
  name: 
    os: 'Your os'
    default: 'No os default'
runs:
  using: "composite"
  steps: 
    - uses: actions/checkout@v2

    - name: Cache functions node_modules
      uses: actions/cache@v2
      with:
        path: node_modules
        key: ${{ inputs.os }}-${{ hashFiles('package.json') }}

    - name: Install dependencies
      run: npm ci

如果您打算将复合材料保存在与主工作流相同的存储库中,则将其称为

    - uses: ./.github/actions/say-hello

对于此文件夹结构:

enter image description here