Debian系統下Jenkins自定義構建腳本的完整流程
在Debian系統上,Jenkins的自定義構建腳本主要通過Pipeline(流水線)實現,支持將構建、測試、部署等步驟標準化、可視化。以下是具體操作步驟:
sudo apt update
sudo apt install openjdk-11-jdk
java -version # 驗證安裝(輸出Java版本信息即成功)
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list
sudo apt update
sudo apt install jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins # 開機自啟
http://<服務器IP>:8080
,通過/var/lib/jenkins/secrets/initialAdminPassword
獲取初始密碼解鎖,完成初始配置(如安裝推薦插件)。Manage Jenkins > Plugin Manager
,安裝Pipeline(核心插件)、Git(代碼托管庫集成)、SSH Agent(遠程服務器操作)等插件。New Item
,輸入任務名稱(如my-app-ci
),選擇Pipeline
類型,點擊OK
。Discard old builds
(避免磁盤空間占用,可設置保留最近10次構建)。Pipeline script from SCM
(從代碼倉庫獲取腳本,推薦方式)。https://github.com/your-repo/my-app.git
)。*/main
,表示main
分支)。Jenkinsfile
,即倉庫根目錄下的Jenkinsfile
文件)。Jenkinsfile是定義構建流程的核心腳本,采用Groovy語法,需放置在項目根目錄下(與代碼一起提交到倉庫)。以下是一個通用模板(可根據實際需求調整):
pipeline {
agent any // 使用任意可用節點執行(若需指定節點,可改為`agent { label 'ubuntu-node' }`)
environment {
// 定義環境變量(可選)
ARTIFACT_DIR = "target" // 構建產物目錄
DEPLOY_SERVER = "user@remote-server" // 遠程服務器地址
DEPLOY_PATH = "/opt/my-app" // 遠程部署路徑
}
stages {
stage('Checkout') {
steps {
git branch: 'main', url: 'https://github.com/your-repo/my-app.git' // 拉取代碼(可直接復用SCM配置,此處為冗余示例)
}
}
stage('Build') {
steps {
echo '正在構建項目...'
sh 'mvn clean package -Dmaven.test.skip=true' // Maven項目構建(跳過測試)
// 若為Python項目:sh 'python setup.py build'
// 若為Node.js項目:sh 'npm install && npm run build'
}
}
stage('Test') {
steps {
echo '正在運行測試...'
sh 'mvn test' // Maven項目運行單元測試
// 若為Python項目:sh 'python -m pytest tests/'
}
}
stage('Deploy') {
steps {
echo '正在部署到遠程服務器...'
sshPublisher(
publishers: [
sshPublisherDesc(
configName: 'remote-ssh', // 需提前在Jenkins中配置SSH憑據(Manage Jenkins > Credentials)
transfers: [
sshTransfer(
sourceFiles: "${ARTIFACT_DIR}/*.jar", // 本地構建產物路徑
removePrefix: ARTIFACT_DIR, // 移除本地路徑前綴
remoteDirectory: DEPLOY_PATH // 遠程目標路徑
)
],
usePromotionTimestamp: false,
useWorkspaceInPromotion: false,
verbose: true
)
]
)
// 若為直接執行腳本:sh 'ssh ${DEPLOY_SERVER} "cd ${DEPLOY_PATH} && ./restart.sh"'
}
}
}
post {
always {
echo '構建完成(無論成功失敗都會執行)'
junit '**/target/surefire-reports/*.xml' // 發布測試報告(Maven項目)
// 若為其他測試框架:junit '**/tests/results/*.xml'
}
success {
echo '構建成功!'
mail to: 'team@example.com', subject: "構建成功: ${currentBuild.fullDisplayName}", body: "項目 ${currentBuild.fullDisplayName} 構建成功!"
}
failure {
echo '構建失??!'
mail to: 'team@example.com', subject: "構建失敗: ${currentBuild.fullDisplayName}", body: "項目 ${currentBuild.fullDisplayName} 構建失敗,請檢查日志!"
}
}
}
關鍵說明:
any
表示任意可用節點,生產環境建議指定固定節點)。stage
(階段),每個stage
代表一個構建步驟(如Checkout
拉取代碼、Build
編譯、Test
測試、Deploy
部署)。stage
下的具體操作,常用sh
(執行Shell命令)、bat
(Windows批處理)、echo
(打印日志)等。若構建流程需要將產物部署到遠程服務器,需提前配置SSH憑據:
Manage Jenkins > Credentials
,點擊System
> Global credentials
。Add Credentials
,選擇SSH Username with private key
類型:
root
、ubuntu
)。Enter directly
,粘貼遠程服務器的私鑰(需提前生成,如ssh-keygen -t rsa
)。OK
保存。Build Now
即可手動啟動構建。Pipeline
配置中添加觸發器,如:
H/5 * * * *
表示每5分鐘檢查一次)。http://<Jenkins服務器IP>:8080/git/notifyCommit?url=<倉庫URL>
,實現代碼提交后自動觸發構建。Build History
中的構建編號,可查看:
junit
步驟)。target/*.jar
文件,可通過Artifacts
鏈接下載)。通過以上步驟,即可在Debian系統上完成Jenkins自定義構建腳本的配置。根據項目需求(如語言、部署目標),可調整Jenkinsfile中的steps
和environment
,實現靈活的CI/CD流程。