减少 Github 操作中的重复

Reduce Duplications in Github Actions

我已经阅读并测试了不同的东西,但没有找到解决方案。也许我不得不接受样板文件,但我还没有完成尝试。

我有以下 github 操作工作流程:

---
name: Code Quality Checks

on:
  push:
    branches: [main, development]
  pull_request:
    branches: [main, development]

jobs: 
  test:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - name: get repo
        uses: actions/checkout@v2
      - name: Set up Python 3.7
        uses: actions/setup-python@v1
        with:
          python-version: 3.7
      - name: Install poetry
        uses: snok/install-poetry@v1.0.0
        with:
          virtualenvs-create: true
          virtualenvs-in-project: true
      - name: Load cached venv
        id: cached-poetry-dependencies
        uses: actions/cache@v2
        with:
          path: .venv
          key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
      - name: Install dependencies
        run: poetry install
        if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
      - name: Unit & Coverage test with pytest
        run: poetry run pytest

  check:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - name: get repo
        uses: actions/checkout@v2
      - name: Set up Python 3.7
        uses: actions/setup-python@v1
        with:
          python-version: 3.7
      - name: Install poetry
        uses: snok/install-poetry@v1.0.0
        with:
          virtualenvs-create: true
          virtualenvs-in-project: true
      - name: Load cached venv
        id: cached-poetry-dependencies
        uses: actions/cache@v2
        with:
          path: .venv
          key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
      - name: Install dependencies
        run: poetry install
        if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
      - name: Check style with flake8
        run: poetry run flake8 boxsup_pytorch/ tests/

如您所见,这两项工作中都有很多样板文件。我测试了 'composition' 缓存变量有问题。我还修补了上传和下载工件,但由于 setup-python 操作在我的 cwd 之外使用 python 二进制文件,我认为上传完整的运行程序不是一个好主意。

我的场景有任何可行的解决方案吗?

我之前的想法主要是基于

您要查找的是 reusable workflow 关于 Github 操作的概念。

Reusing workflows avoids duplication. This makes workflows easier to maintain and allows you to create new workflows more quickly by building on the work of others, just as you do with actions. Workflow reuse also promotes best practice by helping you to use workflows that are well designed, have already been tested, and have been proved to be effective.

请注意限制:

  • 可重用工作流不能调用其他可重用工作流。
  • 存储在私有存储库中的可重用工作流只能由同一存储库中的工作流使用。
  • 在调用者工作流的工作流级别定义的 env 上下文中设置的任何环境变量都不会传播到被调用的工作流。
  • 任何调用可重用工作流的作业都不支持策略 属性。

这里是the documentation step by step to create a reusable workflow.

编辑:如果您想知道复合操作和可重用工作流之间的区别是什么,我建议您阅读此 Github blog post

您可以使用将缓存键作为输入的复合操作;像这样的东西(截至今天更新了动作版本):

name: Install Python and Poetry

inputs:
  python-version:
    description: Python version
    required: false
    default: 3.7
  cache-key:
    description: Key to use for cache action
    required: true

runs:
  using: composite
  steps:
    - name: Set up Python
      uses: actions/setup-python@v3.1.2
      with:
        python-version: ${{ inputs.python-version }}
    - name: Install poetry
      uses: snok/install-poetry@v1.3.1
      with:
        virtualenvs-in-project: true
    - name: Load cached venv
      id: cache
      uses: actions/cache@v3.0.2
      with:
        path: .venv
        key: ${{ inputs.cache-key }}
    - name: Install dependencies
      if: '! steps.cache.outputs.cache-hit'
      shell: bash
      run: poetry install

它取代了作业中的步骤 2-5。它还允许您指定 Python 版本,但在省略时默认为 3.7。

工作流程将如下所示:

name: Code Quality Checks

on:
  push:
    branches:
      - main
      - development
  pull_request:
    branches:
      - main
      - development

jobs:
  test:
    runs-on: ubuntu-20.04
    needs: setup
    steps:
      - name: Get repo
        uses: actions/checkout@v3.0.2
      - name: Install Python and Poetry
        uses: ./.github/actions/poetry
        with:
          cache-key: env-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
      - name: Unit & coverage test with pytest
        run: poetry run pytest

  check:
    runs-on: ubuntu-20.04
    needs: setup
    steps:
      - name: Get repo
        uses: actions/checkout@v3.0.2
      - name: Install Python and Poetry
        uses: ./.github/actions/poetry
        with:
          cache-key: env-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
      - name: Check style with flake8
        run: poetry run flake8 boxsup_pytorch/ tests/

假设上面的 action.yml 位于 .github/actions/poetry,即 .github 目录如下所示:

.github
├── actions
│   └── poetry
│       └── action.yml
└── workflows
    └── checks.yml