Try   HackMD

再 Jenkins 底下執行 NodeJs

tags: Jenkins

介紹

npm為現今最熱門的套件管理系統,在運行時會需要安裝Node.js。延續上一篇透過 Docker 建立 Jenkins完成後,接著我們想要再作業上操作npm相關指令,此篇就是來說明如何在Jenkins上運行。

目錄

一、前言

這邊會直接再所建立的Jenkins(master node)上操作,不過實際運作上建議再建立一個agent node下去執行,主要理由為安全性問題詳情可以參考文章,接著直接進入實作環節。

二、套件安裝

登入Jenkins後點選左側選單管理Jenkins進入管理頁面,接著點選管理外掛程式如下圖:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

接著再搜尋框輸入node並選擇下方的NodeJs,之後點選下方安裝即可

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

這邊可以點選下方選項讓Jenkins重新啟動

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
如果跟我一樣是使用Docker運行記得手動去啟動Jenkins,別傻傻的等

三、全域工具設定

直接進入管理Jenkins頁面,接著點選全域工具設定如下圖:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

進入頁面後拉到最下方會出現一個NodeJs的設定選項

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

接著這邊說明三種方式較常使用指定Nodejs的方式

1. 自動安裝

直接選擇需要的版本,名稱的話直接輸入對應版本名稱,接著點選儲存即可

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

2. 自動安裝 - 透過指定路徑下安裝

在某些情況下安裝相關軟體都必須走公司的規定,其中也提供透過連接下載安裝檔並安裝的方式

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

3. 已安裝指定

當如果你已經安裝好了Nodejs,此時只需要指定路徑即可

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

四、作業Node語法測試

這邊直接點選新增作業來新增一筆作業

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

接著取個好名字,接著我們這邊是使用Pipeline流程來進行操作,接著點選確定

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

進入畫面後直接捲到最下方的Pipeline區塊進行腳本撰寫,完成後直接點選儲存

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

這邊直接附上腳本

pipeline { agent any tools { nodejs '16.17.1' } stages { stage('NodeVer') { steps { echo 'Hello World' sh 'npm version' } } } }

該腳本大致上小小說明一下

  • agent any:在任何可用的代理上執行
  • tool 區塊:這邊指定了Nodejs的版本
  • stages 區塊:可以將各階段進行分類,並且相關運行可以寫在steps當中

運行建置

手動運行非常簡單,這邊直接點選馬上建置會直接運行,下方則會有建置編號

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

點選建置編號後可以查看該流程下的相關資訊,點選Console Output會看到運行輸出,從輸出可以看到再安裝NodeJs

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

最後附上腳本運行結果,其Node版本為所指定的16.17.1

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
這邊使用自動安裝的方式,所以在執行時會先下載安裝對應的NodeJs,第二次執行則會直接執行

結論

經過這樣簡單操作下來,可以發現我們能透過腳本進行許多自動化的作業,這邊只是針對npm做版本顯示,後續想要加入建置、測試及部屬等作業也可以透過腳本來處理,這邊算是只做了一個小小的起頭,未來有機會在將其它流程加入。