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.
#Can we get the current modules version inside the module?
1 messages · Page 1 of 1 (latest)
cc @earnest harbor
i think currentModule.git() maybe?
you want contextual git
you can add a defaultPath annotation to a dagger.GitRepository argument
this API doesn't exist - we never added it because it degrades caching
ah right
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)
}
I think with GitRepository you don't need to set an ignore filter (and probably can't?)
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)
In the future, it would be cool if we added GitRepository.versionString() as a convenience 🙂 Maybe with optional formatting, so you can customize to say eg. -dirty or -<diffhash> or something. We have that logic already implemented in a module for our own version string, but it's very generalizable