😎
GitHub Actionsとは?
GitHub Actionsは、GitHub上でCI/CD(継続的インテグレーション/継続的デリバリー)を実行できるワークフロー自動化ツールです。
例えば、以下のようなことが可能です:
- プッシュやプルリクエスト時に自動でテストを実行
- コードをビルドして成果物を作成
- 自動デプロイ
- LinterやFormatterを自動で実行
Pythonプロジェクトの簡単なGitHub Actions実装例
以下は、Pythonプロジェクトでプッシュ時に自動的にテストを実行するGitHub Actionsの例です。
📁 プロジェクト構成
my-python-project/
├── .github/
│ └── workflows/
│ └── python-app.yml # GitHub Actionsの設定ファイル
├── app.py # Pythonアプリ本体
├── test_app.py # テストコード
└── requirements.txt # 依存関係
app.py
(サンプルコード)
📝 def add(a, b):
return a + b
if __name__ == "__main__":
print("2 + 3 =", add(2, 3))
test_app.py
(テストコード)
🧪 import unittest
from app import add
class TestApp(unittest.TestCase):
def test_add(self):
self.assertEqual(add(2, 3), 5)
self.assertEqual(add(-1, 1), 0)
self.assertEqual(add(0, 0), 0)
if __name__ == '__main__':
unittest.main()
requirements.txt
(依存ライブラリ)
📋 unittest
⚙️ GitHub Actionsの設定ファイル
.github/workflows/python-app.yml
name: Python application
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: |
python -m unittest discover
💡 各ステップの説明
-
on: push / pull_request
-
main
ブランチにプッシュまたはプルリクエストが発生したときにトリガー。
-
-
jobs: build
-
ubuntu-latest
環境で実行。
-
-
Checkout repository
- リポジトリのソースコードを取得。
-
Set up Python
- Python 3.9をインストール。
-
Install dependencies
-
requirements.txt
のライブラリをインストール。
-
-
Run tests
-
unittest
を使ってテストを実行。
-
🚀 実行結果
- GitHubの Actionsタブ から実行状況が確認できます。
- テストが失敗した場合、どのテストが失敗したのかが表示されます。
- 成功すれば緑色のチェックマークがつきます ✅
🛠️ 応用
-
複数のPythonバージョンでテスト:
strategy: matrix: python-version: [3.7, 3.8, 3.9, 3.10]
- Lintツール(例: flake8)を組み込む
- Dockerイメージのビルドやデプロイ も可能
以下は、GitHub Actionsを設定して実際にリポジトリに反映させるためのGitHubコマンドの流れです。
💻 GitHub コマンドの流れ
1️⃣ GitHubリポジトリの作成
# 新しいリポジトリを作成
git init my-python-project
cd my-python-project
# GitHub上でリポジトリを作成して、そのリモートURLを設定
git remote add origin https://github.com/your-username/my-python-project.git
2️⃣ ファイルを準備
# プロジェクト構成に従ってファイルを作成
mkdir -p .github/workflows
# app.py, test_app.py, requirements.txt, python-app.ymlを作成
# (内容は前回のメッセージを参照)
3️⃣ ファイルをステージングしてコミット
# すべてのファイルをステージング
git add .
# 初回コミット
git commit -m "Initial commit with Python app and GitHub Actions"
4️⃣ GitHubにプッシュ
# 初回プッシュ(mainブランチとして設定)
git branch -M main
git push -u origin main
5️⃣ GitHub Actionsの確認
- GitHubのリポジトリページにアクセス。
- Actions タブをクリック。
- ワークフローが実行されていることを確認。
🛠️ その後の開発フローで使うGitHubコマンド
✅ 新しい変更をコミットしてプッシュ
# 変更を確認
git status
# 変更をステージング
git add <変更したファイル名> # 例: git add app.py
# コミット
git commit -m "Fix: corrected add function"
# プッシュ
git push origin main
🔀 新機能用にブランチを切る
# 新しいブランチ作成
git checkout -b feature/new-feature
# コード修正 & コミット
git add .
git commit -m "Add: implemented new feature"
# プッシュ
git push origin feature/new-feature
📦 プルリクエストを作成
- GitHub上で新しくプッシュしたブランチを確認。
- Compare & pull request ボタンをクリック。
- プルリクエストを作成してレビュー依頼。
- マージ後、GitHub Actionsが再び走るのを確認。
⚡ 便利なGitコマンド
-
コミット履歴確認
git log --oneline
-
ファイルの差分確認
git diff
-
過去のコミットを修正
git commit --amend
-
GitHubリポジトリの状態確認
git remote -v
🚀 ワークフローのデバッグ
-
ローカルでGitHub Actionsの実行を確認したい場合は、
act
というCLIツールが便利です。インストール
brew install act # macOSの場合
実行
act
これでGitHub Actionsを設定して、基本的なGitフローを使いながらCI/CDの流れを作成できます!💪🚀
Discussion