Hoping someone can help or provide guidance. I am have been bashing my head trying to a GitHub Workflow to run Prettier against all the files that have been added / modified in a pull request, and then commit them back the pull request. I have managed to get the below going so far, but hitting an error when trying to commit the changes back to the PR.
I am new to all this so could be going about it all wrong and just cannot find a example to reference, which makes me think I am doing something wrong :)
name: Pull Request Workflow
on:
pull_request:
types: [opened, reopened, edited, synchronize]
jobs:
lint-analyse-code:
name: Lint & Analyse Code
runs-on: ubuntu-latest
steps:
- name: Step 1 - Checkout Repository
uses: actions/checkout@v3
- name: Step 2 - Setup GIT
run: |
echo "BRANCHES"
git branch -v
echo "REMOTES"
git remote -v
echo "GITHUB_REF is $GITHUB_REF"
echo "GITHUB_HEAD_REF is $GITHUB_HEAD_REF"
echo "GITHUB_BASE_REF is $GITHUB_BASE_REF"
git config advice.ignoredHook false
- name: Step 3 - Setup Node.js
uses: actions/setup-node@v3
- name: Step 4 - Install NPM Dependencies
run: npm install
- name: Step 5 - Get Changed Files
id: changed-files
uses: tj-actions/changed-files@v29.0.7
with:
since_last_remote_commit: true
- name: Step 6 - Prettier
run: npm run prettier-npx ${{ steps.changed-files.outputs.all_changed_files }}
- name: Step 7 - Commit Changes
run: |
echo HEAD:$GITHUB_HEAD_REF
git config user.email "devops@blahblah.co.za"
git config user.name "DevOps"
git add .
git commit -m "Automatically Applied Prettier [skip actions]" || echo "Nothing to push"
git push origin HEAD:$GITHUB_HEAD_REF
