0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【GitHubActions】マトリックスを利用してScriptを実行する

Posted at

概要

マトリックスを利用し、冗長性のあるワークフローをすっきりさせました。
複数のシェルスクリプトを実行させたい時に利用すると良いかもしれません。

元のコード

name: Sequential Workflow

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

jobs:
  validation:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4.1.7

      - name: Set up validation script permissions
        run: chmod +x .github/scripts/validation.sh

      - name: Run validation script
        run: ./.github/scripts/validation.sh

  setup:
    runs-on: ubuntu-latest
    needs: validation
    steps:
      - name: Checkout code
        uses: actions/checkout@v4.1.7

      - name: Set up setup script permissions
        run: chmod +x .github/scripts/set-up.sh

      - name: Run setup script
        run: ./.github/scripts/set-up.sh

  deploy:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - name: Checkout code
        uses: actions/checkout@v4.1.7

      - name: Set up deploy script permissions
        run: chmod +x .github/scripts/deploy.sh

      - name: Run deploy script
        run: ./.github/scripts/deploy.sh

matrixを利用したコード

name: Matrix Workflow

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

jobs:
  example_matrix:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        script: [validation, set-up, deploy]
    steps:
      - name: Checkout code
        uses: actions/checkout@v4.1.7

      - name: Set up script permissions
        run: chmod +x .github/scripts/${{ matrix.script }}.sh

      - name: Run script
        run: ./.github/scripts/${{ matrix.script }}.sh

まとめ

    strategy:
      matrix:
        script: [validation, set-up, deploy]

上記のワークフローのリストにある validation, set-up, deploy
下記の ${{ matrix.script }} へ順番に代入し、実行しています。

    - name: Set up script permissions
      run: chmod +x .github/scripts/${{ matrix.script }}.sh

これで冗長性のあるワークフローを短くまとめることができます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?