💨
【GitHubActions】マトリックスを利用してScriptを実行する
概要
マトリックスを利用して、冗長性のあるワークフローをすっきりさせました。
元のコード
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
これで冗長性のあるワークフローを短くまとめることができます。
Discussion