-
-
Save tyirvine/658e61e70591a0a0ac76f572b78f6812 to your computer and use it in GitHub Desktop.
dotnet-format for github actions (on push) - C#
This file contains hidden or 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
# This workflow ensures all code being pushed to your repo. is formatted | |
name: Auto-Format | |
# Controls when the action will run. | |
on: | |
push: | |
branches: | |
- "**" | |
# A workflow run is made up of one or more jobs that can run sequentially or in parallel | |
jobs: | |
dotnet-format: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Get Branch Info | |
shell: bash | |
run: echo "##[set-output name=ref;]$(echo ${GITHUB_REF#refs/heads/})" | |
id: comment-branch | |
- name: Checkout repo | |
uses: actions/[email protected] | |
with: | |
ref: ${{ steps.comment-branch.outputs.ref }} | |
persist-credentials: false | |
- name: Check latest commit message | |
id: log | |
run: echo "::set-output name=message::$(git log --no-merges -1 --oneline)" | |
- name: Check Assets/Scripts folder existence | |
id: check_folder | |
uses: andstor/[email protected] | |
with: | |
files: "./Assets/Scripts/" | |
- name: Install dotnet-format | |
if: "!contains(steps.log.outputs.message, 'skip ci') && steps.check_folder.outputs.files_exists == 'true'" | |
run: dotnet tool install -g dotnet-format | |
- name: Check format | |
if: "!contains(steps.log.outputs.message, 'skip ci') && steps.check_folder.outputs.files_exists == 'true'" | |
id: check | |
uses: brmenchl/[email protected] | |
with: | |
repo-token: ${{ secrets.GITHUB_TOKEN }} | |
action: "check" | |
folder: ./Assets/Scripts | |
fail-fast: false | |
- name: Run format | |
if: steps.check.outputs.has-changes == 'true' | |
uses: brmenchl/[email protected] | |
with: | |
repo-token: ${{ secrets.GITHUB_TOKEN }} | |
action: "fix" | |
folder: ./Assets/Scripts | |
- name: Commit files | |
if: steps.check.outputs.has-changes == 'true' | |
run: | | |
git config --local user.name "github-actions[bot]" | |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
git commit -a -m 'Automated dotnet-format update [skip ci]' -m 'Co-authored-by: ${{ github.event.pull_request.user.login }} <${{ github.event.pull_request.user.id }}+${{ github.event.pull_request.user.login }}@users.noreply.github.com>' | |
- name: Push changes | |
if: steps.check.outputs.has-changes == 'true' | |
uses: ad-m/[email protected] | |
with: | |
branch: ${{ steps.comment-branch.outputs.ref }} | |
github_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wasn't using this with pull requests so needed a different way to find branch info. Thankfully this stackoverflow post saved the day for me!
https://stackoverflow.com/questions/58033366/how-to-get-current-branch-within-github-actions