【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】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]のコミット内容をトリガーに実行されている

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

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

サンプル

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

関連記事

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 ~ workflow_run / workflow_call ~
https://dk521123.hatenablog.com/entry/2024/02/14/232546
Github Actions ~ あれこれ編 ~
https://dk521123.hatenablog.com/entry/2023/12/21/155224
Github Actions ~ エラー処理 ~
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 ~ プロパティ ~
https://dk521123.hatenablog.com/entry/2023/12/23/231250
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 ~ 外部シェルスクリプト実行 ~
https://dk521123.hatenablog.com/entry/2024/01/19/003044
Github Actions ~ Pythonを使うには ~
https://dk521123.hatenablog.com/entry/2024/02/04/011205
Github Actions ~ サンプル集 ~
https://dk521123.hatenablog.com/entry/2023/12/31/231438
Github Actions ~ egg / wheel ファイル作成 ~
https://dk521123.hatenablog.com/entry/2022/06/21/143624
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で「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
Amazon SNSAWS CLI
https://dk521123.hatenablog.com/entry/2024/02/21/141346