Skip to content

Commit 7968ed2

Browse files
committed
improve default branch detection in GitHub Actions
The `git remote show origin` command can be unreliable for determining the default branch in some CI environments, particularly for pull requests from forks. This change updates the validation script to prioritize the `GITHUB_BASE_REF` environment variable when it is available. This provides a more robust method for identifying the target branch within a GitHub Actions workflow. Additionally, the script is modified to skip the branch checkout logic when running in a PR context, as the correct branch is already checked out by the workflow.
1 parent c3a1d43 commit 7968ed2

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

bin/validate-plugin-version.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ set -euo pipefail
33
IFS=$'\n\t'
44

55
main() {
6-
# Determine the default branch
7-
DEFAULT_BRANCH=$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)
6+
if [[ -n "${GITHUB_BASE_REF:-}" ]]; then
7+
DEFAULT_BRANCH="${GITHUB_BASE_REF}"
8+
else
9+
DEFAULT_BRANCH=$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)
10+
fi
811
echo "Default branch is $DEFAULT_BRANCH"
912

1013
# Check out the specified branch if $BRANCH is set and not already on it
11-
if [[ -n "${BRANCH:-}" && "$(git rev-parse --abbrev-ref HEAD)" != "$BRANCH" ]]; then
14+
if [[ -z "${GITHUB_BASE_REF:-}" && -n "${BRANCH:-}" && "$(git rev-parse --abbrev-ref HEAD)" != "$BRANCH" ]]; then
1215
echo "Checking if branch $BRANCH exists."
1316
if ! git show-ref --verify --quiet "refs/heads/$BRANCH"; then
1417
if git show-ref --verify --quiet "refs/remotes/origin/$BRANCH"; then

0 commit comments

Comments
 (0)