Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems running it inside Github Workflow #20

Open
dgg opened this issue Feb 28, 2024 · 1 comment
Open

Problems running it inside Github Workflow #20

dgg opened this issue Feb 28, 2024 · 1 comment

Comments

@dgg
Copy link

dgg commented Feb 28, 2024

We found this package that works for validating branch names at the developer workstation via a git hook.

However we also want to enforce the branch name inside a Github Workflow running with the pull_request event.
In that context, the library is not able to successfully get the name of the branch (I think to have found that uses this git command: git rev-parse --abbrev-ref HEAD), getting something like HEAD instead of the name of the branch being merged.

That name comes in the workflow variable github.head_ref but I have not found a way to inject that name into the library.

Can you think of a way that the name of the branch name can be successfully detected in different scenarios/injected via the command line?

Thanks in advance

@dgg
Copy link
Author

dgg commented Feb 28, 2024

adding more information:

excerpt from the workflow
name: validate
on: [pull_request]
jobs:
  chores:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: actions/setup-node@v4
        with:
          node-version-file: ./.nvmrc
          cache: 'npm'
          cache-dependency-path: '**/.github/workflows/*.yml'
      - name: validate branch name
        run: |
          git rev-parse --abbrev-ref HEAD
          npx --yes --package @b12k/branch-name-lint branch-name-lint
