You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Jenkins任务部署Jar包到Windows服务器及配置SSH凭证

How to Configure Jenkins to Copy JARs to a Windows Server

Got it, since you already have SSH working for Linux servers, let's break down how to set this up for Windows—there are two solid approaches depending on your preference: using SSH (to keep consistency with your existing Linux workflow) or using SMB (the native Windows file sharing protocol).

Option 1: Set Up SSH on Windows (Consistent with Linux Workflow)

Windows 10/11 and Server 2019+ come with OpenSSH Server built-in, so this is a straightforward way to reuse your existing SSH knowledge.

Step 1: Install and Enable OpenSSH Server on Windows

  • Via GUI: Go to Settings → Apps → Optional features → Add a feature, search for "OpenSSH Server", install it, then start the service from Services (look for sshd and set it to Automatic startup).
  • Via PowerShell (faster):
    # Install OpenSSH Server
    Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
    # Start and enable the service
    Start-Service sshd
    Set-Service -Name sshd -StartupType 'Automatic'
    

Step 2: Add Windows SSH Credentials in Jenkins

This works almost exactly like your Linux setup:

  1. Go to Manage Jenkins → Manage Credentials.
  2. Add a new credential:
    • For password auth: Select "Username with password". Use the Windows account username (format: .\LocalUsername for local accounts, Domain\DomainUsername for domain accounts) and its password. Give it a descriptive ID for easy reference.
    • For SSH key auth:
      • On the Windows server, run ssh-keygen in PowerShell to generate a key pair (accept defaults unless you need a custom path).
      • Copy the public key (from C:\Users\<YourUsername>\.ssh\id_rsa.pub) into C:\Users\<YourUsername>\.ssh\authorized_keys (create the file if it doesn't exist).
      • In Jenkins, add an "SSH Username with private key" credential, paste the private key (from your local machine or Jenkins master) and the Windows username.

Step 3: Transfer Files via SSH in Your Jenkins Job

  1. Make sure the SSH plugin is installed in Jenkins (if not, grab it from the Plugin Manager).
  2. In your job's Post-build Actions, add "Send files or execute commands over SSH".
  3. Configure the SSH server:
    • Enter your Windows server's IP/hostname, port 22 (default).
    • Select the SSH credential you created.
    • Under Transfers, set:
      • Source files: **/*.jar (or the specific path to your built JAR)
      • Remote directory: C:\deploy\ (Windows path—you can use forward slashes too, like C:/deploy/, Jenkins will handle it)
  4. Save and test the job—your JAR should copy over successfully.

Option 2: Use SMB File Sharing (Native Windows Approach)

If you'd rather avoid setting up SSH, SMB is the standard way to share files on Windows.

Step 1: Create a Shared Folder on Windows

  1. On your Windows server, create a folder (e.g., C:\JenkinsDeploy).
  2. Right-click the folder → Properties → Sharing tab → Advanced Sharing.
  3. Check "Share this folder", set a share name (e.g., JenkinsDeploy), then click Permissions.
  4. Grant read/write access to the Windows account you'll use from Jenkins (local or domain account).

Step 2: Add SMB Credentials in Jenkins

  1. Go to Manage Jenkins → Manage Credentials.
  2. Add a "Username with password" credential, using the same Windows account details as above (format: .\LocalUsername or Domain\DomainUsername).

Step 3: Configure SMB File Transfer in Jenkins

  1. Install the Publish Over SMB plugin from the Plugin Manager.
  2. Go to Manage Jenkins → Configure System, scroll down to SMB Publishers.
  3. Add a new SMB server:
    • Name: A descriptive name for your server.
    • Host: Your Windows server's IP/hostname.
    • Share: The share name you set earlier (e.g., JenkinsDeploy).
    • Credentials: Select the SMB credential you created.
    • Click Test Connection to confirm it works.
  4. In your job's Post-build Actions, add "Send files over SMB".
  5. Set:
    • Source files: **/*.jar
    • Remote directory: ./ (or a subfolder in the share, like ./latest/)
  6. Save and run the job—your JAR will be copied to the shared folder.

Key Notes to Avoid Headaches

  • Firewall Rules: On the Windows server, make sure to open the necessary ports:
    • SSH: Port 22
    • SMB: Port 445
  • Permissions: Double-check that the Jenkins-used account has write access to the target directory on Windows—this is the most common issue!
  • Path Formatting: When specifying Windows paths in Jenkins, forward slashes (/) usually work, but if you use backslashes (\), you'll need to escape them (e.g., C:\\deploy).

内容的提问来源于stack exchange,提问作者Kanchan Kauthale

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:31:02