Skip to content

Commit 9cb142a

Browse files
authored
Fix "Branch 'main' does not exist" error (#24)
* add dependabot file * validate the action yml file too * add GITHUB_BASE_REF * 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. * update trigger events to include push for plugin version validation
1 parent f6658b0 commit 9cb142a

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: Validate Plugin Version Test
2-
on: [pull_request]
2+
on: [push, pull_request]
33
jobs:
44
test:
55
runs-on: ubuntu-latest

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ inputs:
2424
branch:
2525
description: The branch to use as the base for PRs and commit the changes back to.
2626
required: false
27-
default: 'main'
2827
pr-status:
2928
description: The status of the PR to create. Default is 'draft'. Accepts 'draft' or 'open'.
3029
required: false
@@ -50,5 +49,6 @@ runs:
5049
FILENAMES: ${{ inputs.filenames }}
5150
BRANCH: ${{ inputs.branch }}
5251
PR_STATUS: ${{ inputs.pr-status }}
52+
GITHUB_BASE_REF: ${{ github.base_ref }}
5353
run: bash ${{ github.action_path }}/bin/validate-plugin-version.sh
5454

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)