Skip to content

Commit

Permalink
feat: Add paths parameter for skip (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent committed Jun 24, 2024
2 parents 057b017 + b6693c2 commit 3800eba
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 16 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ jobs:
- uses: kunitsucom/github-actions-paths-ignore-alternative@develop
id: paths-ignore
with:
paths: |-
# If any of these regular expressions match, it returns skip=false
# This setting takes precedence over paths-ignore
^action.yml
paths-ignore: |-
# substrings of file paths to ignore written in regular expressions
# If any of these regular expressions match, it returns skip=true
^.github/
^.*\.md$
^.*\.log$
Expand Down
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

A workaround workflow to resolve the incompatibility of the `Require status checks to pass` setting when `paths-ignore` is set for push trigger or pull_request trigger in GitHub Actions.

## `paths-ignore` example

```yml
name: example

Expand Down Expand Up @@ -46,3 +48,49 @@ jobs:
run: |
echo "needs.paths-ignore.outputs.skip: ${{ needs.paths-ignore.outputs.skip }}"
```
## `paths` example

```yml
name: example
on:
push:
# NO paths-ignore
pull_request:
# NO paths-ignore
workflow_dispatch:
jobs:
paths-ignore:
runs-on: ubuntu-latest
outputs:
skip: ${{ steps.paths-ignore.outputs.skip }}
steps:
- uses: kunitsucom/github-actions-paths-ignore-alternative@main
id: paths-ignore
with:
paths: |-
# If any of these regular expressions match, it returns skip=false
# This setting takes precedence over paths-ignore
^action.yml
# > Note: A job that is skipped will report its status as "Success".
# > It will not prevent a pull request from merging, even if it is a required check.
# ref. https://docs.github.com/en/actions/using-jobs/using-conditions-to-control-job-execution#overview
not-skip:
runs-on: ubuntu-latest
needs: paths-ignore
if: ${{ needs.paths-ignore.outputs.skip != 'true' }}
steps:
- name: "if not skip"
run: |
echo "needs.paths-ignore.outputs.skip: ${{ needs.paths-ignore.outputs.skip }}"
skip:
runs-on: ubuntu-latest
needs: paths-ignore
if: ${{ needs.paths-ignore.outputs.skip == 'true' }}
steps:
- name: "if skip"
run: |
echo "needs.paths-ignore.outputs.skip: ${{ needs.paths-ignore.outputs.skip }}"
```
52 changes: 37 additions & 15 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ inputs:
#
# Common
#
paths:
description: 'A list of substrings of file paths to include written in regular expressions (like on.push.paths)'
required: false
default: '' # empty string
paths-ignore:
description: 'A list of substrings of file paths to ignore written in regular expressions (like on.push.paths-ignore)'
required: false
Expand Down Expand Up @@ -62,6 +66,8 @@ runs:
- name: github-actions-paths-ignore-alternative
id: paths-ignore
shell: 'bash'
env:
LOGSH_COLOR: 'true'
run: |
# LICENSE: https://github.com/kunitsucom/log.sh/blob/HEAD/LICENSE
# Common
Expand Down Expand Up @@ -103,14 +109,6 @@ runs:
#
# main
#
LogshInfo "Set skip=false as default"
LogshExec echo "skip=false" >> $GITHUB_OUTPUT
LogshInfo "Create paths-ignore.txt (without comments and empty lines)"
(grep -v -e "^[[:space:]]*#" -e "^[[:space:]]*$" <<'EOF' | tee paths-ignore.txt) || true
${{ inputs.paths-ignore }}
EOF
if [[ "${{ github.event_name }}" == 'push' ]]; then
LogshInfo "${{ github.event_name }} event"
before="${{ inputs.before }}"
Expand All @@ -124,24 +122,48 @@ runs:
exit 0
fi
if ! diff_files=$(git diff --name-only "${before:?}" "${after:?}"); then
LogshDebug "Create paths.txt (without comments and empty lines)"
(grep -v -e "^[[:space:]]*#" -e "^[[:space:]]*$" <<'EOF' | tee paths.txt) || true
${{ inputs.paths }}
EOF
LogshDebug "Create paths-ignore.txt (without comments and empty lines)"
(grep -v -e "^[[:space:]]*#" -e "^[[:space:]]*$" <<'EOF' | tee paths-ignore.txt) || true
${{ inputs.paths-ignore }}
EOF
if ! files_changed=$(git diff --name-only "${before:?}" "${after:?}"); then
LogshWarn "Failed to get diff files. FORCE NOT SKIP."
exit 0
fi
LogshDebug "==> files_changed <=="
LogshDebug "${files_changed:-(empty)}"
if [[ -n "${{ inputs.paths-ignore }}" ]]; then
files_changed=$(echo "${diff_files:?}" | grep -Ev -f paths-ignore.txt) || true
LogshDebug "==> files_changed <=="
if [[ -n "${{ inputs.paths }}" ]] && [[ -n "${{ inputs.paths-ignore }}" ]]; then
LogshWarn "Both paths and paths-ignore are set. paths takes precedence."
fi
if [[ -n "${{ inputs.paths }}" ]]; then
LogshInfo "Apply paths"
files_changed=$(echo "${files_changed:?}" | grep -E -f paths.txt) || true
LogshDebug "==> files_changed (applied paths) <=="
LogshDebug "${files_changed:-(empty)}"
elif [[ -n "${{ inputs.paths-ignore }}" ]]; then
LogshInfo "Apply paths-ignore"
files_changed=$(echo "${files_changed:?}" | grep -Ev -f paths-ignore.txt) || true
LogshDebug "==> files_changed (applied paths-ignore) <=="
LogshDebug "${files_changed:-(empty)}"
else
LogshInfo "paths-ignore is empty"
files_changed=$(echo "${diff_files:?}")
LogshWarn "paths and paths-ignore are not set."
files_changed=$(echo "${files_changed:?}")
fi
if [[ -z "${files_changed:-}" ]]; then
LogshInfo "Skip the following steps"
LogshExec echo "skip=true" >> $GITHUB_OUTPUT
exit 0
else
LogshInfo "Continue the following steps"
LogshExec echo "skip=false" >> $GITHUB_OUTPUT
fi
- name: DEBUG
if: always() && ${{ inputs.debug == 'true' }}
Expand Down

0 comments on commit 3800eba

Please sign in to comment.