check CI check works as intended #18
Workflow file for this run
This file contains 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
name: Check Node Modules Size | |
# https://stackoverflow.com/questions/68737385/how-to-trigger-github-actions-on-push-of-current-branch/68737519#68737519 | |
on: [push] | |
jobs: | |
analyze-size: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Setup Node.js | |
uses: actions/setup-node@v2 | |
with: | |
node-version: 18 | |
- name: Install dependencies | |
run: npm install | |
- 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 }} | |
# 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: Checkout the previous branch so getting `context.sha` works correctly. | |
run: git checkout - | |
- name: Print sizes | |
run: | | |
echo "Main branch size: $MAIN_SIZE" | |
echo "Current branch size: $CURRENT_SIZE" | |
- name: Compare sizes and send alert | |
if: ${{ env.CURRENT_SIZE != env.MAIN_SIZE }} | |
run: | | |
echo "Node Modules size has changed!" | |
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. | |
- name: Create comment with output | |
if: ${{ env.CURRENT_SIZE != env.MAIN_SIZE }} | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const body = `\`node_modules/\` has changed in size.\n | |
Main branch size: ${process.env.MAIN_SIZE}\n | |
Current branch size: ${process.env.CURRENT_SIZE}` | |
const { owner, repo } = context.repo; | |
const commit_sha = context.sha; | |
console.log('commit_sha:', commit_sha); | |
console.log('owner:', owner); | |
console.log('repo:', repo); | |
const { data } = await github.rest.repos.listPullRequestsAssociatedWithCommit({ | |
commit_sha, | |
owner, | |
repo, | |
}); | |
console.log('data is', data); | |
const issue_number = data[0].number; | |
console.log('issue_number is', issue_number); | |
// https://stackoverflow.com/questions/58066966/commenting-a-pull-request-in-a-github-action/76215842#76215842 | |
github.rest.issues.createComment({ | |
body, | |
issue_number, | |
owner, | |
repo, | |
}); |