#How to get commit id when cloning

1 messages · Page 1 of 1 (latest)

rose dirge
#

Hello
Is there a way to get the commit id when cloning with the dagger client ?
I want to use the commit id as the tag for my docker image

rose dirge
#

How to get commit id when cloning

unkempt halo
# rose dirge Hello Is there a way to get the commit id when cloning with the dagger client ? ...

Hi @rose dirge, currently the cloned working copy does not include the .git directory, preventing you to get the commit id of the cloned ref. There is an issue tracking this: https://github.com/dagger/dagger/issues/3657

There is a workaround though, you can run a git clone from a container directly and read the commit id from the container. If you tell me the SDK language you're using, I can give you a working example.

GitHub

What are you trying to do? I am implementing a conventional commit task and I require the .git directory to be present in my cloned repo so that I can generat a new semantic release, Why is this im...

smoky quartz
#

@unkempt halo I see that the Go SDK v0.3.4 has the keepGitDir option in the git step? IIUC that should hepl here, correct?

unkempt halo
vale fog
#

This was already implemented, but the Go SDK hasn't been released yet

#

It's available in the Python SDK

vale fog
unkempt halo
#

@rose dirge let us know the language you use when you’re back. We can provide a snippet.

vale fog
smoky quartz
#

So users can play with this until we craft a release

vale fog
#

@smoky quartz Yep that's true!

vale fog
# vale fog <@336241811179962368> Yep that's true!

There's the chicken and egg problem though -- @main will use the LAST released engine, so API endpoints that haven't been released won't work

/cc @mighty shuttle Actually, would it make sense for the code in main to always use the :main tag so at least go get @main or yarn add . works? But then we'd have to change that on the fly while publishing, which is not ideal

mighty shuttle
# vale fog There's the chicken and egg problem though -- `@main` will use the LAST released...

The SDKs would have to be updated to resolve a tag to a SHA then I think since main is fully mutable, which is a significant amount of new code in each sdk. Or other approaches that have similar sort of tradeoffs.

Not 100% sure yet but I feel like this might be easier when we switch the autoprovision implementation of the SDKs to download the binary from somewhere over normal https rather than get it out of the engine image. I'm guessing there could just be a response header that has the checksum or ID of the binary so we can just easily use that as the "pinned sha"? Maybe?

rose dirge
#

Hi @unkempt halo
I am using the golang SDK.
But I found a way: since the github repo has many files, I am using the git package in golang to get just the refs/head of the branch

#

This is the function I am currently using instead of cloning.

smoky quartz
unkempt halo
# rose dirge This is the function I am currently using instead of cloning.

I don't see any dagger code here, I would assume you used the git go library to clone the code so this does not happen in a container. Here is a working snippet that does the same thing with dagger, containerized and cached:

func main() {
    ctx := context.Background()
    client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
    if err != nil {
        panic(err)
    }

    // clone git repo into a dagger dir
    sourceCode := client.Git("https://github.com/dagger/dagger", dagger.GitOpts{KeepGitDir: true}).Branch("main").Tree()
    // configure a container to be used as our base image (alpine + git)
    container := client.Container().From("alpine:3.16").Exec(
        dagger.ContainerExecOpts{
            Args: []string{"apk", "add", "--no-cache", "git"},
        },
    )
    // set the workdir and mounted directory for the container
    container = container.WithMountedDirectory("/code", sourceCode).WithWorkdir("/code")
    // fetch the commit id using git
    commit, err := container.Exec(dagger.ContainerExecOpts{
        Args: []string{"sh", "-c", "git log --pretty=tformat:'%H' -n1"},
    }).Stdout().Contents(ctx)
    // check error
    if err != nil {
        panic(err)
    }
    fmt.Println("Commit id:", commit)
}

Note that I used dagger.io/dagger@main given that KeepDir is not available yet in the last release (0.4.0).