GitHub Actionsワークフローの個人的まとめ

2025/01/22に公開

はじめに

今回はデスクトップマスコットと呼ばれるOSSプロジェクトの開発過程で、GitHub Actionsワークフローを作成した際の個人的まとめをします。

https://github.com/MidraLab/uDesktopMascot

今回は、starter-workflowsやその他盛り上がっているOSSプロジェクトのワークフローを参考にして、自分のプロジェクトに合わせてワークフローを作成していきました。

https://github.com/actions/starter-workflows/tree/main

公式のドキュメントはこちら
https://docs.github.com/ja/actions

命名規則

Job名

全て小文字ケバブケース

jobs:
  example-job:
  example-job1:

Jobの表示名

通常の文章

jobs:
  example-job:
    name: Example Job
  example-job1:
    name: Example Job1

Step表示名

通常の文章

steps:
  - name: Example step
  - name: Example step1

id名

全て小文字スネークケース

jobs:
  example-job:
    id: example_id
  example-job1:
    id: example_id1

環境変数

すべて大文字スネークケース

env:
  EXAMPLE_ENV: example_env
  EXAMPLE_ENV1: example_env1

output名

すべて小文字スネークケース

outputs:
  example_output: example_output
  example_output1: example_output1

ワークフロー中に取得した変数の受け渡し

Step間での値の受け渡し

steps:
  - name: Example step
    id: example_id
    run: echo "example_output=example_output" >> $GITHUB_OUTPUT
  - name: Example step1
    run: echo "example_output: ${{ steps.example_id.outputs.example_output }}"

Job間での値の受け渡し

jobs:
  example-job:
    outputs:
      example_output: ${{ steps.example_id.outputs.example_output }}
    steps:
      - name: Example step
        id: example_id
        run: echo "example_output=example_output" >> $GITHUB_OUTPUT
  example-job1:
    needs: example-job
    steps:
      - name: Example step1
        run: echo "example_output: ${{ needs.example-job.outputs.example_output }}"

環境変数

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Example step
  run: echo "example_output: ${{ env.GITHUB_TOKEN }}"

ファイルから値を取り出す

grepコマンドでファイルから値を取り出す

current_version=$(grep -m1 'bundleVersion:' ProjectSettings/ProjectSettings.asset | awk '{print $2}')
old_version=$(grep '#define MyAppVersion' Installer/setup.txt | cut -d '"' -f2)

ファイルの内容を変更したらgit pushする

pushするgit userを設定して、ファイルを追加してコミットしてpushする

- name: Push to GitHub
  run: |
   git config --global user.name "github-actions"
   git config --global user.email "github-actions@github.com"
   git add example.txt
   git commit -m "Update example file"
   git push origin HEAD:${{ github.ref }}
GitHubで編集を提案
MidraLab(ミドラボ)

Discussion