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

Upgrading @v1.2 -> @v2 can no longer specify short sha in ref #265

Open
frode-carlsen opened this issue May 30, 2020 · 16 comments
Open

Upgrading @v1.2 -> @v2 can no longer specify short sha in ref #265

frode-carlsen opened this issue May 30, 2020 · 16 comments
Labels
enhancement New feature or request

Comments

@frode-carlsen
Copy link

frode-carlsen commented May 30, 2020

when upgrading to @v2 or later it no longer accepts a single short sha for the ref parameter, and the git command fails:

Setup

uses: actions/checkout@v2.2.0
   with:
     ref: 7daa143

Output:

/usr/bin/git -c protocol.version=2 fetch --no-tags --prune --progress --no-recurse-submodules --depth=5 origin +refs/heads/7daa143*:refs/remotes/origin/7daa143* +refs/tags/7daa143*:refs/tags/7daa143*
  The process '/usr/bin/git' failed with exit code 1
  Waiting 18 seconds before trying again
@ericsciple
Copy link
Contributor

For improved perf, v2 only fetches a single commit by default. If you set fetch-depth: 0 then will fetch all history for all branches and tags and the short sha will probably work. Otherwise need to specify the full SHA.

@frode-carlsen
Copy link
Author

tnx,
fetch-depth: 0 doesn't make any difference, just got a different error.
but full sha works. Seems the checkout command it generates assumes it's a branch or tag when it's a short hash (7 chars) and thereafter generates an invalid checkout command

With a full sha it generrates this checkout command - same as it should be if a short sha had been treated the same
/usr/bin/git -c protocol.version=2 fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin +7daa1431d70a06da41e2da7f2ade272bee3d56ca:refs/remotes/pull/515/merge

@ericsciple ericsciple changed the title Upgrading @v1.2 -> @v2 can no longer specify single commit in ref Upgrading @v1.2 -> @v2 can no longer specify short sha in ref Jun 1, 2020
@ericsciple
Copy link
Contributor

Thanks for confirmation.

Fyi - I updated the title to clarify "short sha" instead of "single ref"

@ericsciple ericsciple added the enhancement New feature or request label Jun 1, 2020
@ericsciple
Copy link
Contributor

Also I marked as enhancement.

Adding some thoughts regarding implementation: I wonder if the git fetch allows a short sha, or whether would need to query REST API to resolve.

Also would need to be implemented for the case when git 2.18 not in the PATH, and fallback to download the tarball from the REST API.

@jsumners
Copy link

Adding some more info here.

I have a set of steps like:

- name: Lookup Branch
        uses: actions/github-script@v3
        id: pr-branch
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const response = await github.pulls.get({
              owner: context.repo.owner,
              repo: context.repo.repo,
              pull_number: context.issue.number
            })
            return response.data.head.ref

- name: Checkout
        uses: actions/checkout@v2
        with:
          ref: ${{ steps.pr-branch.outputs.result }}
          persist-credentials: false

That resu

This ends up trying to issue the command:

/usr/bin/git -c protocol.version=2 fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin +refs/heads/"test-6"*:refs/remotes/origin/"test-6"* +refs/tags/"test-6"*:refs/tags/"test-6"*

Notice that the short ref is being quoted in the command. I don't know if that matters or not, but it's definitely failing. Switching back to @v1 works.

@stleamist
Copy link

Notice that the short ref is being quoted in the command. I don't know if that matters or not, but it's definitely failing. Switching back to @v1 works.

@jsumners fromJSON will unquote the string.

ref: ${{ fromJSON(steps.pr-branch.outputs.result) }}

albrektsson added a commit to navikt/k9-sak-web that referenced this issue Nov 11, 2021
Ikke støtte før short sha ref i v2 per actions/checkout#265
vebnor pushed a commit to navikt/k9-sak-web that referenced this issue Nov 11, 2021
Ikke støtte før short sha ref i v2 per actions/checkout#265
johnboyes added a commit to agilepathway/available-pets-consumer that referenced this issue Dec 4, 2021
GitHub's [Checkout action][1] needs the [full SHA in order to checkout a
commit][2].

To get the full SHA we simply use a [bash array][3] which outputs its
first element.

