■ はじめに
https://dk521123.hatenablog.com/entry/2021/11/04/142835
https://dk521123.hatenablog.com/entry/2022/06/16/151443
の続き。 egg / wheel ファイルをGithub Actionsを使って作成する その際に学べたことメモする。
目次
【0】今回やりたいこと 【1】今回Github Actionsで学べたこと 1)環境変数 2)if 【2】サンプル 【3】動作確認 0)ダウンロード 1)インストール 2)動作確認 3)アンインストール
【0】今回やりたいこと
* egg / wheel ファイルをGithub Actionsを使って作成する * その際、ファイルサイズを確認 + 一定サイズ以下であれば、エラー + 一定サイズを超えていたら、Githubにコミット&プッシュ
【1】今回Github Actionsで学べたこと
1)環境変数
* 以下のサイトが分かりやすい
https://zenn.dev/kyome/articles/a89fd954c5936f
https://docs.github.com/ja/actions/using-workflows/workflow-commands-for-github-actions
構文
run: echo "<環境変数名>=<値>" >> $GITHUB_ENV # ${{ env.<環境変数名> }} で呼び出すことができる
例
run: | echo "ENV1=Hello" >> $GITHUB_ENV echo "ENV2=World" >> $GITHUB_ENV echo "env.ENV1 env.ENV2!!" # Hello World!!
2)if
ファイルサイズの大きさによっては、中断させたいので 分岐になる if が必要
https://zenn.dev/matken/articles/github-actions-if-and-env#if
【2】サンプル
前提条件
https://dk521123.hatenablog.com/entry/2020/02/09/234350
で、egg / wheel ファイルを作成したが、それを Push のタイミングで、 「python setup.py bdist_egg」/「python setup.py bdist_wheel」を 実行するようにする なお、以下のサイトを参考にして実装した。
https://andrewpwheeler.com/2022/05/10/building-wheel-files-in-github-actions/
github-actions-demo2.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: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - uses: actions/checkout@v3 - name: Setup Python uses: actions/setup-python@v2 with: python-version: '3.7' architecture: 'x64' - name: Get Python version run: python -V - name: Build wheel and install run: | python -m pip install --upgrade pip --user pip install wheel pip install PasteScript # Wheelファイル作成およびファイルサイズをWHL_FILE_SIZEに格納 - name: Run Python run: | python ./DemoLib/setup.py bdist_wheel cp ./dist/*.whl ./DemoLib/dist/*.whl echo "WHL_FILE_SIZE=$(wc -c ./dist/*.whl | awk '{print $1}')" >> $GITHUB_ENV # Wheelファイルサイズを確認(2000byte以下であれば中断) - name: File size check if: ${{ env.WHL_FILE_SIZE <= 2000 }} run: exit 1 - name: Configure Git run: | git config --global user.email "xxxxx@gmail.com" git config --global user.name "xxxxx" # Git コミット&プッシュ - name: Commit and push wheel run: | git add -f ./DemoLib/dist/*.whl git commit -m 'pushing new wheel' git push
【3】動作確認
0)ダウンロード
[1] Github で、[Code]-[Download ZIP]でモジュールをダウンロード => 今回は「github-actions-demo-develop.zip」 [2] ダウンロードしたモジュールを解凍する
1)インストール
# インストール pip install DemoLib-0.1.dev0-py3-none-any.whl # 確認1:pip list pip list ・・・ DemoLib 0.0 ・・・ # 確認2:pip show <package> pip show DemoLib
2)動作確認
from demolib.demo_lib import * if __name__=='__main__': result = say_hello('Mike') print('Result = {}'.format(result)) # Result = Hello, Mike.
3)アンインストール
# アンインストール pip uninstall DemoLib # 動作確認(DemoLibが存在しないことを確認) pip list
関連記事
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/2022/06/16/151443
egg / wheel ファイルを作成する
https://dk521123.hatenablog.com/entry/2020/02/09/234350
大きいファイルを扱う際のコマンド
https://dk521123.hatenablog.com/entry/2020/06/12/000000