In a github actions workflow, is there a way to have multiple jobs reuse the same setup? Ask Question

In a github actions workflow, is there a way to have multiple jobs reuse the same setup? Ask Question

I recently hooked up my project with github actions for continuous integration. I created two separate jobs: the first one checks if the code in the pull request is accepted by our linter, and the second one checks if the code passes the test suite. I like that having two jobs like this shows up as two separate checkmarks in the Github webpage for the pull request:

ここに画像の説明を入力してください

The problem I'm having now is that there is some ワークフロー YAML ファイル内の重複コード: LuaとLuarocksをインストールする最初の3つのステップ。メンテナンスが面倒なだけでなく、CI分を無駄にする同じアクションを2回実行することで、これを回避する方法はありますか?セットアップコードは1か所にのみ記述され、1回のみ実行されます。ワークフローはいつ実行されますか?

しかし、どのように進めればよいのかわかりません。

  1. 共有セットアップコードを使用して独自の Github Action を作成する必要がありますか?
  2. Lua と Luarocks がプリインストールされている Docker イメージを作成する必要がありますか?
  3. 単一のジョブを使用する必要がありますか? 同じジョブのステップである場合、リンターとテスト スイートに独立したチェックマークを付けることはできますか?
  4. 他に何かありますか?

私のワークフローの現在の YAML ファイルは次のとおりです。

name: Github Actions CI

on: [ pull_request ]

jobs:
    lint:
        name: Lint
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v2
            - uses: leafo/[email protected]
            - uses: leafo/[email protected]

            - run: luarocks install luacheck
            - run: ./run-linter.sh

    test:
        name: Test
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v2
            - uses: leafo/[email protected]
            - uses: leafo/[email protected]

            - run: luarocks install busted
            - run: ./build-project.sh
            - run: ./run-test-suite.sh

同様の質問を検索してみましたが、私の質問に正確に答えるものは見つかりませんでした。

  1. GitHub Actions ワークフローで APT パッケージをキャッシュする: 使用しているすべての依存関係のすべてのバージョンを正確に指定してキャッシュする方法がないため、このソリューションは使用できません。ワークフローの個別の実行がキャッシュされなくてもかまいません。コードの重複の方が心配です。
  2. Github アクションはジョブ間でワークスペース/成果物を共有しますか?成果物を別のサービスにアップロードし、その後削除する作業を管理する必要はありません。
  3. ジョブ間で github アクションの一部を再利用する: この質問では、ジョブ間の唯一の違いは単一の変数であるため、受け入れられる回答はビルド マトリックスを使用することです。ただし、セットアップ コードのみが同じである私のケースでは、ビルド マトリックスはうまく機能しないと思います。

ベストアンサー1

本日現在(2021年8月)複合アクションは に限定されなくなりましたrunGitHub Actions: アクション合成による重複の削減

name: "Publish to Docker"
description: "Pushes built artifacts to Docker"

inputs:
  registry_username:
    description: “Username for image registry”
    required: true
  registry_password:
    description: “Password for image registry”
    required: true

runs:
  using: "composite"
  steps:
      - uses: docker/setup-buildx-action@v1

      - uses: docker/login-action@v1
        with:
          username: ${{inputs.registry_username}}
          password: ${{inputs.registry_password}}

      - uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          tags: user/app:latest

古い回答

あなたが探しているのは複合アクション一度定義した一連の手順を再利用するのに役立ちます。

jobs:
    lint:
        name: Lint
        runs-on: ubuntu-latest
        steps:
            - uses: octocat/say-hello@v1

            - run: luarocks install luacheck
            - run: ./run-linter.sh

    test:
        name: Test
        runs-on: ubuntu-latest
        steps:
            - uses: octocat/say-hello@v1

            - run: luarocks install busted
            - run: ./build-project.sh
            - run: ./run-test-suite.sh

octocat/say-hello/アクション.yml:

runs:
  using: "composite"
  steps: 
    - run: echo "Nice to meet you!"
      shell: pwsh

詳細については、ADRもご確認ください。ここ

また、各ジョブが異なるマシンで実行される可能性があるため、すべてのジョブに対してこれを 1 回だけ実行できない理由も説明します。

ジョブとは、同じランナーで実行される一連のステップです。デフォルトでは、複数のジョブを含むワークフローは、それらのジョブを並行して実行します。また、ジョブを順番に実行するようにワークフローを構成することもできます。たとえば、ワークフローに、コードをビルドしてテストする 2 つの連続ジョブを含めることができます。この場合、テスト ジョブはビルド ジョブのステータスに依存します。ビルド ジョブが失敗すると、テスト ジョブは実行されません。

おすすめ記事