はなちるのマイノート

Unityをメインとした技術ブログ。自分らしくまったりやっていきたいと思いますー!

【GitHub Actions】GitHub Actions初心者が公式ドキュメントを見ながら理解を深めていったメモ①

はじめに

今までGitHub Actionをまともに使ったことがなかった私ですが、そろそろ学んでいこうと思い公式ドキュメントを見ながら勉強し出しました。

https://docs.github.com/ja/actions/quickstartdocs.github.com

そこで学んだことをメモしておきます。

初めてのGitHub Action

GitHub Actionsの利用

まずは簡単な例を見ながら外観を掴みます。以下の3つのステップを行なってください。

  1. GitHubのリポジトリに.github/workflowsディレクトリを作成
  2. .github/workflowsディレクトリの中にgithub-actions-demo.ymlを作成
  3. 以下のコードをそこに記述する
name: GitHub Actions Demo
on: [push]
jobs:
  Explore-GitHub-Actions:
    runs-on: ubuntu-latest
    steps:
      - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
      - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
      - run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
      - name: Check out repository code
        uses: actions/checkout@v3
      - run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
      - run: echo "🖥️ The workflow is now ready to test your code on the runner."
      - name: List files in the repository
        run: |
          ls ${{ github.workspace }}
      - run: echo "🍏 This job's status is ${{ job.status }}."
CloneしたVSCodeでの様子


変更をpushすると先ほど書いたymlの中に記述したworkflowが実行されます。

結果の確認

GitHubのページにActionというタブがあるので押すと、結果を確認できます。

結果の確認

GitHubActionsとは(用語整理)

GitHub Actionsは継続的インテグレーション及び継続的デリバリ(CI/CD)プラットフォームで、ビルド、テスト、デプロイメントのパイプラインを自動化できるようになります。
You can create workflows that build and test every pull request to your repository, or deploy merged pull requests to production.
...
GitHub provides Linux, Windows, and macOS virtual machines to run your workflows, or you can host your own self-hosted runners in your own data center or cloud infrastructure.

Understanding GitHub Actions - GitHub Docs

// 英語箇所をDeepL翻訳
リポジトリへのすべてのプルリクエストをビルドしてテストするワークフローを作成したり、マージされたプルリクエストを本番環境にデプロイしたりすることができます。
...
GitHubは、ワークフローを実行するためのLinux、Windows、macOSの仮想マシンを提供しています。また、ご自身のデータセンターやクラウドインフラでセルフホスティングのランナーをホストすることも可能です。

GitHub Actions workflow

You can configure a GitHub Actions workflow to be triggered when an event occurs in your repository, such as a pull request being opened or an issue being created. Your workflow contains one or more jobs which can run in sequential order or in parallel.

Understanding GitHub Actions - GitHub Docs

// DeepL翻訳
GitHub Actions のワークフローは、プルリクエストのオープンや課題の作成など、リポジトリでイベントが発生したときに起動するように設定することができます。ワークフローには一つまたは複数のジョブが含まれ、順次または並行して実行されます。

ワークフローは、1つ以上のジョブを実行する設定可能な自動化されたプロセスです。 ワークフローはリポジトリにチェックインされるYAMLファイルで定義され、リポジトリ内のイベントでトリガーされるか、手動でトリガーされるか、定義されたスケジュールに従って実行されます。

ワークフローはリポジトリ中の.github/workflowsディレクトリで定義され、1つのリポジトリに複数のワークフローを持たせ、それぞれが様々なタスクのセットを実行するようにできます。

Understanding GitHub Actions - GitHub Docs

イベント

An event is a specific activity in a repository that triggers a workflow run. For example, activity can originate from GitHub when someone creates a pull request, opens an issue, or pushes a commit to a repository. You can also trigger a workflow run on a schedule, by posting to a REST API, or manually.

Understanding GitHub Actions - GitHub Docs

// DeepL翻訳
イベントとは、ワークフローを実行するきっかけとなるリポジトリ内の特定のアクティビティです。たとえば、誰かがプルリクエストを作成したり、課題を開いたり、リポジトリにコミットをプッシュしたりすると、GitHub からアクティビティが発生することがあります。また、スケジュールやREST APIへの投稿、手動でワークフローの実行をトリガーすることもできます。

ジョブ

A job is a set of steps in a workflow that execute on the same runner. Each step is either a shell script that will be executed, or an action that will be run. Steps are executed in order and are dependent on each other. Since each step is executed on the same runner, you can share data from one step to another.
...
You can configure a job's dependencies with other jobs; by default, jobs have no dependencies and run in parallel with each other. When a job takes a dependency on another job, it will wait for the dependent job to complete before it can run.

Understanding GitHub Actions - GitHub Docs

