【Github】Github Actions ~ 基本編 ~

■ はじめに

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

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

目次

【1】Hello world
 1)手順
 2)動作確認
【2】flake8/black/isort を使う
【3】yamllintを追加

【1】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】flake8/black/isort を使う

Github Actionsで「flake8」「black」「isort」を使いたい
=> 各ツールの詳細については、以下の関連記事を参照のこと。

https://dk521123.hatenablog.com/entry/2020/02/07/000000
https://dk521123.hatenablog.com/entry/2021/11/10/095258
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
    env:
      PYTHON_VERSION: 3.7
      MAX_LINE_LENGTH: 120
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2
      - name: Set up Python 3.X
        uses: actions/setup-python@v2
        with:
          python-version: ${PYTHON_VERSION}
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install flake8 isort black
      - name: Lint
        run: |
          flake8 . --max-line-length=${MAX_LINE_LENGTH}
          isort --check --diff .
          black --check --line-length ${MAX_LINE_LENGTH} .

【3】yamllintを追加

https://github.com/adrienverge/yamllint

にある yamllint を追加してみる。

github-actions-demo.yml

      - uses: actions/checkout@v2

      # ★ここを追加★ => コミット後実行されYAMLファイルがチェックされるはず
      - uses: actions/checkout@v2
      - name: yaml-lint
        uses: ibiqlik/action-yamllint@v3

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

関連記事

Github ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/07/18/234652
Github Actions ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2021/11/04/142835
Github Actions ~ egg / wheel ファイル作成 ~
https://dk521123.hatenablog.com/entry/2022/06/21/143624