# Cài đặt Jenkins Agent trên Windows 10

### Step 1: Cài đặt phần mềm cần thiết

****JDK:****<span style="white-space: pre-wrap;"> JDK là cần thiết để chạy tiến trình của Jenkins Agent. Tại đây mình sử dụng JDK 21.</span>

****Git****: Git để cập nhật code tử các repo

****VSCode****: Code editor để sửa code nếu cần thiết.

****Unity****: UnityHub, Unity Editor, và các module cần thiết

Và cuối cùng là tạo thư mục làm việc riêng cho JenkinsAgent (ở đây là C:\\JenkinsAgent)

### Step 2: Tạo Node Agent trong Jenkins Controller

<span style="white-space: pre-wrap;">Truy cập </span>****Manage****<span style="white-space: pre-wrap;"> </span>****Jenkins -&gt; Nodes****<span style="white-space: pre-wrap;"> và tạo một Node mới.</span>

****Lưu ý:****  
<span style="white-space: pre-wrap;">– </span>****Permanent Agent**** phù hợp với case hiện có. Tức là một agent luôn sẵn sàng. Để phân biệt với Dynamic Agents.  
<span style="white-space: pre-wrap;">– </span>****Number of executors:****<span style="white-space: pre-wrap;"> số lượng build đồng thời có thể chạy trên agent.</span>  
<span style="white-space: pre-wrap;">– </span>****Remote root directory:****<span style="white-space: pre-wrap;"> đường dẫn đến thư mục làm việc đã tạo ở bước trên </span>`<span class="editor-theme-code">C:\JenkinsAgent</span>`.  
<span style="white-space: pre-wrap;">– </span>****Labels****: Nhãn phân loại các agent. Sau này trong Jenkins file sẽ dùng label để chị định job chạy trên agent nào (`<span class="editor-theme-code">agent { label 'windows && unity' }</span>`)  
<span style="white-space: pre-wrap;">– </span>****Usage****<span style="white-space: pre-wrap;"> là cách Jenkins lên lịch sử dụng agent này.</span>  
<span style="white-space: pre-wrap;">– </span>****Launch method:****<span style="white-space: pre-wrap;"> phương thức kết nối.</span>  
<span style="white-space: pre-wrap;">– </span>****Availability****: Kiểm soát khi nào Jenkins bắt đầu, kết thúc agent này.

### Step 3: Kết nối Agent

<span style="white-space: pre-wrap;">Truy cập vào node vừa tạo thông qua </span>****Manage****<span style="white-space: pre-wrap;"> </span>****Jenkins -&gt; Nodes****<span style="white-space: pre-wrap;"> ta sẽ thấy hướng dẫn kết nối bằng command line.</span>

Chọn command line phù hợp rồi chạy trên máy Agent qua cmd.

Khi chạy lệnh “`<span class="editor-theme-code">curl.exe -sO https://jenkins.thanhdv.com/jnlpJars/agent.jar & java -jar agent.jar -url https://jenkins.thanhdv.com/ -secret 73a6b19....e63df7 -name "Unity-WindowsAgent" -webSocket -workDir "C:\JenkinsAgent"</span>`<span style="white-space: pre-wrap;">” gặp lỗi không thể kết nối do truy cập thông qua reverse proxy. Để khắc phục thì không truy cập thông qua reverse proxy nữa mà truy cập qua port như thông thường. Thay </span>`<span class="editor-theme-code">https://jenkins.thanhdv.com</span>`<span style="white-space: pre-wrap;"> bằng </span>`<span class="editor-theme-code">http://thanhdv.com:8080</span>`

Lệnh chính xác cần chạy là  
`<span class="editor-theme-code">curl.exe -sO http://thanhdv.com:8080/jnlpJars/agent.jar & java -jar agent.jar -url http://thanhdv.com:8080/ -secret 73a6b193f2a487ccf1ccf5b494691be0319371e44ed67c4bab29835363e63df7 -name "Unity-WindowsAgent" -webSocket -workDir "C:\JenkinsAgent"</span>`

### Step 4: Chạy JenkinsAgent như một service của Windows (tuỳ chọn)

<span style="white-space: pre-wrap;">Lệnh trên chỉ chạy khi cửa sổ cmd đang bật gây bất tiện. Thế nên để sử dụng một cách thuận thiện hơn mình sẽ cài đặt nó như một service sử dụng </span>****WinSW****

