Created
January 6, 2023 08:05
-
-
Save niski84/c1f5e733cfb90617e8573e577c1b8f6d to your computer and use it in GitHub Desktop.
jenkins login with crumb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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