actions / checkout

Action for checking out a repo
https://github.com/features/actions
MIT License
5.94k stars 1.76k forks source link

How to checkout current PR branch? #1108

Open wolf99 opened 1 year ago

wolf99 commented 1 year ago

I am trying to create a PR workflow that checkouts out the PR branch, lints the markdown, and then makes a commit with the changes (on PR: https://github.com/google/osv-scanner/pull/144).

This is running on the PR branch that is introducing this workflow, which is coming from my fork of this repo.

I am not understanding how to properly checkout the PR branch.

Looking at #20, and the docs for the Prettier action I am using for linting, it seems I should just

- uses: actions/checkout
  with:
    ref: ${{ github.ref }}

But using this causes checkout to fail, see job: https://github.com/google/osv-scanner/actions/runs/3914565858/jobs/6691777605

Fetching the repository
  /usr/bin/git -c protocol.version=2 fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin +refs/heads/prettier-lint-md-workflow*:refs/remotes/origin/prettier-lint-md-workflow* +refs/tags/prettier-lint-md-workflow*:refs/tags/prettier-lint-md-workflow*
  The process '/usr/bin/git' failed with exit code 1
  Waiting 13 seconds before trying again
  /usr/bin/git -c protocol.version=2 fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin +refs/heads/prettier-lint-md-workflow*:refs/remotes/origin/prettier-lint-md-workflow* +refs/tags/prettier-lint-md-workflow*:refs/tags/prettier-lint-md-workflow*
  The process '/usr/bin/git' failed with exit code 1
  Waiting 15 seconds before trying again
  /usr/bin/git -c protocol.version=2 fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin +refs/heads/prettier-lint-md-workflow*:refs/remotes/origin/prettier-lint-md-workflow* +refs/tags/prettier-lint-md-workflow*:refs/tags/prettier-lint-md-workflow*
  Error: The process '/usr/bin/git' failed with exit code 1

My git-fu 🥷 isn't strong enough to figure out what is wrong here

wolf99 commented 1 year ago

Added fetch-depth: 0 so in total:

- uses: actions/checkout
  with:
    ref: ${{ github.ref }}
    fetch-depth: 0

Now the checkout gets slightly further but results in:

Determining the checkout info
  /usr/bin/git branch --list --remote origin/prettier-lint-md-workflow
  /usr/bin/git tag --list prettier-lint-md-workflow
  Error: A branch or tag with the name 'prettier-lint-md-workflow' could not be found

https://github.com/google/osv-scanner/actions/runs/3914653689/jobs/6691959309

wolf99 commented 1 year ago

Tried ref: ${{ github.event.pull_request.head.sha }} instead, but then the Prettier action is back to:

fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
state now, use

    git push origin HEAD:<name-of-remote-branch>

😟

0ptim commented 1 year ago

I think you need to use this: ref: ${{ github.head_ref }}.


Just found your issue because I have the following error when someone makes a PR from a cloned repo.

A branch or tag with the name 'some_branch_name' could not be found

I'm actually also using Prettier. I have this config.

name: Pull Request Check

on:
  pull_request:
    branches:
      - main

jobs:
  prettier:
    name: Format files using Prettier
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          ref: ${{ github.head_ref }}
          fetch-depth: 0

      - name: Prettify code
        uses: creyD/prettier_action@v4.2
        with:
          dry: True
          only_changed: True
          prettier_options: --check **/*.{js,css,md} --end-of-line lf

Maybe our problem is related. This is the run which fails: https://github.com/0ptim/DeFiChainWiki/actions/runs/4108092834/jobs/7088370243

0ptim commented 1 year ago

Just got it working! 🎉 Have a look here: https://github.com/actions/checkout/issues/455