Tải WinSW trên Github và lưu vào folder của Jenkins Agent (C:\\JenkinsAgent)  
<span style="white-space: pre-wrap;">Đổi tên file sao cho có ý nghĩa, ví dụ như </span>`<span class="editor-theme-code">jenkins-agent.exe</span>`

<span style="white-space: pre-wrap;">Tải file agent.jar qua đường dẫn trên </span>`<span class="editor-theme-code">http://thanhdv.com:8080/jnlpJars/agent.jar</span>`<span style="white-space: pre-wrap;"> có thể thấy ở command line. Lưu vào cùng thư mục làm việc với WinSW</span>

Tạo file xml với nội dung như sau:  
`<span class="editor-theme-code"><service></span>`  
`<span class="editor-theme-code">  <id>JenkinsAgentUnityWin</id> <name>Jenkins Agent (Unity Windows)</name> <description>Chạy Jenkins agent để build Unity trên máy Windows này, kết nối tới Jenkins Controller.</description></span>`  
`<span class="editor-theme-code">  <executable>%JAVA_HOME%\bin\java.exe</executable></span>`  
`<span class="editor-theme-code">  <arguments>-Xmx512m -jar agent.jar -url http://thanhdv.com:8080/ -secret 73a6b193f2a487ccf1ccf5b494691be0319371e44ed67c4bab29835363e63df7 -name "Unity-WindowsAgent" -webSocket -workDir "C:\JenkinsAgent"</arguments></span>`  
`<span class="editor-theme-code">  <log mode="roll-by-size"></span>`  
`<span class="editor-theme-code">    <sizeThreshold>10240</sizeThreshold> <keepFiles>5</keepFiles> </log></span>`  
`<span class="editor-theme-code">  <workingdirectory>C:\JenkinsAgent</workingdirectory></span>`  
`<span class="editor-theme-code"></service></span>`  
  
Lưu ý:  
File xml cần đặt tên giống với file exe phía trên (chỉ khác phần mở rộng xml và exe).  
**`<strong class="editor-theme-bold editor-theme-code"><executable></strong>`**<span style="white-space: pre-wrap;"> là đường dẫn đến java.exe. Nếu chưa có biến môi trường JAVA\_HOME thì cần thêm vào trong Windows.</span>  
**`<strong class="editor-theme-bold editor-theme-code"><arguments></strong>`**<span style="white-space: pre-wrap;"> chứa đầy đủ các tham số trên command line. Phần từ phía sau ký tự &amp;</span>  
**`<strong class="editor-theme-bold editor-theme-code"><workingdirectory></strong>`**<span style="white-space: pre-wrap;"> là đường dẫn đến thư mục làm việc của Jenkins Agent.</span>

Cài đặt service  
– Mở cmd trong cửa sổ thư mục của JenkinsAgent hoặc dùng lệnh cd để chuyển đến thư mục làm việc của JenkinsAgent  
<span style="white-space: pre-wrap;">– Chạy lệnh </span>`<span class="editor-theme-code">.\jenkins-agent.exe install</span>`<span style="white-space: pre-wrap;"> để cài đặt.</span>  
<span style="white-space: pre-wrap;">– Chạy tiếp lệnh </span>`<span class="editor-theme-code">.\jenkins-agent.exe start</span>`<span style="white-space: pre-wrap;"> để chạy service (Hoặc chạy trong của sổ services.msc tìm tới tên service </span>`<span class="editor-theme-code">Jenkins Agent (Unity Windows)</span>`<span style="white-space: pre-wrap;"> setup trong file xml)</span>  
– Kiểm tra trên Jenkins (****Manage Jenkins****<span style="white-space: pre-wrap;"> -&gt; </span>****Nodes****) nếu thấy nodes online tức là đã thành công.  
<span style="white-space: pre-wrap;">– Setup service auto restart. Trong cửa sổ </span>****services.msc -&gt; Recovery****. Chuyển tất cả các trường “****First failure****“, “****Second failure****“, “****Subsequent failures****” thành “****Restart the Service****“

****Lưu ý:****  
<span style="white-space: pre-wrap;">– Gỡ cài đặt service bằng lệnh </span>`<span class="editor-theme-code">.\jenkins-agent.exe uninstall</span>`  
– Nếu chạy service gặp lỗi có thể kiểm tra các file log trong thư mục cài đặt của service (trong trường hợp này là C:\\JenkinsAgent)