diff --git a/.cspell/github-action-term.txt b/.cspell/github-action-term.txt index f526704ed..167e7a774 100644 --- a/.cspell/github-action-term.txt +++ b/.cspell/github-action-term.txt @@ -11,3 +11,4 @@ srvaroa startswith webframeworks elif +tonumber diff --git a/.github/initialization/GITHUB_PAGES_PREVIEW.md b/.github/initialization/GITHUB_PAGES_PREVIEW.md new file mode 100644 index 000000000..f873d6ec8 --- /dev/null +++ b/.github/initialization/GITHUB_PAGES_PREVIEW.md @@ -0,0 +1,58 @@ +--- +title: "[初期セットアップ(任意)] Pull Request内でデプロイのワークフローを設定する" +labels: + - initialization + - optional +--- + +# GitHub Pages プレビュー + +この Issue の対応は任意です。 + +このプロジェクトでは `apps/catalog` の Widgetbook を PullRequest 時にプレビューできるようにするワークフローが用意されています。 +プレビューは GitHub Pages 上にデプロイされます。 + +ワークフローを有効化するためには、以下の手順に従って設定してください。 + +## 1. カスタム GitHub Actions ワークフローによる公開設定 + +[カスタム GitHub Actions ワークフローによる公開][1] に従ってGitHub Actions ワークフローでGitHub Pageにデプロイ可能にする。 + +## 2. Visibilityの有効化 + +[GitHub Pages サイトの可視性を変更する][2] に従って`Visibility`を有効化することにより、アクセス制限を設定する。 +ただし、この機能は`GitHub Enterprise Cloud`を契約している`Organization`でしか利用できません。 +`GitHub Enterprise Cloud`以外で非公開のデプロイを行いたい場合、 + +- `AWS CloudFront` +- `Cloudflare Pages` + `Zero Trust` + +等を検討してください。 + +## 3. デプロイ環境の設定確認 + +`GitHub Pages`へデプロイ可能なブランチを制限します。 +`Settings` ⇨ `Environments`から`github-pages`環境を選択し、デプロイ可能なブランチを指定してください。 + +`Deployment branches and tags`では、以下の3つのオプションからプロジェクトに適した設定を選択できます: + +- `No restriction`: すべてのブランチからデプロイ可能 +- `Protected branches only`: 保護されたブランチからのみデプロイ可能 +- `Selected branches and tags`: 指定したブランチやタグパターンに一致するもののみデプロイ可能 + +セキュリティ要件や運用方針に応じて、適切な制限を設定することをお勧めします。 + +## 4. ワークフローの内容を変更する + +`.github/workflows/github-pages-pull-request.yml` を修正する。 + +### 4-1. リポジトリ名条件の削除 + +このテンプレートプロジェクトを元に新規プロジェクトを作成した際に、誤ってワークフローが実行されないように、リポジトリ名による有効化条件を記述しています。この記述はセットアップ後は不要になりますので、削除してください。 + +![github-pages-pull-request.png](https://github.com/user-attachments/assets/324da9ca-078d-4b8f-a7bc-b6441c2c8564) + + + +[1]: https://docs.github.com/ja/pages/getting-started-with-github-pages/configuring-a-publishing-source-for-your-github-pages-site#%E3%82%AB%E3%82%B9%E3%82%BF%E3%83%A0-github-actions-%E3%83%AF%E3%83%BC%E3%82%AF%E3%83%95%E3%83%AD%E3%83%BC%E3%81%AB%E3%82%88%E3%82%8B%E5%85%AC%E9%96%8B +[2]: https://docs.github.com/ja/enterprise-cloud@latest/pages/getting-started-with-github-pages/changing-the-visibility-of-your-github-pages-site diff --git a/.github/workflows/github-pages-pull-request.yml b/.github/workflows/github-pages-pull-request.yml new file mode 100644 index 000000000..40ba78d3c --- /dev/null +++ b/.github/workflows/github-pages-pull-request.yml @@ -0,0 +1,145 @@ +name: Deploy to GitHub Pages on PR + +on: + workflow_dispatch: + pull_request: + paths-ignore: + - ".idea/**" + - ".vscode/**" + - "apps/app/**" + - "docs/**" + - "scripts/**" + - "tools/**" + - "**/docs/**" + - "**/test/**" + - "**.md" + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +env: + PROJECT_DIR: apps/catalog + DEPLOY_DIR: deploy + CURRENT_BRANCH: ${{ github.head_ref || github.ref_name }} + +jobs: + build: + permissions: + actions: read + timeout-minutes: 5 + runs-on: ubuntu-22.04 + # NOTE: See https://github.com/yumemi-inc/flutter-mobile-project-template/blob/main/docs/GITHUB_PAGES_PREVIEW.md + if: ${{ github.repository_owner == 'yumemi-inc' && github.event.repository.name == 'flutter-mobile-project-template' }} + steps: + # https://github.com/actions/checkout + - name: Checkout + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + + - name: Setup Application Runtime + uses: ./.github/actions/setup-application-runtime + + - name: Build web + run: flutter build web --release --base-href /${{ github.event.repository.name }}/${{ env.CURRENT_BRANCH }}/ + working-directory: ${{ env.PROJECT_DIR }} + + - name: Create & Copy combined deployment directory + run: | + mkdir -p ${{ env.DEPLOY_DIR }}/${{ env.CURRENT_BRANCH }}/ + cp -R ${{ env.PROJECT_DIR }}/build/web/. ${{ env.DEPLOY_DIR }}/${{ env.CURRENT_BRANCH }}/ + + - name: Calculate 3 days ago date (UTC) + id: date_calc + run: echo "start_date=$(date -u -d '3 days ago' '+%Y-%m-%dT%H:%M:%SZ')" >> "$GITHUB_OUTPUT" + + - name: Get run IDs from the last 3 days for the triggering workflow + id: get_run_ids + run: | + START_DATE="${{ steps.date_calc.outputs.start_date }}" + echo "Fetching runs for workflow: '${{ github.workflow }}' created since $START_DATE" + + # gh run list で対象ワークフローの過去3日間の成功した実行IDを取得 + # 現在のデプロイワークフロー自身のrun_idは除外 + RUN_IDS=$(gh run list \ + --workflow "${{ github.workflow }}" \ + --status success \ + --created ">=$START_DATE" \ + --json databaseId,headBranch,createdAt \ + | jq -r --arg current_branch "${{ env.CURRENT_BRANCH }}" ' + group_by(.headBranch) | + map(max_by(.createdAt)) | + map(select(.headBranch != $current_branch)) | + map({branch: .headBranch, run_id: .databaseId}) + ') + + if [ -z "$RUN_IDS" ]; then + echo "No successful runs found in the last 3 days for workflow: '${{ github.workflow }}'." + exit 0 # 実行IDが見つからなければ、ジョブを成功として終了 + fi + echo "Found run IDs and their branches:" + echo "$RUN_IDS" + { + echo "run_data<> "$GITHUB_OUTPUT" + + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # gh CLI認証用 + + - name: Download artifacts for each run ID + run: | + mkdir -p _site # GitHub Pagesのルートディレクトリ + + # 出力された各行 (run_id branch_name) をループ処理 + echo '${{ steps.get_run_ids.outputs.run_data }}' | jq -r '.[] | "\(.run_id) \(.branch)"' | while read -r run_id branch; do + if [ -n "$run_id" ] && [ -n "$branch" ]; then + echo "Downloading artifact for run ID: $run_id from branch: $branch" + DOWNLOAD_PATH="dl_artifact/$branch" + mkdir -p "$DOWNLOAD_PATH" + + # gh run download でartifactをダウンロード + gh run download "$run_id" \ + --dir "$DOWNLOAD_PATH" \ + --repo "$GITHUB_REPOSITORY" + echo "Downloaded artifact to $DOWNLOAD_PATH" + + # アーティファクトを解凍して展開 + if [ -f "$DOWNLOAD_PATH/github-pages/artifact.tar" ]; then + echo "Extracting artifact.tar to $DOWNLOAD_PATH" + tar -xf "$DOWNLOAD_PATH/github-pages/artifact.tar" -C "${{ env.DEPLOY_DIR }}" + rm -rf "$DOWNLOAD_PATH/github-pages" # 解凍後のtarファイルとディレクトリを削除 + else + echo "Warning: artifact.tar not found in $DOWNLOAD_PATH/github-pages/" + fi + else + echo "Warning: run_id or branch is empty" + fi + done + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # gh CLI認証用 + + # https://github.com/actions/upload-pages-artifact + - name: Upload static files as artifact + id: deployment + uses: actions/upload-pages-artifact@56afc609e74202658d3ffba0e8f6dda462b719fa # v3.0.1 + with: + path: ./${{ env.DEPLOY_DIR }} + retention-days: 3 + + deploy: + environment: + name: github-pages + # // のフォーマットでURLを生成 + url: ${{ steps.deployment.outputs.page_url }}${{ env.CURRENT_BRANCH }} + permissions: + pages: write + id-token: write + timeout-minutes: 5 + runs-on: ubuntu-22.04 + needs: build + steps: + # https://github.com/actions/deploy-pages + - name: Deploy to GitHub Pages + id: deployment + uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e # v4.0.5