💚Fixed GitHub Actions #35
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Workflow for testing the project across multiple operating systems and Python versions. | |
# This workflow runs on main branch pushes, pull requests to main, and can be manually triggered. | |
# It performs unit tests, generates coverage reports, and updates the README with the latest coverage data. | |
name: Multi-OS Test | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
- '*' | |
pull_request: | |
branches: | |
- 'main' | |
jobs: | |
test: | |
strategy: | |
matrix: | |
os: [macos-12, ubuntu-latest] | |
python-version: ['3.10', '3.11', '3.12'] | |
runs-on: ${{ matrix.os }} | |
env: | |
# タイムゾーンを東京時間に設定 | |
TZ: 'Asia/Tokyo' | |
permissions: | |
contents: write | |
pull-requests: write | |
steps: | |
# リポジトリをチェックアウト | |
- uses: actions/checkout@v4 | |
# Python環境のセットアップ | |
- uses: actions/setup-python@v5 | |
with: | |
python-version: ${{matrix.python-version}} | |
# ffmpegのインストール | |
- name: Install FFmpeg (macOS) | |
if: matrix.os == 'macos-12' | |
run: brew install ffmpeg | |
- name: Install FFmpeg (Ubuntu) | |
if: matrix.os == 'ubuntu-latest' | |
run: sudo apt-get update && sudo apt-get install -y ffmpeg | |
# Windowsのタイムゾーン設定 | |
- name: Set timezone on Windows | |
if: runner.os == 'Windows' | |
run: tzutil /s "Tokyo Standard Time" | |
shell: cmd | |
- name: Install poetry | |
# poetryのインストール | |
run: pip install poetry | |
- name: Cache dependencies | |
#キャッシュの活用 | |
uses: actions/cache@v4 | |
with: | |
path: ~/.cache/pypoetry | |
key: ${{ runner.os }}-poetry-${{ hashFiles('**/poetry.lock') }} | |
- name: Install dependencies | |
# Poetryで依存関係をインストール | |
run: poetry install | |
- name: Run test | |
id: pytest | |
# エラーが発生しても続行する | |
continue-on-error: true | |
#run: poetry run pytest --durations=0 --junitxml=pytest.xml --cov-report "xml:coverage.xml" --cov=video_grid_merge tests/ | tee pytest-coverage.txt | |
run: poetry run task test_gh_action_xml | |
- name: Pytest coverage comment | |
# カバレッジレポートをコメントとして生成 | |
id: coverageComment | |
uses: MishaKav/pytest-coverage-comment@main | |
with: | |
pytest-coverage-path: ./pytest-coverage.txt | |
pytest-xml-coverage-path: ./coverage.xml | |
title: Coverage Report (${{ matrix.os }} / Python ${{ matrix.python-version }}) | |
badge-title: coverage | |
hide-badge: false | |
hide-report: false | |
create-new-comment: false | |
hide-comment: false | |
report-only-changed-files: false | |
remove-link-from-badge: false | |
junitxml-path: ./pytest.xml | |
junitxml-title: "Pytest Result Summary (os: ${{ matrix.os }} / python-version: ${{ matrix.python-version }})" | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Check test results | |
if: steps.pytest.outcome == 'failure' | |
run: | | |
echo "Tests failed. This will prevent merging the pull request." | |
exit 1 | |
- name: Save summary report | |
run: | | |
# カバレッジのサマリーレポートをMarkdownファイルとして保存 | |
echo '${{ steps.coverageComment.outputs.summaryReport }}' > summary-report.md | |
- name: Upload coverage data | |
# カバレッジデータをアップロード | |
uses: actions/upload-artifact@v4 | |
with: | |
name: coverage-data-${{ matrix.os }}-${{ matrix.python-version }} | |
path: | | |
# pytestの出力テキスト(テストの実行結果とカバレッジデータ) | |
pytest-coverage.txt | |
# カバレッジデータのXML形式ファイル(詳細なカバレッジ情報) | |
coverage.xml | |
# テスト結果のJUnit XMLレポート(テストの成功/失敗や詳細なタイムスタンプ情報を含む) | |
pytest.xml | |
# カバレッジとテスト結果のサマリー(Markdown形式で出力したレポート) | |
summary-report.md | |
update_readme: | |
needs: test | |
runs-on: ubuntu-latest | |
if: always() | |
permissions: | |
contents: write | |
pull-requests: write | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Download all artifacts | |
uses: actions/download-artifact@v4 | |
- name: Update README in coverage branch | |
run: | | |
# "coverage" ブランチが存在する場合はチェックアウト、存在しない場合は新規作成する | |
if git ls-remote --heads origin coverage | grep coverage; then | |
git checkout coverage | |
else | |
git checkout --orphan coverage | |
git rm -rf . | |
fi | |
# README.mdのヘッダーとバッジを追加 | |
echo "# Coverage Reports" > README.md | |
echo "[](https://github.com/$GITHUB_REPOSITORY/actions/workflows/test_branch.yml)" >> README.md | |
echo "" >> README.md | |
# 最新のコミット情報をREADMEに追加 | |
commit_hash=${GITHUB_SHA::8} | |
commit_link="[$commit_hash](https://github.com/$GITHUB_REPOSITORY/tree/$commit_hash)" | |
echo -e "> [!Note]" >> README.md | |
echo -e "> " >> README.md | |
echo -e "> Commit: $commit_link" >> README.md | |
echo -e "" >> README.md # この行を追加 | |
# macOS、Ubuntuの各OSとPythonバージョンごとにカバレッジレポートを追加 | |
for os in macos-12 ubuntu-latest; do | |
for version in 3.11 3.12; do | |
echo "## Coverage Report (os: $os / python-version: $version)" >> README.md | |
if [ -f "coverage-data-$os-$version/summary-report.md" ]; then | |
sed -e 's/^"//' -e 's/"$//' -e 's/\\"/"/g' -e 's/\\\\/\\/g' -e 's/\\n/\n/g' -e 's/\r$//' "coverage-data-$os-$version/summary-report.md" | | |
sed -e 's/</</g' -e 's/>/>/g' -e 's/&/\&/g' | | |
sed '/^$/N;/^\n$/D' | | |
sed -e 's/^"//' -e 's/"$//' >> README.md | |
else | |
echo "No summary report found for $os - $version" >> README.md | |
fi | |
echo "" >> README.md | |
done | |
done | |
# ファイルパスを修正する | |
sed -i ' | |
s|/blob/\([a-f0-9]*\)/\([^"]*\)|/blob/\1/video_grid_merge/\2|g; | |
s|/blob/\([a-f0-9]*\)/video_grid_merge/README\.md|/blob/\1/README.md|g | |
' README.md | |
- name: Commit and push | |
run: | | |
# Gitのユーザー設定とコミット、ブランチへのプッシュ | |
git config --local user.email "33836132+github-actions[bot]@users.noreply.github.com" | |
git config --local user.name "github-actions[bot]" | |
git add README.md | |
git commit -m "Update coverage for all environments" | |
git push origin coverage | |
check_all_tests: | |
needs: test | |
runs-on: ubuntu-latest | |
if: always() | |
steps: | |
- name: Check test results | |
if: contains(needs.test.result, 'failure') | |
run: | | |
echo "Some tests failed. Please check the test results and fix any issues before merging." | |
exit 1 |