Skip to content

Commit

Permalink
refactor: runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
rharkor committed Sep 26, 2023
1 parent 7f95488 commit fd0c468
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 35 deletions.
67 changes: 35 additions & 32 deletions .github/workflows/nextjs_bundle_analysis.yml
Original file line number Diff line number Diff line change
@@ -1,58 +1,63 @@
name: "Next.js Bundle Analysis"
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

name: 'Next.js Bundle Analysis'

on:
push:
branches:
- main
- rec
pull_request:
push:
branches:
- main
- rec
- main # change this if your default branch is named differently
workflow_dispatch:

defaults:
run:
# change this if your nextjs app does not live at the root of the repo
working-directory: ./

permissions:
contents: read # for checkout repository
actions: read # for fetching base branch bundle stats
pull-requests: write # for comments

jobs:
analyze:
permissions:
pull-requests: write
env:
SKIP_ENV_VALIDATION: true
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3

- name: Setup Node.js
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: "18.x"
cache: "npm"
node-version: 18

- name: Install dependencies
run: npm ci
uses: bahmutov/npm-install@v1

- name: Set example env
run: cp .env.example .env
# If pnpm is used, you need to switch the previous step with the following one. pnpm does not create a package-lock.json
# so the step above will fail to pull dependencies
# - uses: pnpm/action-setup@v2
# name: Install pnpm
# id: pnpm-install
# with:
# version: 7
# run_install: true

- name: Restore next build
uses: actions/cache@v3
id: restore-build-cache
env:
cache-name: cache-next-build
with:
# if you use a custom build directory, replace all instances of `.next` in this file with your build directory
# ex: if your app builds to `dist`, replace `.next` with `dist`
path: .next/cache
# change this if you prefer a more strict cache
key: ${{ runner.os }}-build-${{ env.cache-name }}

- name: Build next.js app
env:
SKIP_BUILD_PRODUCT_REDIRECTS: 1
# change this if your site requires a custom build command
run: npm run build
run: ./node_modules/.bin/next build

# Here's the first place where next-bundle-analysis' own script is used
# This step pulls the raw bundle stats for the current bundle
Expand Down Expand Up @@ -90,34 +95,32 @@ jobs:
if: success() && github.event.number
run: ls -laR .next/analyze/base && npx -p nextjs-bundle-analysis compare

- name: Get comment body
- name: Get Comment Body
id: get-comment-body
if: success() && github.event.number
uses: actions/github-script@v6
with:
result-encoding: string
script: |
const fs = require('fs')
const comment = fs.readFileSync('.next/analyze/__bundle_analysis_comment.txt', 'utf8')
core.setOutput('body', comment)
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
run: |
echo "body<<EOF" >> $GITHUB_OUTPUT
echo "$(cat .next/analyze/__bundle_analysis_comment.txt)" >> $GITHUB_OUTPUT
echo EOF >> $GITHUB_OUTPUT
- name: Find Comment
uses: peter-evans/find-comment@v2
if: success() && github.event.number
id: fc
with:
issue-number: ${{ github.event.number }}
body-includes: "<!-- __NEXTJS_BUNDLE -->"
body-includes: '<!-- __NEXTJS_BUNDLE -->'

- name: Create Comment
uses: peter-evans/create-or-update-comment@v3
uses: peter-evans/create-or-update-comment@v2
if: success() && github.event.number && steps.fc.outputs.comment-id == 0
with:
issue-number: ${{ github.event.number }}
body: ${{ steps.get-comment-body.outputs.body }}

- name: Update Comment
uses: peter-evans/create-or-update-comment@v3
uses: peter-evans/create-or-update-comment@v2
if: success() && github.event.number && steps.fc.outputs.comment-id != 0
with:
issue-number: ${{ github.event.number }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
with:
node-version: "lts/*"
- name: Install dependencies
run: npm clean-install
run: npm ci
- name: Verify the integrity of provenance attestations and registry signatures for installed dependencies
run: npm audit signatures
- name: Release
Expand Down
2 changes: 1 addition & 1 deletion .releaserc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
branches: ["main"],
branches: ["rec", "main"],
"plugins":
[
["@semantic-release/npm", { "npmPublish": false }],
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,11 @@
"prisma": {
"schema": "prisma/schema.prisma",
"seed": "npx ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
},
"nextBundleAnalysis": {
"budget": 358400,
"budgetPercentIncreaseRed": 20,
"minimumChangeThreshold": 0,
"showDetails": true
}
}
}
40 changes: 40 additions & 0 deletions scripts/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,46 @@ const basicFiles = [
return content.replaceAll(`${oldRuntime.npm} `, `${newRuntime.npm} `)
},
},
{
path: ".github/workflows/check.yml",
replace: (oldRuntime: IRuntime, newRuntime: IRuntime, content: string) => {
content = content.replaceAll(`${oldRuntime.npm} ci`, `${newRuntime.npm} install --production`)
content = content.replaceAll(`${oldRuntime.npm} `, `${newRuntime.npm} `)
return content.replaceAll(
` - name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: "18.x"
cache: "npm"`,
` - name: Install bun
uses: oven-sh/setup-bun@v1`
)
},
},
{
path: ".github/workflows/nextjs_bundle_analysis.yml",
replace: (oldRuntime: IRuntime, newRuntime: IRuntime, content: string) => {
content = content.replaceAll(`${oldRuntime.npm} ci`, `${newRuntime.npm} install --production`)
content = content.replaceAll(`${oldRuntime.npm} `, `${newRuntime.npm} `)
content = content.replaceAll(`${oldRuntime.npx} `, `${newRuntime.npx} `)
return content.replaceAll(
` - name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: "18.x"
cache: "npm"`,
` - name: Install bun
uses: oven-sh/setup-bun@v1`
)
},
},
{
path: ".github/workflows/release.yml",
replace: (oldRuntime: IRuntime, newRuntime: IRuntime, content: string) => {
content = content.replaceAll(`${oldRuntime.npm} ci`, `${newRuntime.npm} install --production`)
return content.replaceAll(`${oldRuntime.npx} `, `${newRuntime.npx} `)
},
},
]

const processBasicFiles = async (currentRuntime: IRuntime, newRuntime: IRuntime) => {
Expand Down

0 comments on commit fd0c468

Please sign in to comment.