excerpt from the output
2024-02-28T10:09:07.7765461Z ##[group]Run actions/checkout@v4
2024-02-28T10:09:07.7766105Z with:
2024-02-28T10:09:07.7766543Z   fetch-depth: 0
2024-02-28T10:09:07.7767019Z   repository: ***/ds-zpl
2024-02-28T10:09:07.7768085Z   token: ***
2024-02-28T10:09:07.7768503Z   ssh-strict: true
2024-02-28T10:09:07.7769011Z   persist-credentials: true
2024-02-28T10:09:07.7769579Z   clean: true
2024-02-28T10:09:07.7770141Z   sparse-checkout-cone-mode: true
2024-02-28T10:09:07.7770744Z   fetch-tags: false
2024-02-28T10:09:07.7771197Z   show-progress: true
2024-02-28T10:09:07.7771635Z   lfs: false
2024-02-28T10:09:07.7772035Z   submodules: false
2024-02-28T10:09:07.7772480Z   set-safe-directory: true
2024-02-28T10:09:07.7772983Z ##[endgroup]
...
2024-02-28T10:09:08.2691937Z Temporarily overriding HOME='/home/runner/work/_temp/7a47a62d-8042-45fc-8ce5-20e448020d9d' before making global git config changes
2024-02-28T10:09:08.2693929Z Adding repository directory to the temporary git global config as a safe directory
2024-02-28T10:09:08.2695578Z [command]/usr/bin/git config --global --add safe.directory /home/runner/work/ds-zpl/ds-zpl
2024-02-28T10:09:08.2698078Z Deleting the contents of '/home/runner/work/ds-zpl/ds-zpl'
2024-02-28T10:09:08.2699335Z ##[group]Initializing the repository
2024-02-28T10:09:08.2700244Z [command]/usr/bin/git init /home/runner/work/ds-zpl/ds-zpl
2024-02-28T10:09:08.2701554Z hint: Using 'master' as the name for the initial branch. This default branch name
2024-02-28T10:09:08.2702942Z hint: is subject to change. To configure the initial branch name to use in all
2024-02-28T10:09:08.2704279Z hint: of your new repositories, which will suppress this warning, call:
2024-02-28T10:09:08.2705689Z hint: 
2024-02-28T10:09:08.2706359Z hint: 	git config --global init.defaultBranch <name>
2024-02-28T10:09:08.2707079Z hint: 
2024-02-28T10:09:08.2707845Z hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
2024-02-28T10:09:08.2709223Z hint: 'development'. The just-created branch can be renamed via this command:
2024-02-28T10:09:08.2710215Z hint: 
2024-02-28T10:09:08.2710639Z hint: 	git branch -m <name>
2024-02-28T10:09:08.2711602Z Initialized empty Git repository in /home/runner/work/ds-zpl/ds-zpl/.git/
2024-02-28T10:09:08.2713834Z [command]/usr/bin/git remote add origin https://github.com/***/ds-zpl
2024-02-28T10:09:08.2715803Z ##[endgroup]
...
...
2024-02-28T10:09:08.3291203Z ##[group]Fetching the repository
2024-02-28T10:09:08.3294393Z [command]/usr/bin/git -c protocol.version=2 fetch --prune --no-recurse-submodules origin +refs/heads/*:refs/remotes/origin/* +refs/tags/*:refs/tags/* +ca85af414e18e9b57875ae8c3540b274e192204f:refs/remotes/pull/4/merge
2024-02-28T10:09:08.7401731Z From https://github.com/***/ds-zpl
2024-02-28T10:09:08.7408333Z  * [new branch]      feature/2-ignore-branch-linting          -> origin/feature/2-ignore-branch-linting
2024-02-28T10:09:08.7410077Z  * [new branch]      main                                     -> origin/main
2024-02-28T10:09:08.7411395Z  * [new ref]         ca85af414e18e9b57875ae8c3540b274e192204f -> pull/4/merge
2024-02-28T10:09:08.7425399Z ##[endgroup]
2024-02-28T10:09:08.7426242Z ##[group]Determining the checkout info
2024-02-28T10:09:08.7428109Z ##[endgroup]
2024-02-28T10:09:08.7428892Z ##[group]Checking out the ref
2024-02-28T10:09:08.7432565Z [command]/usr/bin/git checkout --progress --force refs/remotes/pull/4/merge
2024-02-28T10:09:08.7481194Z Note: switching to 'refs/remotes/pull/4/merge'.
2024-02-28T10:09:08.7481933Z 
2024-02-28T10:09:08.7482768Z You are in 'detached HEAD' state. You can look around, make experimental
2024-02-28T10:09:08.7484069Z changes and commit them, and you can discard any commits you make in this
2024-02-28T10:09:08.7485283Z state without impacting any branches by switching back to a branch.
2024-02-28T10:09:08.7486297Z 
2024-02-28T10:09:08.7486969Z If you want to create a new branch to retain commits you create, you may
2024-02-28T10:09:08.7488243Z do so (now or later) by using -c with the switch command. Example:
2024-02-28T10:09:08.7488935Z 
2024-02-28T10:09:08.7489449Z   git switch -c <new-branch-name>
2024-02-28T10:09:08.7490132Z 
2024-02-28T10:09:08.7490832Z Or undo this operation with:
2024-02-28T10:09:08.7491403Z 
2024-02-28T10:09:08.7491948Z   git switch -
2024-02-28T10:09:08.7492417Z 
2024-02-28T10:09:08.7493387Z Turn off this advice by setting config variable advice.detachedHead to false
2024-02-28T10:09:08.7494431Z 
2024-02-28T10:09:08.7495765Z HEAD is now at ca85af4 Merge 47fcc41c5be8affe27e7e1e03a3830c8e7033905 into 2dc501910b9041da30de0fe43cf27ba1cae57f87
2024-02-28T10:09:08.7546942Z ##[endgroup]
2024-02-28T10:09:08.7548244Z [command]/usr/bin/git log -1 --format='%H'
2024-02-28T10:09:08.7575276Z 'ca85af414e18e9b57875ae8c3540b274e192204f'
...
...
2024-02-28T10:09:10.3554714Z ##[group]Run git rev-parse --abbrev-ref HEAD
2024-02-28T10:09:10.3555466Z �[36;1mgit rev-parse --abbrev-ref HEAD�[0m
2024-02-28T10:09:10.3556277Z �[36;1mnpx --yes --package @b12k/branch-name-lint branch-name-lint�[0m
2024-02-28T10:09:10.3602257Z shell: /usr/bin/bash -e {0}
2024-02-28T10:09:10.3602709Z ##[endgroup]
2024-02-28T10:09:10.3784476Z HEAD
2024-02-28T10:09:11.9459405Z 
2024-02-28T10:09:11.9460533Z [BranchNameLint] Branch name doesnt match the pattern
2024-02-28T10:09:11.9549391Z 
2024-02-28T10:09:11.9549730Z Branch name
2024-02-28T10:09:11.9550137Z   pattern: :type/:name
2024-02-28T10:09:11.9550577Z Name params
2024-02-28T10:09:11.9551006Z   type: feature, bugfix, hotfix
2024-02-28T10:09:11.9551756Z   name: \d+-[a-z0-9-]+
2024-02-28T10:09:11.9552021Z 
2024-02-28T10:09:11.9552030Z 
2024-02-28T10:09:11.9632226Z ##[error]Process completed with exit code 1.

From the log with timestamp 2024-02-28T10:09:10.3555466Z you can see that git rev-parse --abbrev-ref HEAD is executed and from the log line with timestamp 2024-02-28T10:09:10.3784476Z the text HEAD is displayed, as opposed to what github.head_ref would contain: 2-ignore-branch-linting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant