Skip to content

Commit 665f5a0

Browse files
fix: support no changes (#67)
Co-authored-by: Adam Bannach <113929542+abannachGrafana@users.noreply.github.com>
1 parent 51f759d commit 665f5a0

2 files changed

Lines changed: 103 additions & 2 deletions

File tree

.github/workflows/test.yml

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,4 +450,93 @@ jobs:
450450
exit 1
451451
fi
452452
453-
echo "Validation passed: changedFilesIfAvailable is $changedFilesIfAvailable."
453+
echo "Validation passed: changedFilesIfAvailable is $changedFilesIfAvailable."
454+
455+
test-fail-if-no-changes: # make sure the action works on a clean machine without building
456+
runs-on: ubuntu-latest
457+
steps:
458+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
459+
- name: Setup test branch
460+
id: setup-test-branch
461+
run: |
462+
BRANCH_NAME="test_failure_no-changes-$(date +%s)"
463+
464+
git config --global user.name 'github-actions[bot]'
465+
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
466+
467+
git checkout -b $BRANCH_NAME
468+
git push --set-upstream origin $BRANCH_NAME
469+
470+
# output status here to manually verify file changes
471+
git status --porcelain=v2 --branch --untracked-files=no
472+
473+
echo "branch-name=$BRANCH_NAME" >> $GITHUB_OUTPUT
474+
- uses: ./
475+
id: test-action
476+
continue-on-error: true
477+
with:
478+
token: ${{ github.token }}
479+
commit-message: ${{ steps.setup-test-branch.outputs.branch-name }}
480+
- name: Check output is failure
481+
if: ${{ failure() }}
482+
run: |
483+
if [[ -z "${{ steps.test-action.outputs.commit-response }}" ]]; then
484+
echo "Validation passed: commit-response is empty."
485+
else
486+
echo "Error: commit-response is expected to be empty but got not empty."
487+
exit 1
488+
fi
489+
- name: Check output is success
490+
if: ${{ success() }}
491+
run: |
492+
echo "Error: action status is expected to be failure but got success."
493+
exit 1
494+
- name: Delete test branch
495+
if: ${{ always() }}
496+
run: |
497+
git push --force --delete origin ${{ steps.setup-test-branch.outputs.branch-name }}
498+
499+
test-success-if-no-changes: # make sure the action works on a clean machine without building
500+
runs-on: ubuntu-latest
501+
steps:
502+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
503+
- name: Setup test branch
504+
id: setup-test-branch
505+
run: |
506+
BRANCH_NAME="test_success_no-changes-$(date +%s)"
507+
508+
git config --global user.name 'github-actions[bot]'
509+
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
510+
511+
git checkout -b $BRANCH_NAME
512+
git push --set-upstream origin $BRANCH_NAME
513+
514+
# output status here to manually verify file changes
515+
git status --porcelain=v2 --branch --untracked-files=no
516+
517+
echo "branch-name=$BRANCH_NAME" >> $GITHUB_OUTPUT
518+
- uses: ./
519+
id: test-action
520+
continue-on-error: true
521+
with:
522+
token: ${{ github.token }}
523+
success-if-no-changes: true
524+
commit-message: ${{ steps.setup-test-branch.outputs.branch-name }}
525+
- name: Check output is failure
526+
if: ${{ failure() }}
527+
run: |
528+
echo "Error: action status is expected to be success but got failure."
529+
exit 1
530+
- name: Check output is success
531+
if: ${{ success() }}
532+
run: |
533+
if [[ -z "${{ steps.test-action.outputs.commit-response }}" ]]; then
534+
echo "Validation passed: commit-response is empty."
535+
else
536+
echo "Error: commit-response is expected to be empty but got not empty."
537+
exit 1
538+
fi
539+
- name: Delete test branch
540+
if: ${{ always() }}
541+
run: |
542+
git push --force --delete origin ${{ steps.setup-test-branch.outputs.branch-name }}

action.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ inputs:
1414
required: true
1515
description: 'Whether to additionally stage all changed files in the repo prior to committing: true/false'
1616
default: 'false'
17+
success-if-no-changes:
18+
required: true
19+
description: 'Whether to return success if no changes are detected: true/false'
20+
default: 'false'
1721
token:
1822
required: true
1923
description: 'GitHub access token with permissions to write to repo'
@@ -119,7 +123,13 @@ runs:
119123
120124
if [[ "$additions" == "" && "$deletions" == "" ]]; then
121125
echo "No changes to commit"
122-
exit 1
126+
echo "any_changed=false" >> $GITHUB_OUTPUT
127+
128+
if [[ "${{ inputs.success-if-no-changes }}" == "true" ]]; then
129+
exit 0
130+
else
131+
exit 1
132+
fi
123133
fi
124134
125135
if [[ "$additions" == "" ]]; then
@@ -131,10 +141,12 @@ runs:
131141
fi
132142
133143
# Set outputs for the next step
144+
echo "any_changed=true" >> $GITHUB_OUTPUT
134145
echo "additions=$additions" >> $GITHUB_OUTPUT
135146
echo "deletions=$deletions" >> $GITHUB_OUTPUT
136147
137148
- name: Commit changes
149+
if: ${{ steps.additions-and-deletions.outputs.any_changed == 'true' }}
138150
shell: bash
139151
id: commit-changes
140152
run: |

0 commit comments

Comments
 (0)