How to Configure a Jenkins Windows Agent to Run as a Specific User without Password Prompt

Question:

How can I configure a Jenkins Windows agent to run as a service or connect automatically on boot?

I have a Jenkins Windows agent that requires manual login and batch execution to connect to the master. I would like to automate this process and run the agent as a specific user without entering the password. I know that Jenkins used to support installing agents as services, but this feature was deprecated. I attempted to use sc.exe to create a service, but it did not work as expected. Is there any alternative solution to achieve this functionality?

Thank you for your assistance.

Answer:

Jenkins is a popular open-source automation server that allows users to run continuous integration and delivery pipelines. Jenkins can run on various platforms, including Windows, Linux, and macOS. Jenkins agents are processes that connect to the Jenkins master and execute tasks on behalf of the master. Jenkins agents can run on the same machine as the master or on different machines.

One of the challenges of using Jenkins agents on Windows is how to configure them to run as a service or connect automatically on boot. This is useful for scenarios where the agent machine needs to be restarted frequently, or where the agent needs to run under a specific user account without manual intervention. In this article, we will explore some of the possible solutions to achieve this functionality.

Option 1: Use the Jenkins agent installer

One of the easiest ways to install a Jenkins agent as a service on Windows is to use the Jenkins agent installer. This is a Java Web Start (JNLP) application that can be launched from the Jenkins master web interface. To use this option, follow these steps:

  • On the Jenkins master, go to Manage Jenkins > Manage Nodes and Clouds > New Node.
  • Enter a name for the agent node and select Permanent Agent.
  • Configure the agent node properties, such as Remote root directory, Labels, Usage, and Launch method. For the launch method, select Launch agent by connecting it to the master.
  • Click Save to create the agent node.
  • On the agent machine, open a web browser and go to the Jenkins master URL. Navigate to the agent node page and click on the Launch button.
  • A JNLP file will be downloaded. Run the file with Java Web Start. This will launch the Jenkins agent installer.
  • Follow the instructions on the installer to install the agent as a service. You will need to provide the user account and password that the agent will run under. You can also configure the service name, description, and startup type.
  • Once the installation is complete, the agent service will start automatically and connect to the master. You can verify the connection status on the agent node page.
  • Option 2: Use NSSM (Non-Sucking Service Manager)

    Another way to install a Jenkins agent as a service on Windows is to use NSSM (Non-Sucking Service Manager). This is a third-party tool that can create and manage Windows services from any executable file. To use this option, follow these steps:

  • On the Jenkins master, go to Manage Jenkins > Manage Nodes and Clouds > New Node.
  • Enter a name for the agent node and select Permanent Agent.
  • Configure the agent node properties, such as Remote root directory, Labels, Usage, and Launch method. For the launch method, select Launch agent via execution of command on the master.
  • In the Command field, enter the command that will launch the agent on the remote machine. For example, you can use PowerShell Remoting to invoke the agent JAR file. The command should look something like this:
  • “`powershell

    powershell -NoProfile -ExecutionPolicy Bypass -Command “& {Invoke-Command -ComputerName -ScriptBlock {java -jar C:\jenkins\agent.jar -jnlpUrl /computer//slave-agent.jnlp -secret -workDir C:\jenkins}}”

    “`

  • Replace the placeholders with the actual values for your agent machine name, master URL, agent node name, secret, and work directory. You can find the secret on the agent node page under the Advanced button.
  • Click Save to create the agent node.
  • On the agent machine, download and install NSSM from https://nssm.cc/.
  • Open a command prompt as administrator and run the following command to create a service for the agent:
  • “`cmd

    nssm install JenkinsAgent

    “`

  • A GUI window will open. In the Application tab, enter the path to the PowerShell executable in the Path field, and the command that will launch the agent in the Arguments field. For example:
  • “`cmd

    Path: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

    Arguments: -NoProfile -ExecutionPolicy Bypass -Command “& {Invoke-Command -ComputerName -ScriptBlock {java -jar C:\jenkins\agent.jar -jnlpUrl /computer//slave-agent.jnlp -secret -workDir C:\jenkins}}”

    “`

  • In the Log on tab, enter the user account and password that the agent will run under.
  • In the Details tab, enter a name and description for the service.
  • Click Install service to create the service.
  • Open the Services Manager and start the JenkinsAgent service. The service will launch the agent and connect to the master. You can verify the connection status on the agent node page.
  • Option 3: Use Task Scheduler

    A third way to run a Jenkins agent on Windows is to use Task Scheduler. This is a built-in Windows feature that can run programs or scripts at a specified time or event. To use this option, follow these steps:

  • On the Jenkins master, go to Manage Jenkins > Manage Nodes and Clouds > New Node.
  • Enter a name for the agent node and select Permanent Agent.
  • Configure the agent node properties, such as Remote root directory, Labels, Usage, and Launch method. For the launch method, select Launch agent via execution of command on the master.
  • In the Command field, enter the command that will launch the agent on the remote machine. For example, you can use PowerShell Remoting to invoke the agent JAR file. The command should look something like this:
  • “`powershell

    powershell -NoProfile -ExecutionPolicy Bypass -Command “& {Invoke-Command -ComputerName -ScriptBlock {java -jar C:\jenkins\agent.jar -jnlpUrl /computer//slave-agent.jnlp -secret -workDir C:\jenkins}}”

    “`

  • Replace the placeholders with the actual values for your agent machine name, master URL, agent node name, secret, and work directory. You can find the secret on the agent node page under the Advanced button.
  • Click Save to create the agent node.
  • On the agent machine, open Task Scheduler and create a new task.
  • In the General tab, enter a name and description for the task. Check the option to Run whether user is logged on or not and enter the user account and password that the agent will run under.
  • In the Triggers tab, create a new trigger that will start the task At startup.
  • In the Actions tab, create a new action that will start a program. In the Program/script field, enter the path to the PowerShell executable. In the Add arguments field, enter the command that will launch the agent. For example:
  • “`cmd

    Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

    Add arguments: -NoProfile -ExecutionPolicy Bypass -Command “& {Invoke-Command -ComputerName -ScriptBlock {java -jar C:\jenkins\agent.jar -jnlpUrl /computer//slave-agent.jnlp -secret -workDir C:\jenkins}}”

    “`

  • Click OK to save the task.
  • Restart the agent machine. The task will run on startup and launch the agent. The agent will connect to the master. You can verify the connection status on the agent node page.
  • Conclusion

    In

this article, we have discussed three possible ways to configure a Jenkins Windows agent to run as a service or connect automatically on boot. Each option has its own advantages and disadvantages, depending on your specific needs and preferences. You can choose the option that suits you best and enjoy the benefits of Jenkins automation on Windows.

Leave a Reply

Your email address will not be published. Required fields are marked *

Privacy Terms Contacts About Us