diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 87cf1581..7f6a8cb9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -450,4 +450,93 @@ jobs: exit 1 fi - echo "Validation passed: changedFilesIfAvailable is $changedFilesIfAvailable." \ No newline at end of file + echo "Validation passed: changedFilesIfAvailable is $changedFilesIfAvailable." + + 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 + - name: Setup test branch + id: setup-test-branch + run: | + 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' + + 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 }} + 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 }} + + 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 + - name: Setup test branch + id: setup-test-branch + run: | + 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' + + 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 }} + 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." + else + 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 }} diff --git a/action.yml b/action.yml index bbd2e8e3..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' @@ -119,7 +123,13 @@ runs: if [[ "$additions" == "" && "$deletions" == "" ]]; then echo "No changes to commit" - exit 1 + echo "any_changed=false" >> $GITHUB_OUTPUT + + if [[ "${{ inputs.success-if-no-changes }}" == "true" ]]; then + exit 0 + else + exit 1 + fi fi if [[ "$additions" == "" ]]; then @@ -131,10 +141,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: |