Skip to content

Commit

Permalink
ci: check change in size of node_modules/
Browse files Browse the repository at this point in the history
  • Loading branch information
hamirmahal committed Aug 19, 2023
1 parent 65e85db commit c9e9835
Showing 1 changed file with 63 additions and 10 deletions.
73 changes: 63 additions & 10 deletions .github/workflows/check-node-modules-size.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,77 @@ jobs:
node-version: 18

- name: Install dependencies
run: yarn install
run: npm install

- name: Get main branch node_modules size
id: main_size
run: echo "::set-output name=size::$(du -sh node_modules | awk '{print $1}')"
- name: Analyze `node_modules` size
id: current_size
run: echo "CURRENT_SIZE=$(du -sh node_modules | awk '{print $1}')" >> $GITHUB_ENV
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Analyze node_modules size
id: current_size
run: echo "::set-output name=size::$(du -sh node_modules | awk '{print $1}')"
# Checkout the main branch and get the size of its `node_modules` directory.
- name: Checkout main branch
uses: actions/checkout@v2
with:
ref: main
- name: Remove `node_modules` for a clean slate on the main branch.
run: rm -rf node_modules/
- name: Reinstall dependencies on the main branch so we can check the size of `node_modules`.
run: npm install
- name: Get the size of `node_modules` on the main branch.
id: main_size
run: echo "MAIN_SIZE=$(du -sh node_modules | awk '{print $1}')" >> $GITHUB_ENV
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Print sizes
run: |
echo "Main branch size: $MAIN_SIZE"
echo "Current branch size: $CURRENT_SIZE"
- name: Get pull request number
id: get_pr_number
run: |
echo "$(node --eval 'console.log(process.env.GITHUB_EVENT_PATH)')"
echo "$(node --eval 'console.log(process.env.GITHUB_EVENT_PATH)')" >> $GITHUB_ENV
- name: Compare sizes and send alert
if: ${{ steps.current_size.outputs.size != steps.main_size.outputs.size }}
if: ${{ env.CURRENT_SIZE != env.MAIN_SIZE }}
run: |
echo "Node Modules size has changed!"
echo "Main branch size: ${{ steps.main_size.outputs.size }}"
echo "Current branch size: ${{ steps.current_size.outputs.size }}"
echo "Main branch size: $MAIN_SIZE"
echo "Current branch size: $CURRENT_SIZE"
# Add your alerting mechanism here, such as sending a Slack notification, creating an issue, etc.
# We have to checkout the current branch again because listing pull requests associated with a commit
# will fail otherwise.
# This block prevents the following error:
# TypeError: Cannot read properties of undefined (reading 'number')
- name: Checkout current branch
uses: actions/checkout@v2
with:
ref: ${{ github.head_ref }}

- name: Create comment with output
if: ${{ env.CURRENT_SIZE != env.MAIN_SIZE }}
uses: actions/github-script@v6
with:
script: |
const body = `\`node_modules/\` size has changed!\n
Main branch size: ${process.env.MAIN_SIZE}\n
Current branch size: ${process.env.CURRENT_SIZE}`
const issue_number = process.env.PR_NUMBER;
console.log('issue_number:', issue_number);
console.log('data is', (await github.rest.repos.listPullRequestsAssociatedWithCommit({
commit_sha: context.sha,
owner: context.repo.owner,
repo: context.repo.repo,
})).data);
// https://stackoverflow.com/questions/58066966/commenting-a-pull-request-in-a-github-action/76215842#76215842
github.rest.issues.createComment({
body,
issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
});

0 comments on commit c9e9835

Please sign in to comment.