-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
87 lines (71 loc) · 2.59 KB
/
main.go
File metadata and controls
87 lines (71 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"context"
"encoding/pem"
"fmt"
"log"
"os/exec"
"strconv"
"github.com/golang-jwt/jwt/v5"
"create-github-app-token/internal/github"
"create-github-app-token/internal/secret"
)
func main() {
// Get environment variables using the secret package
appIDStr, found := secret.GetEnvVar("APP_ID", "", true)
if !found {
log.Fatalf("Required environment variable APP_ID not set")
}
privateKeyContent, found := secret.GetEnvVar("PRIVATE_KEY", "", true)
if !found {
log.Fatalf("Required environment variable PRIVATE_KEY not set")
}
// For ENV_VAR_NAME, provide a default value of "GITHUB_TOKEN"
envVarName, _ := secret.GetEnvVar("ENV_VAR_NAME", "GITHUB_TOKEN", false)
// Convert app ID to int64
appIDInt, err := strconv.ParseInt(appIDStr, 10, 64)
if err != nil {
log.Fatalf("Invalid app ID: %v", err)
}
// Parse private key
block, _ := pem.Decode([]byte(privateKeyContent))
if block == nil {
log.Fatalf("Failed to parse PEM block containing the private key")
}
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(privateKeyContent))
if err != nil {
log.Fatalf("Failed to parse private key: %v", err)
}
// Create a context
ctx := context.Background()
// Create GitHub client with JWT authentication
client, err := github.NewClientWithJWT(appIDInt, privateKey)
if err != nil {
log.Fatalf("Failed to create GitHub client: %v", err)
}
// Create an installation token
installationToken, err := client.CreateInstallationToken(ctx)
if err != nil {
log.Fatalf("Failed to create installation token: %v", err)
}
// Set Buildkite environment variable
fmt.Printf("Setting Buildkite environment variable %s\n", envVarName)
err = setBuildkiteEnvVar(envVarName, installationToken)
if err != nil {
log.Fatalf("Failed to set Buildkite environment variable: %v", err)
}
fmt.Printf("Successfully set %s in Buildkite environment variable\n", envVarName)
}
// setBuildkiteEnvVar sets a Buildkite metadata value and also sets it as an environment variable
func setBuildkiteEnvVar(name, value string) error {
// Set as environment variable for the current job using buildkite-agent env set
// This ensures the value is available as an environment variable in the current job
fmt.Printf("Setting as environment variable for current job\n")
envCmd := exec.Command("buildkite-agent", "env", "set", fmt.Sprintf("%s=%s", name, value))
envOutput, err := envCmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to set environment variable: %v\nOutput: %s", err, string(envOutput))
}
fmt.Printf("Successfully set environment variable: %s\n", string(envOutput))
return nil
}