Skip to content

Instantly share code, notes, and snippets.

@Java4all
Created July 28, 2025 18:17
Show Gist options
  • Select an option

  • Save Java4all/26ff36e2d5e4e3fffccc6d84fda71017 to your computer and use it in GitHub Desktop.

Select an option

Save Java4all/26ff36e2d5e4e3fffccc6d84fda71017 to your computer and use it in GitHub Desktop.
groovy call
jenkins_create_token() {
local user=$1 pass=$2 name=$3 jurl=$4
# Get CSRF crumb
local crumb
crumb=$(curl -s -u "$user:$pass" \
"$jurl/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)")
# Create token
curl -s -u "$user:$pass" \
-H "$crumb" \
-X POST \
"$jurl/user/$(printf %q "$user")/descriptorByName/jenkins.security.ApiTokenProperty/generateNewToken" \
--data-urlencode "newTokenName=$name"
}
# Example usage:
jenkins_create_token "[email protected]" "P@ssw0rd!" "ci-bot" "https://jenkins.example.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment