我努力从专用的从属计算机(Windows 2012 R2服务器)启动(动态)启动Jenkins从属代理。Jenkins主服务器(版本1.617-我可以根据需要进行升级,但不能降级(在版本1.498之前不需要凭据))位于Windows 2012 R2服务器上。通过Active Directory插件和基于项目的矩阵授权策略来启用和配置安全性。
由于涉及到Active Directory,因此我不能简单地添加系统用户(通过-jnlpCredentials username:password
或通过-jnlpCredentials username:apitoken
)进行身份验证。作为一种解决方法,我正在使用我的Jenkins服务用户,但是我不喜欢API令牌位于脚本中的硬编码中。
我正在尝试使用替代方法-secret secretKey
,但是当在主节点上注册一个从属节点时,将随机创建secretKey。
由于我使用的是Azure Slave插件,因此将为我创建从属节点和关联的虚拟机。虚拟机是根据预定义的映像创建的,可以根据需要进行任何更改。
在此预定义的映像中,我具有在启动时执行的PowerShell脚本。它来自此处给出的样本。不必一定是PowerShell,也可以采用其他任何方式。
Set-ExecutionPolicy Unrestricted
# base url to Jenkins master
$jenkinsserverurl = "https://jenkins.mycompany.com/"
# the azure-slave-plugin is creating VMs with names like 'Azure0807150842'
$vmname = (Get-Culture).TextInfo.ToTitleCase($env:computername.tolower())
# authenticate with Jenkins service user + API-token - since we don't know the '-secret'
$apiToken="jenkins_user:1234abcdefab56c7d890de1f2a345b67"
Write-Output "Downloading jenkins slave jar "
# in order to avoid updating it manually for Jenkins master updates
$slaveJarSource = $jenkinsserverurl + "jnlpJars/slave.jar"
$slaveJarLocal = "C:\jenkins_home\slave.jar"
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($slaveJarSource, $slaveJarLocal)
Write-Output "Executing slave process "
$jnlpSource = $jenkinsserverurl+"computer/" + $vmname + "/slave-agent.jnlp"
# expect java.exe in the PATH, and use -noCertificateCheck to skip SSL validation
& java -jar $slaveJarLocal -jnlpCredentials $apiToken -jnlpUrl $jnlpSource -noCertificateCheck
下载JNLP文件并读取其中包含的秘密是没有选择的,因为我也需要在Jenkins主服务器上进行正确的HTTP身份验证。
Write-Output "Downloading jenkins slave jnlp "
$jnlpSource = $jenkinsserverurl+"computer/" + $vmname + "/slave-agent.jnlp"
$jnlpLocal = "C:\jenkins_home\slave-agent.jnlp"
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($jnlpSource, $jnlpLocal)
Write-Output "Extracting secret from jenkins slave jnlp "
[xml]$jnlpFile = Get-Content $jnlpLocal
# the first argument in the generated JNLP contains the secret
$secret = Select-Xml "//jnlp/application-desc/argument[1]/text()" $jnlpFile
在理想情况下,创建从属节点及其VM的插件将登录到创建的VM,并执行与我的问题类似的脚本-添加注入的Jenkins服务器URL,VM名称和生成的密钥。由于当前的Azure Slave插件版本不是这种情况,因此我只能使用现有的Jenkins服务用户使用解决方法脚本。
我用它来让插件动态创建一个更大/更快的虚拟机,该虚拟机仅用于日常测试运行,并在其余时间自动关闭(因此,在不使用时不会产生任何费用)。如果有人感兴趣,那是我最终得到的设置:
通用Azure VM映像(Windows 2012 R2,已安装JDK,Maven,Git)。通过NSSM,我将PowerShell脚本(启动从属代理)安装为Windows服务,并在计算机启动时自动执行(与问题相同):
Set-ExecutionPolicy Unrestricted
# base url to Jenkins master
$jenkinsserverurl = "https://jenkins.mycompany.com/"
# the azure-slave-plugin is creating VMs with names like 'Azure0807150842'
$vmname = (Get-Culture).TextInfo.ToTitleCase($env:computername.tolower())
# authenticate with Jenkins service user + API-token - since we don't know the '-secret'
$apiToken="jenkins_user:1234abcdefab56c7d890de1f2a345b67"
Write-Output "Downloading jenkins slave jar "
# in order to avoid updating it manually for Jenkins master updates
$slaveJarSource = $jenkinsserverurl + "jnlpJars/slave.jar"
$slaveJarLocal = "C:\jenkins_home\slave.jar"
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($slaveJarSource, $slaveJarLocal)
Write-Output "Executing slave process "
$jnlpSource = $jenkinsserverurl+"computer/" + $vmname + "/slave-agent.jnlp"
# expect java.exe in the PATH, and use -noCertificateCheck to skip SSL validation
& java -jar $slaveJarLocal -jnlpCredentials $apiToken -jnlpUrl $jnlpSource -noCertificateCheck
安装了Azure Slave插件的Jenkins主服务器已配置并配置为使用此VM映像,并在五分钟后空闲时关闭。
配置为仅在Azure从属节点上运行的Jenkins Maven项目(Job),从Git检出我的测试项目,然后从那里执行jUnit Selenium测试。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句