[1]: https://github.com/actions/checkout
[2]: actions/checkout#265
[3]: https://opensource.com/article/18/5/you-dont-know-bash-intro-bash-arrays
johnboyes added a commit to agilepathway/available-pets-consumer that referenced this issue Dec 4, 2021
GitHub's [Checkout action][1] needs the [full SHA in order to checkout a
commit][2].

To get the full SHA we simply use a [bash array][3] which outputs its
first element.

[1]: https://github.com/actions/checkout
[2]: actions/checkout#265
[3]: https://opensource.com/article/18/5/you-dont-know-bash-intro-bash-arrays
@bpleines
Copy link

Unsurprisingly this also affects checkout@v3. Reverting back to v1 satisfied my use case as many others have suggested

@yasarutkuu
Copy link

yasarutkuu commented Sep 21, 2022

same problem, checkout@v2 doesnt support short sha

@weineran
Copy link

For now, as a workaround, I am using

fullSHA=$(git rev-parse ${shortSHA})

in order to get the full SHA from the short SHA, then passing the full SHA to the checkout action.

However, it would be really nice if we could pass in the short SHA directly like we could in v1.

@prein
Copy link

prein commented Jul 13, 2023

For now, as a workaround, I am using

fullSHA=$(git rev-parse ${shortSHA})

in order to get the full SHA from the short SHA, then passing the full SHA to the checkout action.

Isn't is a chicken and egg problem? How can you run git rev-parse before checkout?

@socketbox
Copy link

What I'm failing to grasp here is the notion that this is somehow an enhancement, when it seems like a regression: this was a part of functionality that was working and is now not working. Unless there was a deliberate design decision to remove short hashes from acceptable inputs (and I certainly haven't done enough digging to ascertain as much), I don't think this issue can accurately be characterized as a feature request.

@jlongo-encora
Copy link

Any updates here? This would be a good feature to return.

@gentoorax
Copy link

Bump +1 - please return this feature, I've had to move back to v1. Totally agree with @socketbox.

@thockin
Copy link

thockin commented Nov 11, 2023

I stumbled on this because I also have a tool that would like to use short SHA's, but like this one it uses git fetch which does not resolve short SHA's. If there is a way to ask a remote "please expand this short SHA", I don't know what it is. That means that both checkout and my own tool both require full SHAs.

If there is a way to do this, please tell me (us).

@ryan-williams
Copy link

I just ran into this, and worked around it by using gh to resolve a short SHA to a full SHA (without checking out the repository), then passing the full SHA to actions/checkout:

on:
  workflow_dispatch:
    inputs:
      ref:
        type: string
        default: main
        description: "The branch, tag, or SHA to run the workflow from"
jobs:
  run:
    runs-on: ubuntu-latest
    steps:
      - name: Resolve inputs.ref to full SHA
        id: resolve-ref
        # https://github.com/actions/checkout/issues/265
        env:
          GH_TOKEN: ${{ secrets.GH_TOKEN }}
        run: |
          ref=${{ inputs.ref }}
          sha=$(gh api /repos/$GITHUB_REPOSITORY/commits/$ref | jq -r .sha)
          if [ -z "$sha" ]; then
            echo "Failed to resolve ref $ref (possibly missing GH_TOKEN env var?)" >&2
            exit 1
          fi
          echo "Expanded ref $ref to SHA $sha"
          echo "sha=$sha" >> $GITHUB_OUTPUT
      - uses: actions/checkout@v4
        with:
          ref: ${{ steps.resolve-ref.outputs.sha }}

(source)

Note that:

  • Calling gh requires a GH_TOKEN secret (docs).
  • The above will resolve all refs (including branch names) to a SHA. If something later in the job requires a specific branch to be checked out, that might break.

In my case, another workflow was calling this one, and passing the short SHA; I also needed secrets: inherit at the call-site:

rebuild_www:
  name: Rebuild www
  needs: refresh_data
  if: needs.refresh_data.outputs.build_www
  uses: ./.github/workflows/www.yml
  secrets: inherit
  with:
    ref: ${{ needs.refresh_data.outputs.sha }}

(source)

@abower-digimarc
Copy link

I also need to be able to checkout via short sha.

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

No branches or pull requests