In a JavaScript action, how do I execute a Git command and save the output to a variable?

Hello. I have created a JavaScript GitHub action, and inside, want to execute a simple Git command and save it to a JavaScript variable. Using the exec toolkit package, I can execute the Git command. That prints the correct output to the console (06aee1b). But when I try to save the output to a variable, the value of the variable is 0. How do I save the correct output to a variable? My code and output are below:

I have intermediate JavaScript and Git experience. But this is my first experience with GitHub actions.

My Code (in my JavaScript action)

import * as core from "@actions/core";
import * as github from "@actions/github";
import * as exec from "@actions/exec";

const execOutput = await exec.exec("git rev-parse --short HEAD");
console.log("execOutput is: ");


execOutput is: 

Check this part of the docs:

const exec = require('@actions/exec');

let myOutput = '';
let myError = '';

const options = {
  listeners: {
    stdout: (data: Buffer) => {
      myOutput += data.toString();
    stderr: (data: Buffer) => {
      myError += data.toString();

await exec.exec('git', ['rev-parse', '--short', 'HEAD'], options);
1 Like

OK thanks so much–that’s exactly what I needed. Didn’t read the docs carefully enough :-/