woodpecker-ci / plugin-git

Woodpecker plugin for cloning Git repositories
https://woodpecker-ci.org/docs/usage/workflow-syntax#clone
Apache License 2.0
15 stars 25 forks source link

unable to checkout branch head #87

Closed fracai closed 10 months ago

fracai commented 10 months ago

With 2.1.0 I used a configuration as follows to checkout the head of a branch

  clone-site:
    image: woodpeckerci/plugin-git:2.1.0
    group: clone
    settings:
      remote: https://${GIT_HOST}/${CI_REPO_OWNER}/${CI_REPO_NAME}
      branch: site
      ref: refs/heads/site
      sha: FETCH_HEAD
      path: ../_tmp/site

With 2.1.1 and 2.1.2 I saw that this was failing to checkout, I found the recent PRs (#76 and #84), and tried some changes (dropping the ref and sha):

  clone-site:
    image: woodpeckerci/plugin-git:2.1.2
    group: clone
    settings:
      remote: https://${GIT_HOST}/${CI_REPO_OWNER}/${CI_REPO_NAME}
      branch: site
      path: ../_tmp/site

This looked promising, as the log indicated the correct branch, but the hash used was the most recent for the wrong branch. In the example below, fracai is the user, site the correct branch, repo the repository, and XXXXXXXX the incorrect hash.

+ git init -b site
Initialized empty Git repository in /woodpecker/src/********/fracai/_tmp/site/.git/
+ git config --global safe.directory /woodpecker/src/********/fracai/repo
+ git remote add origin https://********/fracai/repo
+ git fetch --no-tags --depth=1 --filter=tree:0 origin +XXXXXXXX:
From https://********/fracai/repo
 * branch            XXXXXXXX -> FETCH_HEAD
+ git reset --hard -q XXXXXXXX
+ git submodule update --init --recursive
+ git lfs fetch
fetch: Fetching reference refs/heads/site
+ git lfs checkout

So for now I'm pinned to 2.1.0. Am I "holding it wrong"? Is there a new way to correctly get the head of a branch?

qwerty287 commented 10 months ago

Can you check woodpecker's API or the env vars or the DB if the hash is correct there? Which forge do you use?

fracai commented 10 months ago

What do you mean by checking the API, env vars, or DB? What variables, API calls, DB queries are you interested in? I'm using Forego 1.19.4 and Woodpecker is 1.0-010da2e174.

The hash matches the current commit being built, but I want the head of a different branch. And while I could specify that hash of that other branch in the woodpecker step, that would extra steps of having to keep that hash up to date. Specifying "whatever hash is the head of that other branch" would be much simpler and works in 2.1.0.

Let me know what other info would help. Thanks.

fracai commented 10 months ago

I'll add that if I include a refs: refs/heads/site line, the checkout still checks out the wrong branch (main when I want site)

qwerty287 commented 10 months ago

Actually, you don't even need to check the api/db. In the pipeline ui, you can see the sha over the step list. Is this sha correct?

fracai commented 10 months ago

Oh, yes. Well, it's correct for what is being built. But, the step in question is meant to checkout a different branch to a different directory. And the same sha is being used for both clones.

The initial default clone is correct, but this clone-site step is meant to checkout the head commit of a different branch. So the step "succeeds" because the sha does exist in this repo, but it's checking out the wrong branch.

qwerty287 commented 10 months ago

Ahh I think I finally understood your problem now, sorry...

You need to set sha to an empty string (I.e. sha: '') to disable sha cloning but rather use the ref you specified. This behavior was changed indeed.

fracai commented 10 months ago

Here's another example. With the following step:

  clone-site:
    image: woodpeckerci/plugin-git:2.1.2
    group: clone
    settings:
      remote: https://${GIT_HOST}/${CI_REPO_OWNER}/recipes
      branch: site
      ref: refs/heads/site
      path: ../_tmp/site

I get the following output:

Initialized empty Git repository in /woodpecker/src/********/fracai/_tmp/site/.git/
+ git config --global safe.directory /woodpecker/src/********/fracai/repo
+ git remote add origin https://********/fracai/recipes
+ git fetch --no-tags --depth=1 --filter=tree:0 origin + XXXXXXXX:
fatal: remote error: upload-pack: not our ref XXXXXXXX
exit status 128

Note that in this case I'm trying to checkout the site branch from a different repo (recipes). So the plugin is still trying to checkout the current sha from that different repo, which then fails because that sha is of course not available.

Leaving off the refs: config line adds an initial command: git init -b site to the beginning of the step output, but the result is the same.

Basically, the plugin is always referencing the current repo, regardless of what is configured by the remote: setting.

fracai commented 10 months ago

Just saw your reply. Thanks. That sounded promising, but the following is still showing the pushed commit is checked out, not the site branch.

  clone-site:
    image: woodpeckerci/plugin-git:2.1.2
    group: clone
    settings:
      remote: https://${GIT_HOST}/${CI_REPO_OWNER}/${CI_REPO_NAME}
      branch: site
      sha: ''
      path: ../_tmp/site

  check-site:
    image: woodpeckerci/plugin-git:2.1.2
    commands:
      - cd /woodpecker/src/${GIT_HOST}/${CI_REPO_OWNER}/_tmp/site
      - git rev-parse HEAD
fracai commented 10 months ago

I can also achieve the checkout I'm looking for with the following, but listing the commands kinda defeats the purpose of using the plugin. I suppose I could switch to just using a git container, but I do like the plugin.

  clone-site:
    image: woodpeckerci/plugin-git:2.1.2
    commands:
      - mkdir -p /woodpecker/src/${GIT_HOST}/${CI_REPO_OWNER}/_tmp/site
      - cd /woodpecker/src/${GIT_HOST}/${CI_REPO_OWNER}/_tmp/site
      - git init -b site
      - git config --global safe.directory /woodpecker/src/${GIT_HOST}/${CI_REPO_OWNER}/${CI_REPO_NAME}
      - git remote add origin https://${GIT_HOST}/${CI_REPO_OWNER}/${CI_REPO_NAME}
      - "git fetch --no-tags --depth=1 --filter=tree:0 origin +refs/heads/site:"
      - git reset --hard -q FETCH_HEAD
      - git submodule update --init --recursive
      - git lfs fetch
      - git lfs checkout
      - git rev-parse HEAD

(That one line is in quotes to make sure the : isn't interpreted as yaml.)

fracai commented 10 months ago

Whew. Got it.

I was using refs: instead of ref:. My working configuration is:

  clone-site:
    image: woodpeckerci/plugin-git:2.1.2
    group: clone
    settings:
      remote: https://${GIT_HOST}/${CI_REPO_OWNER}/${CI_REPO_NAME}
      branch: site
      ref: refs/heads/site
      sha: ''
      path: ../_tmp/site

I can either close this or repurpose it for updating documentation. I note that remote, ref, sha, and path aren't documented on https://woodpecker-ci.org/plugins/Git%20Clone

Thanks for your help