【Github】Github Actions ~ 入門編 ~

■ はじめに

https://dk521123.hatenablog.com/entry/2021/11/04/142835

の続き。

今回は、ハンズオン的なことをやってみる。

目次

【1】Github Actions の書き方
 1)イメージ
【2】Hello world
 1)手順
 2)動作確認
【3】actions/checkout@vX について
 1)fetch-depth
 2)ref
 3)repository
 4)path
 5)filter

【1】Github Actions の書き方

1)イメージ

* Githubイベントに応じて、Workflow (YAMLファイル)に掛かれたJobを実行していくだけ
 => Github Actions を身に着けることは、このWorkflowの書き方が分かればいいだけ。

Github Event ====> Workflow (.github/workflows/{WORKFLOW_NAME}.yml)
                   +-----------------------------------------------+
                   | workflow                                      |
                   | +-------------------------------------------+ |
                   | | job                                       | | => Runner (サーバで実行)
                   | +-------------------------------------------+ |
                   | +-------------------------------------------+ |
                   | | job                                       | | => Runner (サーバで実行)
                   | | +---------------------------------------+ | |
                   | | | step                                  | | |
                   | | +---------------------------------------+ | |
                   | | +---------------------------------------+ | |
                   | | | step                                  | | |
                   | | +---------------------------------------+ | |
                   | +-------------------------------------------+ |

【2】Hello world

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

に載っている Quick Start をやってみる。

1)手順

* 適当なリポジトリで以下を行う。

[1] Githubにアクセスし [Actions]を選択
[2] 「Simple workflow」内の「Set up this workflow」ボタン押下
[3] ファイル名「github-actions-demo.yml」にし、
 以下のサンプルを参考にYAMLファイルの内容を改変する
[4] 「Start Commit」ボタン押下
 => 後は、流れで、YAMLファイルをコミットしていく

github-actions-demo.yml

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the develop branch
  push:
    branches: [ develop ]
  pull_request:
    branches: [ develop ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    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 }}."
      - run: echo "This job's status is ${{ job.status }}."

      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: echo Hello, world!

      # Runs a set of commands using the runners shell
      - name: Run a multi-line script
        run: |
          echo Add other actions to build,
          echo test, and deploy your project.

2)動作確認

[1] README.mdでもなんでもいいので、ソースを修正し、Commit & Push
[2] ブラウザで Github の該当のページを開き、 [Actions]を選択
 => 「X workflow runs」(X:数字 (e.g. 2))内で、
  [1]のコミット内容をトリガーに実行されている

【3】actions/checkout@vX について

* Github Actionsでソースコードをcloneする

サンプル

  uses: actions/checkout@v4
        with:
          path: path/to/hello # <- 配下にクローンする

1)fetch-depth

* 0 は、全てのブランチ・タグの全ての履歴を取得することを意味する
* Default: 1 (最新のコミットのみを取得する)

ex. Fetch all history for all tags and branches
https://github.com/actions/checkout?tab=readme-ov-file#fetch-all-history-for-all-tags-and-branches

- uses: actions/checkout@v4
  with:
    fetch-depth: 0

2)ref

* ブランチ名を指定

ex. Checkout a different branch
https://github.com/actions/checkout?tab=readme-ov-file#checkout-a-different-branch

- uses: actions/checkout@v4
  with:
    ref: my-branch

3)repository

* リポジトリ名を指定

ex. Checkout multiple repos (side by side)
https://github.com/actions/checkout?tab=readme-ov-file#checkout-multiple-repos-side-by-side

- name: Checkout
  uses: actions/checkout@v4
  with:
    path: main

- name: Checkout tools repo
  uses: actions/checkout@v4
  with:
    repository: my-org/my-tools
    path: my-tools

4)path

* リポジトリをどのパスに落とすか

ex. Checkout multiple repos (private)
https://github.com/actions/checkout?tab=readme-ov-file#checkout-multiple-repos-private

- name: Checkout
  uses: actions/checkout@v4
  with:
    path: main

- name: Checkout private tools
  uses: actions/checkout@v4
  with:
    repository: my-org/my-private-tools
    token: ${{ secrets.GH_PAT }} # `GH_PAT` is a secret that contains your PAT
    path: my-tools

