Last active
June 11, 2022 10:50
-
-
Save gagomes/d2c3ba10a44006f82ec6242c8b0c43f4 to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
set -ex | |
source credentials.sh | |
export JENKINS_URL=http://127.0.0.1:8080 | |
export NODE_NAME=foobar3 | |
export JENKINS_CRUMB=$(curl --cookie-jar jenkins-cookies -s -X GET $JENKINS_URL/crumbIssuer/api/json --user ${JENKINS_USERNAME}:${JENKINS_PASSWORD} | jq -r '.crumb') | |
export JENKINS_TOKEN=$(curl --cookie-jar jenkins-cookies -s -X POST "${JENKINS_URL}/user/${JENKINS_USERNAME}/descriptorByName/jenkins.security.ApiTokenProperty/generateNewToken" --data 'newTokenName=kb-token' --user ${JENKINS_USERNAME}:${JENKINS_PASSWORD} -H "Jenkins-Crumb:${JENKINS_CRUMB}") | |
JSON_OBJECT=$(cat << EOM | |
{ | |
"Jenkins-Crumb": "${JENKINS_CRUMB}", | |
"": [ | |
"hudson.plugins.sshslaves.SSHLauncher", | |
"hudson.slaves.RetentionStrategy\$Always" | |
], | |
"launcher": { | |
"": "2", | |
"\$class": "hudson.plugins.sshslaves.SSHLauncher", | |
"credentialsId": "d436fff1-af1c-45df-8cb6-3907d119b8fa", | |
"host": "host", | |
"javaPath": "", | |
"jvmOptions": "", | |
"launchTimeoutSeconds": "", | |
"maxNumRetries": "", | |
"port": "22", | |
"prefixStartSlaveCmd": "", | |
"suffixStartSlaveCmd": "", | |
"retryWaitTime": "", | |
"sshHostKeyVerificationStrategy": { | |
"\$class": "hudson.plugins.sshslaves.verifiers.ManuallyTrustedKeyVerificationStrategy", | |
"requireInitialManualTrust": true, | |
"stapler-class": "hudson.plugins.sshslaves.verifiers.ManuallyTrustedKeyVerificationStrategy" | |
}, | |
"stapler-class": "hudson.plugins.sshslaves.SSHLauncher" | |
}, | |
"retentionStrategy": { | |
"\$class": "hudson.slaves.RetentionStrategy\$Always", | |
"stapler-class": "hudson.slaves.RetentionStrategy\$Always" | |
}, | |
"type": "hudson.slaves.DumbSlave", | |
"mode": "NORMAL", | |
"name": "${NODE_NAME}", | |
"numExecutors": "1", | |
"remoteFS": "/home/jenkins", | |
"nodeDescription": "Agent node description", | |
"labelString": "agent-node-label", | |
"nodeProperties": { | |
"stapler-class-bag": "true" | |
} | |
} | |
EOM | |
) | |
curl -v -L -s -o /dev/null -w "%{http_code}" -u "${JENKINS_USERNAME}:${JENKINS_TOKEN}" \ | |
-H "Content-Type:application/x-www-form-urlencoded" \ | |
-H "Jenkins-Crumb:${JENKINS_CRUMB}" \ | |
-X POST \ | |
-d 'json=${JSON_OBJECT}' "${JENKINS_URL}/computer/doCreateItem?name=${NODE_NAME}&type=hudson.slaves.DumbSlave" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment