文章目录

  • 环境
  • 安装
    • 下载安装
    • 启动
    • 初始化
  • Pipeline
    • UI
    • SCM(Source Control Management)
      • 准备
      • pipeline
  • 参考

环境

  • RHEL 9.3
  • Jenkins 2.44.0.1

安装

参考 https://www.jenkins.io/doc/book/installing/linux/#red-hat-centos

下载安装

[ding@192 ~]$ sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo--2024-03-02 18:13:19--https://pkg.jenkins.io/redhat-stable/jenkins.repoResolving pkg.jenkins.io (pkg.jenkins.io)... 2a04:4e42:1a::645, 151.101.110.133Connecting to pkg.jenkins.io (pkg.jenkins.io)|2a04:4e42:1a::645|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 85Saving to: ‘/etc/yum.repos.d/jenkins.repo’/etc/yum.repos.d/jenkins.repo 100%[========================================================================================================================================>]85--.-KB/sin 0s2024-03-02 18:13:20 (3.03 MB/s) -/etc/yum.repos.d/jenkins.repo’ saved [85/85]
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
sudo yum upgrade
sudo yum install fontconfig java-17-openjdk # Add required dependencies for the jenkins package
sudo yum install jenkins
sudo systemctl daemon-reload

启动

设置开机启动:

sudo systemctl enable jenkins

启动:

sudo systemctl start jenkins

检查Jenkins状态:

[ding@192 ~]$ sudo systemctl status jenkins● jenkins.service - Jenkins Continuous Integration Server Loaded: loaded (/usr/lib/systemd/system/jenkins.service; enabled; preset: disabled) Active: active (running) since Sat 2024-03-02 18:18:30 CST; 19s ago Main PID: 9453 (java)Tasks: 49 (limit: 22752) Memory: 1.0GCPU: 34.636s CGroup: /system.slice/jenkins.service └─9453 /usr/bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=/var/cache/jenkins/war --httpPort=8080Mar 02 18:18:12 192.168.1.11 jenkins[9453]: 9cc784ffe4ad46e4bb2b8a1a4116839bMar 02 18:18:12 192.168.1.11 jenkins[9453]: This may also be found at: /var/lib/jenkins/secrets/initialAdminPasswordMar 02 18:18:12 192.168.1.11 jenkins[9453]: *************************************************************Mar 02 18:18:12 192.168.1.11 jenkins[9453]: *************************************************************Mar 02 18:18:12 192.168.1.11 jenkins[9453]: *************************************************************Mar 02 18:18:30 192.168.1.11 jenkins[9453]: 2024-03-02 10:18:30.504+0000 [id=33]INFOjenkins.InitReactorRunner$1#onAttained: Completed initializationMar 02 18:18:30 192.168.1.11 jenkins[9453]: 2024-03-02 10:18:30.526+0000 [id=24]INFOhudson.lifecycle.Lifecycle#onReady: Jenkins is fully up and runningMar 02 18:18:30 192.168.1.11 systemd[1]: Started Jenkins Continuous Integration Server.Mar 02 18:18:31 192.168.1.11 jenkins[9453]: 2024-03-02 10:18:31.611+0000 [id=49]INFOh.m.DownloadService$Downloadable#load: Obtained the updated data file for hudson.tasks.Maven.MavenInstallerMar 02 18:18:31 192.168.1.11 jenkins[9453]: 2024-03-02 10:18:31.613+0000 [id=49]INFOhudson.util.Retrier#start: Performed the action check updates server successfully at the attempt #1

至此,Jenkins已经安装好,并启动。

初始化

打开浏览器,访问 http://localhost:8080

按照提示,访问 /var/lib/jenkins/secrets/initialAdminPassword 文件:

[ding@192 ~]$ sudo cat /var/lib/jenkins/secrets/initialAdminPassword9cc784ffe4ad46e4bb2b8a1a4116839b

注:这个密码也是默认的 admin 用户的密码。

把文件内容复制到输入框,并点击“Continue”按钮。

对于新手,选择左边的默认的插件就行。点击后,开始安装:

大约需要几分钟时间,就安装好了,自动跳转到下一个页面:

填入用户名、密码、邮箱等信息,点击“Save and Continue”按钮,跳转到下一个页面:

默认的URL是 http://localhost:8080/ ,无需修改,直接点击“Save and Finish”按钮,进入下一个页面:

点击“Start using Jenkins”按钮,进入Jenkins主页面:

至此,Jenkins初始化完成,可以开始工作了。

Pipeline

参考 https://www.jenkins.io/doc/book/pipeline/getting-started