5)filter

* フィルタリング機能
* Default: null

関連記事

Github ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/07/18/234652
Github Actions ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2021/11/04/142835
Github Actions ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2023/12/22/195715
Github Actions ~ ワークフロー制御 ~
https://dk521123.hatenablog.com/entry/2024/01/28/004128
Github Actions ~ if ~
https://dk521123.hatenablog.com/entry/2024/03/11/000000
Github Actions ~ workflow_dispatch / 手動実行 ~
https://dk521123.hatenablog.com/entry/2024/03/12/012953
Github Actions ~ workflow_run / workflow_call ~
https://dk521123.hatenablog.com/entry/2024/02/14/232546
Github Actions ~ pull_request / pull_request_target ~
https://dk521123.hatenablog.com/entry/2024/04/10/152101
Github Actions ~ あれこれ編 ~
https://dk521123.hatenablog.com/entry/2023/12/21/155224
Github Actions ~ エラー処理 / continue-on-error ~
https://dk521123.hatenablog.com/entry/2024/01/01/232057
Github Actions ~ GITHUB_ENV ~
https://dk521123.hatenablog.com/entry/2023/12/29/000840
Github Actions ~ GITHUB_OUTPUT ~
https://dk521123.hatenablog.com/entry/2024/01/30/002943
Github Actions ~ GITHUB_TOKEN / permissions ~
https://dk521123.hatenablog.com/entry/2024/04/22/221027
Github Actions ~ プロパティ ~
https://dk521123.hatenablog.com/entry/2023/12/23/231250
Github Actions ~ Annotations / Matcher ~
https://dk521123.hatenablog.com/entry/2024/03/25/182153
Github Actions ~ Artifact ~
https://dk521123.hatenablog.com/entry/2024/02/13/021717
Github Actions ~ timeout-minutes ~
https://dk521123.hatenablog.com/entry/2024/02/09/000129
Github Actions ~ Github Actions環境変数
https://dk521123.hatenablog.com/entry/2024/02/26/194437
Github Actions ~ 外部シェルスクリプト実行 ~
https://dk521123.hatenablog.com/entry/2024/01/19/003044
Github Actions ~ SQL Linter ~
https://dk521123.hatenablog.com/entry/2024/03/04/180308
Github Actions ~ Scala Linter ~
https://dk521123.hatenablog.com/entry/2024/04/02/002828
Github Actions ~ Pythonを使うには ~
https://dk521123.hatenablog.com/entry/2024/02/04/011205
Github Actions ~ Python関連 ~
https://dk521123.hatenablog.com/entry/2022/06/21/143624
Github Actions ~ Slack連携 ~
https://dk521123.hatenablog.com/entry/2024/04/03/003053
Github Actions ~ サンプル集 ~
https://dk521123.hatenablog.com/entry/2023/12/31/231438
Github Actions ~ 設定値を切り替えることを考える ~
https://dk521123.hatenablog.com/entry/2024/02/18/232926
Github Actions ~ Self-hosted runners / 入門編 ~
https://dk521123.hatenablog.com/entry/2023/12/18/204119
Github Actions ~ Self-hosted runners / あれこれ編 ~
https://dk521123.hatenablog.com/entry/2024/02/07/002736
Github Actions ~ runs-onを動的変更することを考える ~
https://dk521123.hatenablog.com/entry/2024/05/18/032229
Github Actions ~ セキュリティ/Third-Party Github Action ~
https://dk521123.hatenablog.com/entry/2024/04/05/000136
Github Actions ~ セキュリティ/インジェクション攻撃 ~
https://dk521123.hatenablog.com/entry/2024/04/16/222419
Github Actionsで「Waiting for a runner to pick up this job...」から進まない
https://dk521123.hatenablog.com/entry/2024/01/10/195350
GitHub CLI ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2024/02/17/233836
reviewdog ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2024/04/13/232832
reviewdog ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2024/04/18/161200
Amazon SNSAWS CLI
https://dk521123.hatenablog.com/entry/2024/02/21/141346