■ はじめに
GitHub Actions について、調べなければならなくなったのでメモ。
目次
【1】GitHub Actions 【2】料金 1)パブリックリポジトリの場合 2)プライベートリポジトリの場合 【3】用語整理 1)Workflow(ワークフロー) 2)Action(アクション) 【4】サンプル 例1:Hello world 1)手順 2)動作確認 例2:flake8/black/isort を使う 例3:yamllintを追加 【5】Github Actions あれこれ 1)過去の履歴を見たい場合 2)Market Placeの活用
【1】GitHub Actions
* Github の CI/CD機能 => AWS CodeBuild の Github でできる版
https://dk521123.hatenablog.com/entry/2020/01/21/221122
* GitHubの、Pushなどのイベントをトリガーにし、 予め設定していた対応するアクションを組み合わせてワークフローの自動化が行える仕組み
おさらい:CI/CD
* CI:Continuous Integration(継続的インテグレーション) => 開発環境におけるコード統合(e.g. ビルド, Unit Tests)を継続的に実現するための手法 * CD:Continuous Delivery(継続的デリバリー) => ビルド, デプロイ, 単体試験, などの「価値を届ける」ことを 継続的に実現するための手法 cf. Delivery = 配送, 引き渡す => ここでは、ユーザーにソフトウェアの「価値を届ける」といった意味 詳細は、以下のサイトを参照。
https://atmarkit.itmedia.co.jp/ait/articles/2107/28/news014.html
https://www.kagoya.jp/howto/it-glossary/develop/cicd/
【2】料金
* 公式サイトの料金設定に関する詳細は、以下。
* 色々なサイトを調べたが、安すぎると評判、、、
1)パブリックリポジトリの場合
* 完全無料
2)プライベートリポジトリの場合
* プラン毎に月当たりの無料枠が設定されていてその範囲内なら無料 * 利用するOSが「Linux(Ubuntu)」「Windows」「Mac」でも料金が異なる * 不安な場合は、以下の公式サイトで見積もりできるので計算してみてもいいかも。 (さすが、サービスが細かいところまですごく気が利いている、、、)
https://github.com/pricing/calculator
【3】用語整理
1)Workflow(ワークフロー)
* 実行する処理を定義したYAML形式のファイル * リポジトリ内の.github/workflowsディレクトリ内に保存することで実行 => AWS CodeBuild の buildspec.yml みたいなもの
2)Action(アクション)
* あらかじめ、定義済みの処理セットのこと
【4】サンプル
例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!
【5】Github Actions あれこれ
1)過去の履歴を見たい場合
[1] Github の該当のページを開き、 [Actions]を選択 [2] 左側の「Workflows」の該当するワークフロー(今回の場合「CI」)を選択 [3] 該当の実行結果を選択 [4] 「build」を選択 => すると、YAMLファイルに記載された内容が表示されている
2)Market Placeの活用
* Market Place によく使うものが用意されている * 以下が参考になると思う。
https://yoshikiito.net/blog/archives/python-github-actions-flake8/
Market Place の検索について
https://github.com/marketplace?type=actions
で、対象のツール(例えば、「flake8」と「black」)を検索して使う
flake8 の場合
* 「Flake8 action」を選択
https://github.com/marketplace/actions/flake8-action
black の場合
* 「Black Check」を選択
https://github.com/marketplace/actions/black-check
参考文献
https://qiita.com/bigwheel/items/2ab7deb237122db2fb8d
https://qiita.com/sin9270/items/85e2dab4c0144c79987d
関連記事
Github ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/07/18/234652
Github ~ Webhook 編 ~
https://dk521123.hatenablog.com/entry/2020/01/25/224402
CodeBuild ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2020/01/21/221122
Python を奇麗に書くためのツール群
https://dk521123.hatenablog.com/entry/2021/11/08/221219
flake8 ~ Pythonコードチェック ~
https://dk521123.hatenablog.com/entry/2020/02/07/000000
black ~ Python formatter ~
https://dk521123.hatenablog.com/entry/2021/11/10/095258