I have two Github actions one for auto formating and another for linting:
Auto lint
name: Lint Changed Files
on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
lint:
name: Lint Changed Files
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '20'
- name: Install pnpm
run: npm install -g pnpm
- name: Install dependencies
run: pnpm install
- name: Get changed files
id: changed-files
run: |
echo "${{ github.event.pull_request.base.sha }}" > base_sha.txt
echo "${{ github.event.pull_request.head.sha }}" > head_sha.txt
git diff --name-only $(cat base_sha.txt) $(cat head_sha.txt) > changed_files.txt
- name: Create lint script for changed files
run: |
# Read the list of changed files
changed_files=$(cat changed_files.txt)
# Initialize an empty array to hold files to be linted
files_to_lint=()
# Loop over each changed file
for file in $changed_files; do
# Add JS/TS files to the list
if [[ $file == *.js || $file == *.ts || $file == *.tsx || $file == *.cjs || $file == *.mjs || $file == *.astro || $file == *.spec.ts || $file == *.mdx ]]; then
files_to_lint+=($file)
fi
done
# Create a temporary lint script if there are files to lint
if [ ${#files_to_lint[@]} -eq 0 ]; then
echo "No JavaScript or TypeScript files were changed." > lint.sh
else
echo "pnpm run lint:check -- ${files_to_lint[@]}" > lint.sh
fi
chmod +x lint.sh
- name: Run linter on changed files
run: ./lint.sh
Auto format
name: Autoformat Changed Files
on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
autoformat:
name: Autoformat Changed Files
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '20'
- name: Install pnpm
run: npm install -g pnpm
- name: Install dependencies
run: pnpm install
- name: Get changed files
id: changed-files
run: |
echo "${{ github.event.pull_request.base.sha }}" > base_sha.txt
echo "${{ github.event.pull_request.head.sha }}" > head_sha.txt
git diff --name-only $(cat base_sha.txt) $(cat head_sha.txt) > changed_files.txt
- name: Create format script for changed files
run: |
# Read the list of changed files
changed_files=$(cat changed_files.txt)
# Initialize an empty array to hold files to be formatted
files_to_format=()
# Loop over each changed file
for file in $changed_files; do
# Add JS/TS files to the list
if [[ $file == *.js || $file == *.ts || $file == *.tsx || $file == *.cjs || $file == *.mjs || $file == *.astro || $file == *.spec.ts || $file == *.mdx ]]; then
files_to_format+=($file)
fi
done
# Create a temporary format script if there are files to format
if [ ${#files_to_format[@]} -eq 0 ]; then
echo "No JavaScript or TypeScript files were changed." > format.sh
else
echo "pnpm run format -- ${files_to_format[@]}" > format.sh
fi
chmod +x format.sh
- name: Run formatter on changed files
run: ./format.sh
- name: Commit formatted changes
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git add .
git commit -m 'Autoformat changed files' || echo "No changes to commit"
git push origin HEAD:${{ github.head_ref }}
When running these two with Act and github actions they fail with the error:
[Lint Changed Files/Lint Changed Files ] ✅ Success - Main Install dependencies
[Lint Changed Files/Lint Changed Files ] ⭐ Run Main Get changed files
[Lint Changed Files/Lint Changed Files ] ???? docker exec cmd=[bash --noprofile --norc -e -o pipefail /var/run/act/workflow/changed-files] user= workdir=
[Lint Changed Files/Lint Changed Files ] ✅ Success - Main Get changed files
[Lint Changed Files/Lint Changed Files ] ⭐ Run Main Create lint script for changed files
[Lint Changed Files/Lint Changed Files ] ???? docker exec cmd=[bash --noprofile --norc -e -o pipefail /var/run/act/workflow/5] user= workdir=
[Lint Changed Files/Lint Changed Files ] ✅ Success - Main Create lint script for changed files
[Lint Changed Files/Lint Changed Files ] ⭐ Run Main Run linter on changed files
[Lint Changed Files/Lint Changed Files ] ???? docker exec cmd=[bash --noprofile --norc -e -o pipefail /var/run/act/workflow/6] user= workdir=
| ./lint.sh: line 1: No: command not found
[Lint Changed Files/Lint Changed Files ] ❌ Failure - Main Run linter on changed files
[Lint Changed Files/Lint Changed Files ] exitcode '127': command not found, please refer to https://github.com/nektos/act/issues/107 for more information
[Lint Changed Files/Lint Changed Files ] ???? Job failed
[run-tests/format changed files ] ❓ ::endgroup::
[run-tests/format changed files ] ❓ add-matcher /run/act/actions/actions-setup-node@v4/.github/tsc.json
[run-tests/format changed files ] ❓ add-matcher /run/act/actions/actions-setup-node@v4/.github/eslint-stylish.json
[run-tests/format changed files ] ❓ add-matcher /run/act/actions/actions-setup-node@v4/.github/eslint-compact.json
[run-tests/format changed files ] ✅ Success - Main actions/setup-node@v4
[run-tests/format changed files ] ⚙ ::set-output:: node-version=v20.14.0
[run-tests/format changed files ] ⚙ ::add-path:: /opt/hostedtoolcache/node/20.14.0/arm64/bin
[run-tests/format changed files ] ⭐ Run Main actions/checkout@v4
[run-tests/format changed files ] ???? docker cp src=/Users/ekrich/git/eTech-UI/. dst=/Users/ekrich/git/eTech-UI
| npm error code ENOTEMPTY
| npm error syscall rename
| npm error path /opt/hostedtoolcache/node/20.14.0/arm64/lib/node_modules/pnpm
| npm error dest /opt/hostedtoolcache/node/20.14.0/arm64/lib/node_modules/.pnpm-i4BzBorb
| npm error errno -39
| npm error ENOTEMPTY: directory not empty, rename '/opt/hostedtoolcache/node/20.14.0/arm64/lib/node_modules/pnpm' -> '/opt/hostedtoolcache/node/20.14.0/arm64/lib/node_modules/.pnpm-i4BzBorb'
|
| npm error A complete log of this run can be found in: /root/.npm/_logs/2024-06-08T03_31_06_800Z-debug-0.log
[Autoformat Changed Files/Autoformat Changed Files] ❌ Failure - Main Install pnpm
[Autoformat Changed Files/Autoformat Changed Files] exitcode '217': failure
[Autoformat Changed Files/Autoformat Changed Files] ???? Job failed
[run-tests/format changed files ] ✅ Success - Main actions/checkout@v4
[run-tests/format changed files ] ⭐ Run Main pnpm install
Any idea what is wrong here? Is there a pre-made action I can use instead? I’ve tried using actions/changed-files but that doesn’t work either.
Refrences I’ve looked at but still had no luck in:
- Get modified files in Github actions
- https://github.com/marketplace/actions/github-action-auto-format