-
Notifications
You must be signed in to change notification settings - Fork 0
GitHub Actions で dev・master からのデプロイを自動化する #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| name: Verify worker | ||
| description: >- | ||
| lockfile どおりに依存を入れて lint / typecheck / test を回し、Worker が実際に | ||
| バンドルできることを dev・production 双方の設定で確認する。検証用ワークフローと | ||
| デプロイ用ワークフローで同じ手順を踏むため composite action に切り出してある。 | ||
|
|
||
| inputs: | ||
| node-version: | ||
| description: Node.js のバージョン (package.json の engines と揃えること) | ||
| required: false | ||
| default: "22" | ||
|
|
||
| # ローカル action は checkout 済みでないと解決できないため、checkout は | ||
| # 呼び出し側のワークフローに置いてある。 | ||
| runs: | ||
| using: composite | ||
| steps: | ||
| - uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: ${{ inputs.node-version }} | ||
| cache: npm | ||
|
|
||
| # npm install と違い package-lock.json を書き換えないので、ローカルで | ||
| # 動かしたのと同じ wrangler / biome / TypeScript の版で検証できる。 | ||
| # wrangler もこの lockfile から入るため、デプロイに使う版の固定先は | ||
| # ワークフロー側ではなく package-lock.json 一箇所で済む。 | ||
| - name: Install dependencies | ||
| shell: bash | ||
| run: npm ci | ||
|
|
||
| - name: Lint | ||
| shell: bash | ||
| run: npm run lint | ||
|
|
||
| - name: Typecheck | ||
| shell: bash | ||
| run: npm run typecheck | ||
|
|
||
| - name: Test | ||
| shell: bash | ||
| run: npm test | ||
|
|
||
| # tsc は型しか見ないので、import の解決ミスや nodejs_compat で賄えない | ||
| # Node API はバンドルして初めて落ちる。--dry-run は Cloudflare API を | ||
| # 叩かないため認証情報なしで回せる。 | ||
| # | ||
| # dev と production を両方バンドルするのは、wrangler.jsonc の env.production | ||
| # 側だけが壊れている状態を master へ入れる前に捕まえるため。dev への push | ||
| # では production 設定に一切触れないまま緑になってしまう。 | ||
| - name: Build (dry-run) | ||
| shell: bash | ||
| env: | ||
| WRANGLER_SEND_METRICS: "false" | ||
| # wrangler の色付けが Total Upload 行に混ざると要約が読めなくなる | ||
| NO_COLOR: "1" | ||
| run: | | ||
| # dev は wrangler.jsonc の top-level 設定。wrangler 4 は環境が複数ある | ||
| # 状態で --env を省くと警告を出すため、空文字でも明示する。 | ||
| for target in "" production; do | ||
| label="${target:-dev}" | ||
| npx wrangler deploy --env="$target" --dry-run \ | ||
| --outdir "$RUNNER_TEMP/bundle-$label" 2>&1 | tee "$RUNNER_TEMP/$label.log" | ||
| # Workers の上限は gzip 後で 10 MiB。今は 1/10 にも届かないので | ||
| # 失敗にはせず、増え方が見えるよう要約に残すだけにする。 | ||
| size=$(grep -m1 'Total Upload' "$RUNNER_TEMP/$label.log" || true) | ||
| echo "- \`$label\`: ${size:-size unknown}" >> "$GITHUB_STEP_SUMMARY" | ||
| done |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # lint / typecheck / test が通ることと、Worker がバンドルできることを検証する | ||
| # だけのワークフロー。デプロイはしない。 | ||
| # | ||
| # デプロイは環境ごとに別ファイルへ分けてある: | ||
| # dev -> deploy_dev.yml (trainlcd-worker-dev) | ||
| # master -> deploy_production.yml (trainlcd-worker) | ||
| # デプロイ先をトリガとファイルで固定することで、他のブランチが誤って | ||
| # どこかの環境へ向くことがないようにしている。 | ||
| on: | ||
| pull_request: | ||
| # GitHub Actions は YAML のアンカー / エイリアスを解釈しないため、 | ||
| # このリストは push 側とも、デプロイ用の 2 ファイルとも二重に書く必要が | ||
| # ある。片方だけ直さないこと。 | ||
| paths: | ||
| - "src/**" | ||
| - "test/**" | ||
| - "scripts/**" | ||
| - "package.json" | ||
| - "package-lock.json" | ||
| - "tsconfig.json" | ||
| - "biome.json" | ||
| - "jest.config.js" | ||
| - "wrangler.jsonc" | ||
| - ".github/actions/verify/action.yml" | ||
| - ".github/workflows/ci.yml" | ||
| # deploy 用の 2 ファイルはどちらも pull_request で起動しないため、 | ||
| # ここに載せておかないと変更した PR がどの workflow も通らないまま | ||
| # マージされ、デプロイ時に初めて動くことになる。 | ||
| - ".github/workflows/deploy_dev.yml" | ||
| - ".github/workflows/deploy_production.yml" | ||
| push: | ||
| # dev / master は deploy_dev.yml / deploy_production.yml が同じ composite | ||
| # action で検証してからデプロイするため、ここでは走らせない。 | ||
| branches-ignore: | ||
| - dev | ||
| - master | ||
| paths: | ||
| - "src/**" | ||
| - "test/**" | ||
| - "scripts/**" | ||
| - "package.json" | ||
| - "package-lock.json" | ||
| - "tsconfig.json" | ||
| - "biome.json" | ||
| - "jest.config.js" | ||
| - "wrangler.jsonc" | ||
| - ".github/actions/verify/action.yml" | ||
| - ".github/workflows/ci.yml" | ||
| # deploy 用の 2 ファイルはどちらも pull_request で起動しないため、 | ||
| # ここに載せておかないと変更した PR がどの workflow も通らないまま | ||
| # マージされ、デプロイ時に初めて動くことになる。 | ||
| - ".github/workflows/deploy_dev.yml" | ||
| - ".github/workflows/deploy_production.yml" | ||
| workflow_dispatch: | ||
|
|
||
| name: Continuous integration | ||
|
|
||
| # 同じ PR / ブランチに続けて push したとき、古い方は結果が要らない。 | ||
| concurrency: | ||
| group: ci-${{ github.ref }} | ||
| cancel-in-progress: true | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| verify: | ||
| name: Lint, typecheck, test and build | ||
| runs-on: ubuntu-latest | ||
|
|
||
| # ここでは environment を宣言しない。environment は if: と違って | ||
| # ジョブが走れば必ず適用されるため、宣言すると全ブランチ・全 PR が | ||
| # その環境へのデプロイとして履歴に載り、環境 Secret (デプロイ用の | ||
| # CLOUDFLARE_API_TOKEN を含む) が任意のブランチのビルドから触れる。 | ||
| # | ||
| # 検証は --dry-run で Cloudflare API を叩かないため、そもそも認証情報が要らない。 | ||
| steps: | ||
| # checkout は既定で GITHUB_TOKEN を .git/config に残す。後続の npm ci は | ||
| # 依存パッケージの install スクリプトを実行するため読み取られうる。 | ||
| # ここから先で git 認証は使わない。 | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| persist-credentials: false | ||
|
|
||
| - uses: ./.github/actions/verify | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # dev を dev 環境 (trainlcd-worker-dev) へデプロイする。 | ||
| # | ||
| # デプロイ先はこのファイルとトリガで固定してある。ブランチを式で判定して | ||
| # 環境を選ぶ作りにすると、environment は if: と違ってジョブが走れば必ず | ||
| # 適用されるため、意図しないブランチがこの環境の履歴と Secret に触れる。 | ||
| on: | ||
| push: | ||
| branches: | ||
| - dev | ||
| # GitHub Actions は YAML のアンカー / エイリアスを解釈しないため、 | ||
| # このリストは ci.yml / deploy_production.yml とも二重に書く必要がある。 | ||
| # 片方だけ直さないこと。 | ||
| paths: | ||
| - "src/**" | ||
| - "test/**" | ||
| - "scripts/**" | ||
| - "package.json" | ||
| - "package-lock.json" | ||
| - "tsconfig.json" | ||
| - "biome.json" | ||
| - "jest.config.js" | ||
| - "wrangler.jsonc" | ||
| - ".github/actions/verify/action.yml" | ||
| - ".github/workflows/deploy_dev.yml" | ||
| workflow_dispatch: | ||
|
|
||
| name: Deploy to dev | ||
|
|
||
| # 同時に流れると、先に始まった古い版が後から上書きしうる。 | ||
| concurrency: | ||
| group: deploy-dev | ||
| cancel-in-progress: false | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| deploy: | ||
| name: Verify and deploy to dev | ||
| runs-on: ubuntu-latest | ||
|
|
||
| # workflow_dispatch にはブランチ絞り込みが無いので、ここで塞ぐ。 | ||
| # push は on: branches で dev に限定済み。 | ||
| if: github.ref == 'refs/heads/dev' | ||
|
|
||
| environment: dev | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| persist-credentials: false | ||
|
|
||
| - uses: ./.github/actions/verify | ||
|
|
||
| # 直前の検証で --dry-run 済みのものと同じ入力から同じバンドルが組み上がる。 | ||
| # wrangler は node_modules から解決されるので、検証と同じ版が走る。 | ||
| # | ||
| # dev は wrangler.jsonc の top-level 設定なので環境名は空にする。 | ||
| # wrangler 4 は環境が複数あると --env の省略を警告するため、空でも明示する。 | ||
| # | ||
| # Worker の secrets (SESSION_JWT_SECRET など) はここでは触らない。 | ||
| # deploy は既存の secrets を保持するため、投入は scripts/put-secrets.sh で | ||
| # 手元から行う運用のままでよい。 | ||
| - name: Deploy | ||
| env: | ||
| CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | ||
| CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | ||
| WRANGLER_SEND_METRICS: "false" | ||
| run: npx wrangler deploy --env="" |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # master を production (trainlcd-worker) へデプロイする。 | ||
| # | ||
| # デプロイ先はこのファイルとトリガで固定してある。ブランチを式で判定して | ||
| # 環境を選ぶ作りにすると、environment は if: と違ってジョブが走れば必ず | ||
| # 適用されるため、意図しないブランチがこの環境の履歴と Secret に触れる。 | ||
| on: | ||
| push: | ||
| branches: | ||
| - master | ||
| # GitHub Actions は YAML のアンカー / エイリアスを解釈しないため、 | ||
| # このリストは ci.yml / deploy_dev.yml とも二重に書く必要がある。 | ||
| # 片方だけ直さないこと。 | ||
| paths: | ||
| - "src/**" | ||
| - "test/**" | ||
| - "scripts/**" | ||
| - "package.json" | ||
| - "package-lock.json" | ||
| - "tsconfig.json" | ||
| - "biome.json" | ||
| - "jest.config.js" | ||
| - "wrangler.jsonc" | ||
| - ".github/actions/verify/action.yml" | ||
| - ".github/workflows/deploy_production.yml" | ||
| workflow_dispatch: | ||
|
|
||
| name: Deploy to production | ||
|
|
||
| # 同時に流れると、先に始まった古い版が後から上書きしうる。 | ||
| concurrency: | ||
| group: deploy-production | ||
| cancel-in-progress: false | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| deploy: | ||
| name: Verify and deploy to production | ||
| runs-on: ubuntu-latest | ||
|
|
||
| # workflow_dispatch にはブランチ絞り込みが無いので、ここで塞ぐ。 | ||
| # push は on: branches で master に限定済み。 | ||
| if: github.ref == 'refs/heads/master' | ||
|
|
||
| environment: production | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| persist-credentials: false | ||
|
|
||
| - uses: ./.github/actions/verify | ||
|
|
||
| # 直前の検証で --dry-run 済みのものと同じ入力から同じバンドルが組み上がる。 | ||
| # wrangler は node_modules から解決されるので、検証と同じ版が走る。 | ||
| # | ||
| # production は wrangler.jsonc の env.production を指す。 | ||
| # | ||
| # Worker の secrets (SESSION_JWT_SECRET など) はここでは触らない。 | ||
| # deploy は既存の secrets を保持するため、投入は scripts/put-secrets.sh で | ||
| # 手元から行う運用のままでよい。 | ||
| - name: Deploy | ||
| env: | ||
| CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | ||
| CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | ||
| WRANGLER_SEND_METRICS: "false" | ||
| run: npx wrangler deploy --env production |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.