如何通过命令行创建Jenkins从节点?含Windows及新VM接入场景
Alright, let's break this down into two clear parts as you asked—covering both direct command-line slave creation and integrating a new Windows VM into your Jenkins master after provisioning.
1. Directly Create a Jenkins Slave Node via Command Line
The easiest way to do this is using the Jenkins CLI or REST API. You'll need admin access to your Jenkins master, plus an API token for your admin user.
Option 1: Use Jenkins CLI
First, grab the Jenkins CLI JAR from your master:
# Windows PowerShell Invoke-WebRequest -Uri "http://<YOUR_JENKINS_URL>/jnlpJars/jenkins-cli.jar" -OutFile "jenkins-cli.jar"
Next, define your slave configuration as XML and pipe it to the CLI to create the node. Replace placeholders like <YOUR_JENKINS_URL>, <ADMIN_USER>, <API_TOKEN>, and slave details with your actual values:
$slaveConfig = @" <slave> <name>my-windows-slave</name> <description>Windows slave created via CLI</description> <remoteFS>C:\jenkins-workspace</remoteFS> <numExecutors>2</numExecutors> <mode>NORMAL</mode> <retentionStrategy class="hudson.slaves.RetentionStrategy`$Always"/> <launcher class="hudson.slaves.JNLPLauncher"> <workDirSettings> <disabled>false</disabled> <internalDir>remoting</internalDir> <failIfWorkDirIsMissing>false</failIfWorkDirIsMissing> </workDirSettings> </launcher> <label>windows build-node</label> <nodeProperties/> </slave> "@ # Send the config to Jenkins to create the node $slaveConfig | java -jar jenkins-cli.jar -s "http://<YOUR_JENKINS_URL>/" --username "<ADMIN_USER>" --password "<API_TOKEN>" create-node "my-windows-slave"
Option 2: Use Jenkins REST API
If you prefer REST, create a slave-config.xml file with the same XML as above, then run this PowerShell command to post it to Jenkins:
$auth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("<ADMIN_USER>:<API_TOKEN>")) Invoke-WebRequest -Uri "http://<YOUR_JENKINS_URL>/computer/doCreateItem?name=my-windows-slave" ` -Method POST ` -Headers @{Authorization = "Basic $auth"} ` -ContentType "application/xml" ` -InFile "slave-config.xml"
2. Integrate a New Windows VM as Jenkins Slave (Post-Provisioning)
Once your VM creation script finishes, you need two steps: configure the slave node on Jenkins master (via command line) and set up the agent on the VM to connect back to the master.
Step 1: Master Node Command-Line Setup
First, create the slave node on Jenkins using the CLI/REST method from Part 1 (make sure the launcher is set to JNLPLauncher). Then, fetch the slave's secret key (needed for the VM to authenticate):
# Get the slave config XML and extract the secret $nodeXml = java -jar jenkins-cli.jar -s "http://<YOUR_JENKINS_URL>/" --username "<ADMIN_USER>" --password "<API_TOKEN>" get-node "my-windows-slave" $slaveSecret = ([xml]$nodeXml).slave.launcher.secret
Pass this secret to your VM provisioning script (e.g., via environment variable or remote execution).
Step 2: VM-Side Command-Line Setup
On the new Windows VM, run these commands automatically (e.g., via PowerShell remote execution or a startup script):
1. Prepare the Jenkins Agent Environment
# Create workspace directory mkdir C:\jenkins-slave -Force # Install Chocolatey (for easy Java installation, skip if Java is already present) Set-ExecutionPolicy Bypass -Scope Process -Force [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072 iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) # Install OpenJDK choco install openjdk -y --no-progress
2. Download and Run the Jenkins Agent
# Download the Jenkins agent JAR Invoke-WebRequest -Uri "http://<YOUR_JENKINS_URL>/jnlpJars/agent.jar" -OutFile "C:\jenkins-slave\agent.jar" # Connect to Jenkins master using the secret from Step 1 java -jar C:\jenkins-slave\agent.jar ` -jnlpUrl "http://<YOUR_JENKINS_URL>/computer/my-windows-slave/slave-agent.jnlp" ` -secret "$slaveSecret" ` -workDir "C:\jenkins-slave"
3. (Optional) Run Agent as a Windows Service
To make the agent persistent through reboots, use nssm (Non-Sucking Service Manager):
# Download and extract nssm Invoke-WebRequest -Uri "https://nssm.cc/release/nssm-2.24.zip" -OutFile "C:\jenkins-slave\nssm.zip" Expand-Archive -Path C:\jenkins-slave\nssm.zip -DestinationPath C:\jenkins-slave\nssm # Install the agent as a service C:\jenkins-slave\nssm\win64\nssm install JenkinsAgent "C:\Program Files\OpenJDK\jdk-17\bin\java.exe" C:\jenkins-slave\nssm\win64\nssm set JenkinsAgent AppParameters "-jar C:\jenkins-slave\agent.jar -jnlpUrl http://<YOUR_JENKINS_URL>/computer/my-windows-slave/slave-agent.jnlp -secret $slaveSecret -workDir C:\jenkins-slave" C:\jenkins-slave\nssm\win64\nssm set JenkinsAgent DisplayName "Jenkins Slave Agent" # Start the service C:\jenkins-slave\nssm\win64\nssm start JenkinsAgent
内容的提问来源于stack exchange,提问作者Epligam

