Skip to content

Commit d930a54

Browse files
authoredFeb 1, 2025··
Use gh auth token for default GITHUB_TOKEN secret (#43)
* initial version
1 parent 358722a commit d930a54

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed
 

‎cmd/root.go

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/actions-oss/act-cli/pkg/artifacts"
3030
"github.com/actions-oss/act-cli/pkg/common"
3131
"github.com/actions-oss/act-cli/pkg/container"
32+
"github.com/actions-oss/act-cli/pkg/gh"
3233
"github.com/actions-oss/act-cli/pkg/model"
3334
"github.com/actions-oss/act-cli/pkg/runner"
3435
)
@@ -411,6 +412,15 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
411412
log.Debugf("Loading secrets from %s", input.Secretfile())
412413
secrets := newSecrets(input.secrets)
413414
_ = readEnvs(input.Secretfile(), secrets)
415+
hasGitHubToken := false
416+
for k := range secrets {
417+
if strings.EqualFold(k, "GITHUB_TOKEN") {
418+
hasGitHubToken = true
419+
}
420+
}
421+
if !hasGitHubToken {
422+
secrets["GITHUB_TOKEN"], _ = gh.GetToken(ctx, "")
423+
}
414424

415425
log.Debugf("Loading vars from %s", input.Varfile())
416426
vars := newSecrets(input.vars)

‎pkg/gh/gh.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package gh
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"context"
7+
"os/exec"
8+
)
9+
10+
func GetToken(ctx context.Context, workingDirectory string) (string, error) {
11+
var token string
12+
13+
// Locate the 'gh' executable
14+
path, err := exec.LookPath("gh")
15+
if err != nil {
16+
return "", err
17+
}
18+
19+
// Command setup
20+
cmd := exec.CommandContext(ctx, path, "auth", "token")
21+
cmd.Dir = workingDirectory
22+
23+
// Capture the output
24+
var out bytes.Buffer
25+
cmd.Stdout = &out
26+
27+
// Run the command
28+
err = cmd.Run()
29+
if err != nil {
30+
return "", err
31+
}
32+
33+
// Read the first line of the output
34+
scanner := bufio.NewScanner(&out)
35+
if scanner.Scan() {
36+
token = scanner.Text()
37+
}
38+
39+
return token, nil
40+
}

‎pkg/gh/gh_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package gh
2+
3+
import (
4+
"context"
5+
"testing"
6+
)
7+
8+
func TestGetToken(t *testing.T) {
9+
token, _ := GetToken(context.TODO(), "")
10+
t.Log(token)
11+
}

0 commit comments

Comments
 (0)
Please sign in to comment.