From f640b1bccf4837450f9d623e60f2ebaab7686658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eligio=20Mari=C3=B1o?= <22875166+gmeligio@users.noreply.github.com> Date: Thu, 27 Mar 2025 23:14:54 +0100 Subject: [PATCH 1/5] fix: do not error if there are no changes --- action.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/action.yml b/action.yml index bbd2e8e3..646c35d5 100644 --- a/action.yml +++ b/action.yml @@ -119,7 +119,6 @@ runs: if [[ "$additions" == "" && "$deletions" == "" ]]; then echo "No changes to commit" - exit 1 fi if [[ "$additions" == "" ]]; then From e191f697f10d45226c79bd2b4949c03f6a077f28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eligio=20Mari=C3=B1o?= <22875166+gmeligio@users.noreply.github.com> Date: Sat, 5 Apr 2025 12:55:58 +0200 Subject: [PATCH 2/5] fix: support no changes --- .github/workflows/test.yml | 41 +++++++++++++++++++++++++++++++++++++- action.yml | 4 ++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 87cf1581..610d90dc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -450,4 +450,43 @@ jobs: exit 1 fi - echo "Validation passed: changedFilesIfAvailable is $changedFilesIfAvailable." \ No newline at end of file + echo "Validation passed: changedFilesIfAvailable is $changedFilesIfAvailable." + + test-no-changes: # make sure the action works on a clean machine without building + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + ref: ${{ github.head_ref || github.ref }} + - name: Setup test branch + id: setup-test-branch + run: | + BRANCH_NAME="test_no-changes-$(date +%s)" + + git config --global user.name 'github-actions[bot]' + git config --global user.email 'github-actions[bot]@users.noreply.github.com' + + git checkout -b $BRANCH_NAME + git push --set-upstream origin $BRANCH_NAME + + # output status here to manually verify file changes + git status --porcelain=v2 --branch --untracked-files=no + + echo "branch-name=$BRANCH_NAME" >> $GITHUB_OUTPUT + - uses: ./ + id: test-action + with: + token: ${{ github.token }} + stage-all-files: true + commit-message: ${{ steps.setup-test-branch.outputs.branch-name }} + - name: Delete test branch + run: | + git push --force --delete origin ${{ steps.setup-test-branch.outputs.branch-name }} + - name: Check output + run: | + if [[ -z "${{ steps.test-action.outputs.commit-response }}" ]]; then + echo "Validation passed: commit-response is empty." + else + echo "Error: commit-response is expected to be empty but got not empty." + exit 1 + fi diff --git a/action.yml b/action.yml index 646c35d5..48cfcbce 100644 --- a/action.yml +++ b/action.yml @@ -119,6 +119,8 @@ runs: if [[ "$additions" == "" && "$deletions" == "" ]]; then echo "No changes to commit" + echo "any_changed=false" >> $GITHUB_OUTPUT + exit 0 fi if [[ "$additions" == "" ]]; then @@ -130,10 +132,12 @@ runs: fi # Set outputs for the next step + echo "any_changed=true" >> $GITHUB_OUTPUT echo "additions=$additions" >> $GITHUB_OUTPUT echo "deletions=$deletions" >> $GITHUB_OUTPUT - name: Commit changes + if: ${{ steps.additions-and-deletions.outputs.any_changed == 'true' }} shell: bash id: commit-changes run: | From 1c970467071e90ca51e278b373aaf4c811e99c24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eligio=20Mari=C3=B1o?= <22875166+gmeligio@users.noreply.github.com> Date: Thu, 10 Apr 2025 18:49:07 +0200 Subject: [PATCH 3/5] feat: add success-if-no-changes --- .github/workflows/test.yml | 61 ++++++++++++++++++++++++++++++++++++-- action.yml | 11 ++++++- 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 610d90dc..a3a065d3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -452,7 +452,7 @@ jobs: echo "Validation passed: changedFilesIfAvailable is $changedFilesIfAvailable." - test-no-changes: # make sure the action works on a clean machine without building + test-fail-if-no-changes: # make sure the action works on a clean machine without building runs-on: ubuntu-latest steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 @@ -475,14 +475,67 @@ jobs: echo "branch-name=$BRANCH_NAME" >> $GITHUB_OUTPUT - uses: ./ id: test-action + continue-on-error: true with: token: ${{ github.token }} stage-all-files: true commit-message: ${{ steps.setup-test-branch.outputs.branch-name }} + - name: Check output is failure + if: ${{ failure() }} + run: | + if [[ -z "${{ steps.test-action.outputs.commit-response }}" ]]; then + echo "Validation passed: commit-response is empty." + else + echo "Error: commit-response is expected to be empty but got not empty." + exit 1 + fi + - name: Check output is success + if: ${{ success() }} + run: | + echo "Error: action status is expected to be failure but got success." + exit 1 - name: Delete test branch + if: ${{ always() }} run: | git push --force --delete origin ${{ steps.setup-test-branch.outputs.branch-name }} - - name: Check output + + + test-success-if-no-changes: # make sure the action works on a clean machine without building + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + ref: ${{ github.head_ref || github.ref }} + - name: Setup test branch + id: setup-test-branch + run: | + BRANCH_NAME="test_no-changes-$(date +%s)" + + git config --global user.name 'github-actions[bot]' + git config --global user.email 'github-actions[bot]@users.noreply.github.com' + + git checkout -b $BRANCH_NAME + git push --set-upstream origin $BRANCH_NAME + + # output status here to manually verify file changes + git status --porcelain=v2 --branch --untracked-files=no + + echo "branch-name=$BRANCH_NAME" >> $GITHUB_OUTPUT + - uses: ./ + id: test-action + continue-on-error: true + with: + token: ${{ github.token }} + stage-all-files: true + success-if-no-changes: true + commit-message: ${{ steps.setup-test-branch.outputs.branch-name }} + - name: Check output is failure + if: ${{ failure() }} + run: | + echo "Error: action status is expected to be success but got failure." + exit 1 + - name: Check output is success + if: ${{ success() }} run: | if [[ -z "${{ steps.test-action.outputs.commit-response }}" ]]; then echo "Validation passed: commit-response is empty." @@ -490,3 +543,7 @@ jobs: echo "Error: commit-response is expected to be empty but got not empty." exit 1 fi + - name: Delete test branch + if: ${{ always() }} + run: | + git push --force --delete origin ${{ steps.setup-test-branch.outputs.branch-name }} \ No newline at end of file diff --git a/action.yml b/action.yml index 48cfcbce..fb3e0faa 100644 --- a/action.yml +++ b/action.yml @@ -14,6 +14,10 @@ inputs: required: true description: 'Whether to additionally stage all changed files in the repo prior to committing: true/false' default: 'false' + success-if-no-changes: + required: true + description: 'Whether to return success if no changes are detected: true/false' + default: 'false' token: required: true description: 'GitHub access token with permissions to write to repo' @@ -120,7 +124,12 @@ runs: if [[ "$additions" == "" && "$deletions" == "" ]]; then echo "No changes to commit" echo "any_changed=false" >> $GITHUB_OUTPUT - exit 0 + + if [[ "${{ inputs.success-if-no-changes }}" == "true" ]]; then + exit 0 + else + exit 1 + fi fi if [[ "$additions" == "" ]]; then From 88056993b31b247dd5767729f882515ddf34cf08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eligio=20Mari=C3=B1o?= <22875166+gmeligio@users.noreply.github.com> Date: Thu, 10 Apr 2025 18:52:32 +0200 Subject: [PATCH 4/5] chore: add line at end of file --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a3a065d3..404897ed 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -546,4 +546,4 @@ jobs: - name: Delete test branch if: ${{ always() }} run: | - git push --force --delete origin ${{ steps.setup-test-branch.outputs.branch-name }} \ No newline at end of file + git push --force --delete origin ${{ steps.setup-test-branch.outputs.branch-name }} From b2c0b64b5a8d21166e77f93609d13ddec77fe523 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eligio=20Mari=C3=B1o?= <22875166+gmeligio@users.noreply.github.com> Date: Thu, 10 Apr 2025 19:45:15 +0200 Subject: [PATCH 5/5] Apply suggestions from code review Co-authored-by: Adam Bannach <113929542+abannachGrafana@users.noreply.github.com> --- .github/workflows/test.yml | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 404897ed..7f6a8cb9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -456,12 +456,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - ref: ${{ github.head_ref || github.ref }} - name: Setup test branch id: setup-test-branch run: | - BRANCH_NAME="test_no-changes-$(date +%s)" + BRANCH_NAME="test_failure_no-changes-$(date +%s)" git config --global user.name 'github-actions[bot]' git config --global user.email 'github-actions[bot]@users.noreply.github.com' @@ -478,7 +476,6 @@ jobs: continue-on-error: true with: token: ${{ github.token }} - stage-all-files: true commit-message: ${{ steps.setup-test-branch.outputs.branch-name }} - name: Check output is failure if: ${{ failure() }} @@ -499,17 +496,14 @@ jobs: run: | git push --force --delete origin ${{ steps.setup-test-branch.outputs.branch-name }} - test-success-if-no-changes: # make sure the action works on a clean machine without building runs-on: ubuntu-latest steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - ref: ${{ github.head_ref || github.ref }} - name: Setup test branch id: setup-test-branch run: | - BRANCH_NAME="test_no-changes-$(date +%s)" + BRANCH_NAME="test_success_no-changes-$(date +%s)" git config --global user.name 'github-actions[bot]' git config --global user.email 'github-actions[bot]@users.noreply.github.com' @@ -526,7 +520,6 @@ jobs: continue-on-error: true with: token: ${{ github.token }} - stage-all-files: true success-if-no-changes: true commit-message: ${{ steps.setup-test-branch.outputs.branch-name }} - name: Check output is failure