#get current git commit id and git tags

1 messages · Page 1 of 1 (latest)

static spruce
#

Hello I am trying to use dagger typescript sdk. in one of my functions I need to have access to the current commit id and the tags (if any). How would you do that with dagger ? lets say that we actually have the whole repo available because I actually pass it to the dagger function as a parameter. (the git repo has already been fetched before I call dagger)

I know that I could of course run some git commands from within a container but that is "horrible". (in that case I could just get the info I want in a script and give it as parameters to the dagger functions...).
What are the other alternatives ? (could I use a javascript library for that ? if yes please tell me how I can use the dagger "Directory" as an input to a third party library).

Thank you.

bitter glade
static spruce
#

thank you @bitter glade ... anyone else from dagger to suggest how to do without using the "create a container and run a command from within" ?

maiden stone
#

The main difference is that dag.Git clones the specified url from a git remote and you can't use your host working directory yet

#

Otherwise you have two ways to achieve this:

  1. Call the git CLI in a container as mentioned a above.

  2. Use a git library in your typescript module like nodegit and get the data from there

sonic imp
#

@static spruce do you want to know the current git commit and tags of your own module?

sonic imp
# sonic imp <@920219503718187019> do you want to know the current git commit and tags of you...

For our own CI, we need something similar to compute a version string based on the current git context (for output of dagger version).

We encapsulated this logic in github.com/dagger/dagger/version. Here's how we access the git metadata, by opening the .git directory from our context directory: https://github.com/dagger/dagger/blob/main/version/main.go#L35

Note that this adds overhead in local dev, because of uploading the git objects into the engine. Besides that, it works great.

GitHub

An engine to run your pipelines in containers. Contribute to dagger/dagger development by creating an account on GitHub.

static spruce
#

@maiden stone thank you for your answer.
I currently have this:

  @func()
  async gitCurrentCommit(@argument({ defaultPath: "/.git" }) gitRoot: Directory): Promise<string> {
    const cleanContainer = dag.container()
      .from("alpine:latest")
      .withRun("apk update")
      .withRun("apk add --no-cache zsh curl git bash")
      .withDirectory("/mnt/.git", gitRoot)
      .withWorkdir("/mnt")
      .withRun("git config --global --add safe.directory /mnt")
      .withRun("git checkout -f");


    return cleanContainer.withExec(["git", "rev-parse", "HEAD"]).stdout();
  }

I do not like this solution at all.
Can you show me how I can use nodegit from withing a dagger function like this but without cloning the repo from within dagger: the git repo is given as an argument to the function.

In the following example from nodegit, how would I get the "tmp" folder ?

// Open the repository directory.
Git.Repository.open("tmp")
  // Open the master branch.
  .then(function(repo) {
    return repo.getMasterCommit();
  })
  // Display information about commits on master.
  .then(function(firstCommitOnMaster) {
    // Create a new history event emitter.
    var history = firstCommitOnMaster.history();

    // Create a counter to only show up to 9 entries.
    var count = 0;

    // Listen for commit events from the history.
    history.on("commit", function(commit) {
      // Disregard commits past 9.
      if (++count >= 9) {
        return;
      }

      // Show the commit sha.
      console.log("commit " + commit.sha());

      // Store the author object.
      var author = commit.author();

      // Display author information.
      console.log("Author:\t" + author.name() + " <" + author.email() + ">");

      // Show the commit date.
      console.log("Date:\t" + commit.date());

      // Give some space and show the message.
      console.log("\n    " + commit.message());
    });

    // Start emitting events.
    history.start();
  });