ジョブは、同じランナーで実行されるワークフローのステップのセットです。各ステップは、実行されるシェルスクリプト、または実行されるアクションです。ステップは順番に実行され、互いに依存し合っています。各ステップは同じランナー上で実行されるので、あるステップから別のステップへデータを共有することができます。
...
ジョブは他のジョブとの依存関係を設定することができます。デフォルトでは、ジョブは依存関係を持たず、互いに並行して実行されます。あるジョブが他のジョブに依存する場合、そのジョブは依存するジョブの完了を待って実行されます。

Actions

An action is a custom application for the GitHub Actions platform that performs a complex but frequently repeated task. Use an action to help reduce the amount of repetitive code that you write in your workflow files.

Understanding GitHub Actions - GitHub Docs

// DeepL翻訳
アクションとは、GitHub Actions プラットフォームのカスタムアプリケーションで、複雑だが頻繁に繰り返されるタスクを実行するものです。アクションを使うことで、ワークフローファイルの中に書く繰り返しコードを減らすことができます。

ランナー

ランナーは、トリガーされたときにワークフローを実行するサーバーです。 Each runner can run a single job at a time. GitHub provides Ubuntu Linux, Microsoft Windows, and macOS runners to run your workflows; each workflow run executes in a fresh, newly-provisioned virtual machine.

Understanding GitHub Actions - GitHub Docs

// DeepL翻訳
各ランナーは一度に一つのジョブを実行することができます。GitHubは、ワークフローを実行するためのUbuntu Linux、Microsoft Windows、macOSランナーを提供します。各ワークフローの実行は、新しくプロビジョニングされた仮想マシンで行われます。

ワークフローファイルを理解する

以下のymlを例に各行について理解を深めます。

name: learn-github-actions
on: [push]
jobs:
  check-bats-version:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: '14'
      - run: npm install -g bats
      - run: bats -v

name:

GitHub リポジトリの [Actions] タブに表示されるワークフローの名前。(オプション)

name: learn-github-actions

on:

このワークフローのトリガーを指定します。 pushイベントを使っているので、プッシュorPull Requestをマージするたびにトリガーされます。
また全てのブランチが対象になります。

on: [push]

push以外にもforkcreate等めちゃくちゃ種類があるっぽいですね。

branch_protection_rule
check_run
check_suite
create
delete
deployment
deployment_status
discussion
discussion_comment
fork
gollum
issue_comment
issues
label
merge_group
milestone
page_build
project
project_card
project_column
public
pull_request
pull_request_comment (use issue_comment)
pull_request_review
pull_request_review_comment
pull_request_target
push
registry_package
release
repository_dispatch
schedule
status
watch
workflow_call
workflow_dispatch
workflow_run

Events that trigger workflows - GitHub Docs

複数イベントの利用
on: [push, fork]
ブランチの指定
on:
  pull_request:
    # Sequence of patterns matched against refs/heads
    branches:    
      - main
      - 'mona/octocat'
      - 'releases/**'
  • mainという名前のブランチ
  • mona/octocatという名前のブランチ
  • releases/10のようにreleases/で始まる名前のブランチ

jobs:

learn-github-actionsワークフロー中で実行されるすべてのジョブをグループ化します。

check-bats-version:

check-bats-versionという名前のジョブを定義します。 子キーはジョブのプロパティを定義します。

runs-on:

ジョブがGitHubによってホストされているどの新しい仮想マシンで実行するか決めます。

runs-on: ubuntu-latest

利用可能なGitHubホストランナーは以下の通り。

windows-latest
windows-2022
windows-2019
ubuntu-22.04
ubuntu-latest
ubuntu-20.04
ubuntu-18.04(非推奨)
macos-12
macos-latest
macos-11
macos-10.15(非推奨)

GitHub Actionsのワークフロー構文 - GitHub Docs

steps:

ジョブで実行されるすべてのステップをグループ化します。
このセクションの下にネストされている各アイテムは、個別のアクションもしくはシェルスクリプトです。

- uses:

usesはGitHubやサードパーティの公開アクションを利用します。

 - uses: actions/checkout@v3

usesキーワードは、このステップがactions/checkoutアクションのv3を実行することを指定しています。 これは、リポジトリをランナーにチェックアウトするアクションで、コードに対してスクリプトや他のアクション(ビルドやテストツールなど)を実行できるようにしてくれます。 ワークフローがリポジトリのコードに対して実行されるときには、いつでもチェックアウトのアクションを使うべきです。

- uses: actions/setup-node@v3
     with:
       node-version: '14'

このステップはactions/setup-node@v3アクションを使ってNode.jsの指定されたバージョンをインストールします(この例ではv14が使われます)。 これは、node及びnpmコマンドをどちらもPATHに置きます

- runs:

runでコマンドを実行することができます。

- run: npm install -g bats

npmを使用してbatsをインストールしています。

- run: bats -v

batsのバージョンを出力します。

ひとこと

まだまだ覚えた方がいいこともありますので、公式ドキュメントを見るのが一番です。

またGitHub Actionsを利用する上でShallへの知識をもっとつけた方が良いとつくづく思いました。
www.tohoho-web.com