UI

在Jenkins主页面,点击左上角的“New Item”:

在接下来的页面中,填入名字,选择“Pipeline”,然后点击“OK”按钮:

在接下来的“Config”页面,到页面最下面:

可见,默认选择的是“Pipeline script”。在“Script”框里,填写pipeline代码。

如果不知道写什么好,可以在右边的下拉列表里,选择一个,比如“Hello World”:

生成的代码如下:

pipeline {agent anystages {stage('Hello') {steps {echo 'Hello World'}}

最后,点击“Save”按钮。至此,pipeline就创建好了。

接下来,点击左边的“Build Now”,运行 hello1 pipeline:

很快就可以看到,build运行成功了:

点击 #1 ,然后点击 “Console Output”,可以查看log:


可见,确实输出了 Hello World

SCM(Source Control Management)

前面的例子,代码是直接写在Jenkins里的,缺点是移植性和可维护性比较差。

更好的方法是把代码写在文本文件里,用源码控制工具来管理。该文件被称为 Jenkinsfile 。Jenkins用源码控制工具把 Jenkinsfile' pull下来,把它作为pipeline项目构建过程的一部分,然后来运行pipeline。

准备

首先要安装git。

然后在github创建一个项目 jenkinstest 。在项目根目录下,创建文件 Jenkinsfile 如下:

pipeline {agent any stages {stage('Stage 1') {steps {echo 'Hello world!' }}}}

注:在其它位置创建 Jenkinsfile 也行,需在Jenkins里指定其路径。

pipeline

回到Jenkins主页面,点击左上角“New Item”,仍然选择“Pipeline”,拉到页面最下面,这次选择 Pipeline script from SCM


Repository URL 处,填写git repo的URL,本例为 https://github.com/dukeding/jenkinstest

Credentials 处,选择git的credential。如果还没有配置,则点击下方的 Add 按钮添加。

本例中,使用的是用户名密码的方式:


添加完成后,就可以选择它了:

接下来,在 Branch 处选择git branch,默认值是 */master ,本例中为 */main

Script Path 处选择 Jenkinsfile 的路径,默认值是 Jenkinsfile ,本例中文件名和路径都无需修改(即项目根目录下的 Jenkinsfile 文件)。

最后,点击“Save”按钮。

现在就可以运行pipeline了。点击左上角的 Build Now ,很快就能看到运行成功了:

点击 #1 ,然后点击 Console Output 查看log:

Started by user dingObtained Jenkinsfile from git https://github.com/dukeding/jenkinstest[Pipeline] Start of Pipeline[Pipeline] nodeRunning on Jenkins in /var/lib/jenkins/workspace/hello2[Pipeline] {[Pipeline] stage[Pipeline] { (Declarative: Checkout SCM)[Pipeline] checkoutSelected Git installation does not exist. Using DefaultThe recommended git tool is: NONEusing credential duke_ding_gitCloning the remote Git repositoryCloning repository https://github.com/dukeding/jenkinstest > git init /var/lib/jenkins/workspace/hello2 # timeout=10Fetching upstream changes from https://github.com/dukeding/jenkinstest > git --version # timeout=10 > git --version # 'git version 2.39.3'using GIT_ASKPASS to set credentials> git fetch --tags --force --progress -- https://github.com/dukeding/jenkinstest +refs/heads/*:refs/remotes/origin/* # timeout=10 > git config remote.origin.url https://github.com/dukeding/jenkinstest # timeout=10 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10Avoid second fetch > git rev-parse refs/remotes/origin/main^{commit} # timeout=10Checking out Revision ff3347d08ac2eec7bed80862b80efe02bcf21b93 (refs/remotes/origin/main) > git config core.sparsecheckout # timeout=10 > git checkout -f ff3347d08ac2eec7bed80862b80efe02bcf21b93 # timeout=10Commit message: "Update Jenkinsfile"First time build. Skipping changelog.[Pipeline] }[Pipeline] // stage[Pipeline] withEnv[Pipeline] {[Pipeline] stage[Pipeline] { (Stage 1)[Pipeline] echoHello world![Pipeline] }[Pipeline] // stage[Pipeline] }[Pipeline] // withEnv[Pipeline] }[Pipeline] // node[Pipeline] End of PipelineFinished: SUCCESS

可见,确实输出了 Hello World

参考

  • https://www.jenkins.io/doc/book/installing/linux/#red-hat-centos
  • https://www.jenkins.io/doc/book/pipeline/getting-started
  • https://www.jenkins.io/zh/doc/book/getting-started/ (中文文档)