#Can we get the current modules version inside the module?

1 messages · Page 1 of 1 (latest)

lilac igloo
#

I want to add the current module's Git revision as an annotation to a container it creates. For example, I have a Dagger module that builds a container, which I call using dagger call -m my-builder@deadbeef container, and the returned container should have an annotation vnd.my-company.my-builder.revision=deadbeef. Is it possible to get the current module's revision (assuming it's called from a Git reference rather than a directory) without passing the revision as an extra argument? For example, something like dag.CurrentModule().Source().AsGit().Head().Commit(ctx) doesn't work because CurrentModule.Source() returns only the module's source directory (e.g., .dagger), not the full context.

spice shore
earnest harbor
#

you want contextual git

#

you can add a defaultPath annotation to a dagger.GitRepository argument

earnest harbor
spice shore
#

ah right

lilac igloo
# earnest harbor you can add a defaultPath annotation to a dagger.GitRepository argument

Thanks for the hints. This does the trick (might add a +ignore filter to ignore local changes):

type MyModule struct {
    // +private
    ModuleGitRepository *dagger.GitRepository
}

func New(
    // +optional
    // +defaultPath="."
    moduleGitRepository *dagger.GitRepository,
) *MyModule {
    return &MyModule{
        ModuleGitRepository: moduleGitRepository,
    }
}

func (m *MyModule) MyFunc(ctx context.Context) (string, error) {
    return m.ModuleGitRepository.Head().Commit(ctx)
}
spice shore
#

I think with GitRepository you don't need to set an ignore filter (and probably can't?)

earnest harbor
#

you can't yup

#

as of the .4 though, it should automatically handle gitignore

lilac igloo
#

Right, just learned this as well. But with the just released GitRepository.Uncommited function, we might be able to add a -dirty to the rev, which is even more helpful (and I guess this function needs the workdir anyway, so should be fine)

spice shore