Skip to content

Instantly share code, notes, and snippets.

@rolandjohann
Last active September 4, 2020 12:19
Show Gist options
  • Save rolandjohann/7b789fa87d76b92972d92cd5bbc9dea2 to your computer and use it in GitHub Desktop.
Save rolandjohann/7b789fa87d76b92972d92cd5bbc9dea2 to your computer and use it in GitHub Desktop.
Jenkins HTTP API
#!/bin/bash
set -eu -o pipefail
function create_jenkins_azure_service_principal_credentials() {
local jenkins_url=""
local jenkins_username=""
local jenkins_token=""
local credential_id=""
local subscription_id=""
local client_id=""
local client_secret=""
local tenant=""
local description=""
local credential_request_body
credential_request_body="$(
cat <<EOF
{
"": "0",
"credentials": {
"scope": "GLOBAL",
"id": "${credential_id}",
"subscriptionId": "${subscription_id}",
"clientId": "${client_id}",
"clientSecret": "${client_secret}",
"tenant": "${tenant}",
"description": "${description}",
"\$class": "com.microsoft.azure.util.AzureCredentials"
}
}
EOF
)"
curl -s -X POST \
"${jenkins_url}/credentials/store/system/domain/_/createCredentials" \
--user "${jenkins_username}:${jenkins_token}" \
--data-urlencode "json=${credential_request_body}"
}
function create_jenkins_username_password_credentials() {
local jenkins_url=""
local jenkins_username=""
local jenkins_token=""
local credential_id=""
local username=""
local password=""
local description=""
local credential_request_body
credential_request_body="$(
cat <<EOF
{
"": "0",
"credentials": {
"scope": "GLOBAL",
"id": "{$credential_id}",
"username": "{$username}",
"password": "${password}",
"description": "${description}",
"\$class": "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"
}
}
EOF
)"
curl -s -X POST \
"${jenkins_url}/credentials/store/system/domain/_/createCredentials" \
--user "${jenkins_username}:${jenkins_token}" \
--data-urlencode "json=${credential_request_body}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment