From 2f84b511a7f1d40ad6a87cec1c5551722566562e Mon Sep 17 00:00:00 2001 From: KastTrifork Date: Wed, 27 Nov 2024 20:47:26 +0100 Subject: [PATCH] chore(linter): add check for changes before committing linter fixes in mega-linter workflow --- .github/workflows/mega-linter.yml | 34 ++++++++++++++++--------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/.github/workflows/mega-linter.yml b/.github/workflows/mega-linter.yml index 5d4a2ad..383fdac 100644 --- a/.github/workflows/mega-linter.yml +++ b/.github/workflows/mega-linter.yml @@ -90,19 +90,25 @@ jobs: run: | echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}" echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}" + + - name: Check for changes + id: git_status + run: | + if git diff --quiet; then + echo "changes=false" >> $GITHUB_ENV + echo "No changes detected." + else + echo "Changes detected" + echo "changes=true" >> $GITHUB_ENV + fi # Push new commit if applicable (for now works only on PR from same repository, not from forks) - name: Prepare commit - if: steps.ml.outputs.has_updated_sources == 1 && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'commit' && github.ref != 'refs/heads/main' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository) + if: ${{ steps.ml.outputs.has_updated_sources == 1 && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'commit' && github.ref != 'refs/heads/main' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository) && !contains(github.event.head_commit.message, 'skip fix') && env.changes == 'true' }} run: sudo chown -Rc $UID .git/ - name: Setup git and add changes - if: steps.ml.outputs.has_updated_sources == 1 && - (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && - env.APPLY_FIXES_MODE == 'commit' && - github.ref != 'refs/heads/main' && - (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository) && - !contains(github.event.head_commit.message, 'skip fix') + if: ${{ steps.ml.outputs.has_updated_sources == 1 && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'commit' && github.ref != 'refs/heads/main' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository) && !contains(github.event.head_commit.message, 'skip fix') && env.changes == 'true' }} env: branch_name: ${{ github.head_ref || github.ref_name }} run: | @@ -112,20 +118,16 @@ jobs: git add . - name: Commit applied linter fixes - if: steps.ml.outputs.has_updated_sources == 1 && - (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && - env.APPLY_FIXES_MODE == 'commit' && - github.ref != 'refs/heads/main' && - (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository) && - !contains(github.event.head_commit.message, 'skip fix') + if: ${{ steps.ml.outputs.has_updated_sources == 1 && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'commit' && github.ref != 'refs/heads/main' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository) && !contains(github.event.head_commit.message, 'skip fix') && env.changes == 'true' }} id: commit uses: qoomon/actions--create-commit@v1 with: message: "[MegaLinter] Apply linters fixes" allow-empty: true - - name: Pull with rebase and Push changes - if: steps.commit.outputs.commit + - name: Push changes + if: ${{ steps.commit.outputs.commit || env.changes == 'true' }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - git pull --rebase origin ${{ github.head_ref || github.ref_name }} git push origin ${{ github.head_ref || github.ref_name }}