diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..97f6577 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM ubuntu:20.04 + +LABEL maintainer="Antrea " +LABEL description="A Docker-based Github action to determine if changes were made outside of a provided path exclusion list." + +RUN apt-get update && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends git jq && \ + rm -rf /var/lib/apt/lists/* + +COPY entrypoint.sh /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] diff --git a/README.md b/README.md index b0da520..49aed7a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,26 @@ -# has-changes -A GitHub action to detect if the diff for a push or pull_request event has changes outside of a list of paths +# "Has Changes" Docker Action + +This action sets a boolean output (`has_changes`) if the diff (`push` or +`pull_request` event) includes changes outside of a provided list of paths. + +## Inputs + +The list of paths to exclude. The action will use Bash pattern matching, so +wildcards (`*`) are supported. + +## Outputs + +### `has_changes` + +Whether ('yes' or 'no') the diff includes changes outside of the provided list +of paths. + +## Example usage + +```yaml +uses: antrea-io/antrea/ci/gh-actions/has-changes@main +with: + args: docs *.md ci +``` + +Make sure to checkout the repo first. diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..9cea910 --- /dev/null +++ b/action.yml @@ -0,0 +1,9 @@ +# action.yml +name: 'Has Changes' +description: 'Checks if diff includes changes outside of a provided path exclusion list' +outputs: + has_changes: + description: 'Whether (yes/no) the diff includes changes' +runs: + using: 'docker' + image: 'Dockerfile' diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..fc4840b --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,71 @@ +#!/usr/bin/env bash +# Copyright 2022 Antrea Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +set -eo pipefail + +read -r -a PATTERNS <<< "$*" + +cat "$GITHUB_EVENT_PATH" + +PR_BASE_SHA=$(jq -r '.pull_request.base.sha' "$GITHUB_EVENT_PATH") +BEFORE=$(jq -r '.before' "$GITHUB_EVENT_PATH") +if [[ $PR_BASE_SHA != "" && $PR_BASE_SHA != "null" ]]; then # PR events + SHA=$PR_BASE_SHA +elif [[ $BEFORE != "" && $BEFORE != "null" ]]; then # push events + SHA=$BEFORE +else + echo "This does not appear to be a PR or a push event" + echo "Setting 'has_changes' to 'yes'" + echo "::set-output name=has_changes::yes" + exit 0 +fi +echo "BASE SHA: $SHA" + +CHANGED_FILES=$(git diff --name-only "$SHA" HEAD) || rc=$? + +if [[ $rc -ne 0 ]]; then + echo "Error when running 'git diff'" + echo "This is expected when the repo was not checked-out properly, or after a force push" + echo "Setting 'has_changes' to 'yes'" + echo "::set-output name=has_changes::yes" + exit 0 +fi + +echo "CHANGED_FILES are:" +echo "$CHANGED_FILES" + +has_changes=false +for changed_file in $CHANGED_FILES; do + matched=false + for pattern in "${PATTERNS[@]}"; do + if [[ "$changed_file" == $pattern ]]; then + matched=true + break + fi + done + if ! $matched; then + has_changes=true + break + fi +done + +if $has_changes; then + echo "Setting 'has_changes' to 'yes'" + echo "::set-output name=has_changes::yes" +else + echo "Setting 'has_changes' to 'no'" + echo "::set-output name=has_changes::no" +fi