Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux 上安裝配置 Jenkins 持續整合平台完整實戰教學

編輯:關於 Linux

Jenkins 是最廣泛採用的開源 CI/CD 平台,支援豐富外掛生態。本文記錄在 CentOS 環境下透過 yum 安裝 Jenkins、整合 Git/GitHub、設定 SSH 金鑰與郵件通知的完整流程,適合快速建置自動化建置流水線。

Jenkins 專案建立介面

Jenkins SSH 金鑰設定

環境準備

  • JDK 1.8+(建議 OpenJDK 11/17)
  • CentOS 7/8、RHEL 或相容發行版

安裝 Jenkins(CentOS/yum 方式)

# 匯入官方倉庫與金鑰
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

# 安裝 Jenkins 並啟動
sudo yum install -y jenkins
sudo systemctl enable --now jenkins

# 防火牆開放 8080
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload

瀏覽器開啟 https://:8080,依提示完成初始化解鎖(初始密碼位於 /var/lib/jenkins/secrets/initialAdminPassword)。

安裝 Git 與必要工具

sudo yum install -y git zlib-devel openssl-devel perl cpio expat-devel gettext-devel curl-devel autoconf
# 若需特定版本可自行編譯安裝

Jenkins 使用者 SSH 設定(用於拉取私有倉庫)

# 修改 jenkins 使用者 shell
sudo usermod -s /bin/bash jenkins

# 切換至 jenkins 使用者產生金鑰
sudo su - jenkins
ssh-keygen -t rsa -b 4096 -C "jenkins@yourdomain.com"
# 將 ~/.ssh/id_rsa.pub 內容加入 GitHub/GitLab Deploy Keys 或使用者 SSH Keys

Web 介面關鍵設定

  1. 安裝外掛:系統管理 → 外掛管理 → 可選外掛 → 搜尋並安裝 Git plugin、GitHub plugin、Pipeline 等。
  2. 全域工具設定:系統管理 → 全域工具設定 → Git 路徑(通常 /usr/bin/git)、Git 使用者名稱/信箱。
  3. GitHub 整合:系統管理 → 系統設定 → GitHub Server → 新增憑證 → 設定 Webhook URL。
  4. 郵件通知:系統管理 → 系統設定 → E-mail Notification → 填入 SMTP 伺服器、帳號密碼 → 測試發送。

建立第一個 Pipeline 專案

# Jenkinsfile 範例(放置於專案根目錄)
pipeline {
    agent any
    stages {
        stage('Checkout') { steps { git 'git@github.com:user/repo.git' } }
        stage('Build')    { steps { sh 'mvn clean package' } }
        stage('Test')     { steps { sh 'mvn test' } }
        stage('Deploy')   { steps { sh './deploy.sh' } }
    }
    post {
        always { mail to: 'team@example.com', subject: "Build ${currentBuild.result}", body: "${BUILD_URL}" }
    }
}

新建專案選擇「Pipeline」,SCM 選 Git,填入倉庫 URL 與憑證,Script Path 填 Jenkinsfile 即可。

Copyright © Windows教程網 All Rights Reserved