8
3

More than 1 year has passed since last update.

GitHub Actions入門~Pythonを自動で実行する~

Last updated at Posted at 2022-07-21

GitHub ActionsPythonスクリプトを自動実行する手順

1.リポジトリ作成

今回はサンプルなので、Publicで作成する

2.Pythonファイルの作成

ルートディレクトリに以下の内容でsample.pyを作成する

import numpy as np

a = np.array([1, 2])
b = np.array([3, 4])
print(a + b)

3.Pythonライブラリ設定ファイルの作成

ルートディレクトリrequirements.txtを作成する
Pythonスクリプトで利用するライブラリとそのバージョンをすべて記載する必要がある
今回はnumpyを利用するので以下の内容を記載

numpy >= 1.21

4.GitHub Actions設定ファイルの作成

  1. ルートディレクトリに.github/workflows/フォルダを作成する
  2. yamlファイルsample.yamlを作成し、以下の内容に編集する
name: Sample

on:
  push:
    branches: 
      - main
  workflow_dispatch:

jobs:
  numpy-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Setup Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.8'
          architecture: 'x64'
      - name: Install dependencies
        run: pip install -r requirements.txt
      - name: Run Python
        run: python sample.py

5.GitHubリポジトリにpushする

作成したファイルをすべてcommitしてGitHubリポジトリにpushする

実行結果の確認

GitHubActionsタブを開くとワークフローが成功していることが確認できる

image.png

中身を確認すると、Pythonの実行結果が出力できていることが確認できます

image.png

今回作成したリポジトリ

参考

Python のビルドとテスト - GitHub Docs
GitHub ActionsでPythonスクリプトを実行する

8
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
3