Install Jenkins slave as a Windows service in command line

前端 未结 3 1059
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 10:46

I have been looking a lot on Google on how to install the service in command line (so without manual interaction) but I am stuck on how to get the jenkins-slave.exe

<
相关标签:
3条回答
  • 2020-12-30 10:59

    Or is there a way to run the "Install as a service" in command line from the slave.jar?

    I don't use jenkins-slave.exe, but instead a custom script in which I can control the exact environment variable I want to set for the Jenkins slave, when launching java -jar slave.jar with the secret key you can see in the Jenkins master node page for that new slave.

    To get slave.jar from the master onto the slave, execute from the slave Windows server:

    curl -o slave.jar https://your.server/jenkins/jnlpJars/slave.jar
    

    To replace the jenkins-slave.exe, I use a script declared as a Windows service, with nssm

    The script is similar to agent.bat:

    set PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0
    set PATH=D:\Tools\SonarRunner\bin;%PATH%
    set M2_HOME=D:\Tools\apache-maven-3.5.0
    set PATH=%M2_HOME%\bin;%PATH%
    set PATH=D:\Tools\apache-ant-1.9.3\bin;%PATH%
    set GH=D:\Tools\Git
    set PATH=%GH%\bin;%GH%\usr\bin;%GH%\mingw64\bin;%PATH%
    set PATH=%JAVA_HOME%\bin;%PATH%
    
    set WORKSPACE_FOLDER=D:\Jenkins\workspace
    set GIT_WORKSPACE_FOLDER=D:\Jenkins\workspace
    
    java -Xmx768m -jar slave.jar -jnlpUrl https://your.server/jenkins/computer/<SlaveName>/slave-agent.jnlp -secret 87ef3d...
    

    That script is then called as a Windows service, ran by a dedicated user account:

    runas /user:<domain>\<jenkinsUser> cmd ( enter `jenkinsUser` Windows password )
    
    D:\Tools\nssm-2.24\win64\nssm.exe install <SlaveName> D:\Jenkins\agent.bat
    

    Its Windows service is then configured:

    sc config <SlaveName> obj= <domain>\<jenkinsUsers> password= <jenkinsUser password>
    sc config <SlaveName> start= auto
    

    For automating the installation of other software: see Chocolatey - Software Management Automation, The package manager for Windows.


    To fully automate the declaration-side of slaves, use the web API to create the slave, and a groovy script to retrieve the Jenkins node/slave secret JnlpMac key.
    See this script for the creation.
    And the groovy script (with Jenkins 2.46 or newer) to get the secret key:

    echo 'println jenkins.model.Jenkins.instance.nodesObject.getNode("my-agent")?.computer?.jnlpMac' \
      | java -jar ~/Downloads/jenkins-cli.jar -s https://jenkins/ groovy =
    
    0 讨论(0)
  • 2020-12-30 11:05

    What I would do is:

    • Download the slave.jar file (from the node's page of Jenkins)
    • Copy the java -jar slave.jar -jnlpUrl http://<YOUR URL HERE> command from the node's page
    • Paste this command into a new .bat file and save it
    • Register a scheduled task to run this .bat file when Windows starts
    0 讨论(0)
  • 2020-12-30 11:19

    To answer my own question, after having contacted the plugin developers:

    There is no actual way to download the exe from Jenkins directly, the slave.jar gets it from the master via the remoting protocol. I have created a request to be able to download it via an URL (as suggested by the developer), so it might be available in the future.

    Right now the executable is a renamed Windows Service Wrapper binary: https://github.com/kohsuke/winsw so I used this binary the same way.

    Regarding the configuration used by WinSW and XML files, I used the one from the GitHub repository https://github.com/jenkinsci/windows-slave-installer-module. The versions are compatible in terms of the configuration.

    So basically I download the exe, get the private key from Jenkins and create the service using the configuration from the original plugin. Then I install the service using jenkins-slave.exe install.

    Step by step:

    • Get the JNLP command from Jenkins (from the Node page) to get the private key, e.g. java -jar slave.jar -jnlpUrl http://jenkins...
    • Download the slave.jar file from Jenkins (gotten from the JNLP command)
    • Download the service wrapper executable, e.g. http://repo.jenkins-ci.org/public/com/sun/winsw/winsw/2.1.0/winsw-2.1.0-bin.exe
    • Setup the XML used to run the service (available in the module or directly on winsw website)
    • Setup the slave configuration XML file (available on the module source code)
    • Then install the service using jenkins-slave.exe install
    0 讨论(0)
提交回复
热议问题