Skip to content

Commit

Permalink
Move files over from main Antrea repository
Browse files Browse the repository at this point in the history
Signed-off-by: Antonin Bas <abas@vmware.com>
  • Loading branch information
antoninbas committed Feb 28, 2022
1 parent 7a52732 commit 2ef01c4
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM ubuntu:20.04

LABEL maintainer="Antrea <projectantrea-dev@googlegroups.com>"
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"]
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 9 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -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'
71 changes: 71 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2ef01c4

Please sign in to comment.