Skip to content

Instantly share code, notes, and snippets.

@niski84
Created January 6, 2023 08:05
Show Gist options
  • Save niski84/c1f5e733cfb90617e8573e577c1b8f6d to your computer and use it in GitHub Desktop.
Save niski84/c1f5e733cfb90617e8573e577c1b8f6d to your computer and use it in GitHub Desktop.
jenkins login with crumb
package main
import (
"fmt"
"github.com/bndr/gojenkins"
)
func main() {
// Create a Jenkins client
client, err := gojenkins.CreateJenkins(nil, "http://jenkins.example.com")
if err != nil {
panic(err)
}
// Get the CSRF protection crumb
crumb, err := client.GetCrumb()
if err != nil {
panic(err)
}
// Authenticate to Jenkins
username := "your-username"
password := "your-password"
err = client.Init()
if err != nil {
panic(err)
}
err = client.Login(username, password)
if err != nil {
panic(err)
}
// Get the session ID
session, err := client.GetCurrentUserSession()
if err != nil {
panic(err)
}
fmt.Println("Session ID:", session.SessionID)
// Set the CSRF protection crumb
client.Crumb = crumb
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment