• Centos 8.2 本地部署 Jenkins


    关于Jenkins 部署上一篇是:minikube & helm 安装 jenkins

    1. 简介

    Jenkins 是一个 CI/CD 工具。这里CI是指持续集成,CD是指持续交付。Jenkins 也被认为是自动化工具或服务器,它有助于自动化与构建、测试和部署相关的软件开发。

    2. 准备条件

    • 系统:centos 8.2
    • cpu:4
    • 内存:8G
    • 硬盘:40G
    • ip: 192.168.10.90
    • hostname: jenkins_master

    3. 安装依赖工具

    1. 安装依赖
    sudo dnf -y install make autoconf automake cmake perl-CPAN libcurl-devel libtool gcc gcc-c++ glibc-headers zlib-devel git-lfs telnet lrzsz jq expat-devel openssl-devel
    
    • 1
    1. 安装 Git
    
    cd /tmp
    wget --no-check-certificate https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.38.1.tar.gz
    tar -xvzf git-2.38.1.tar.gz
    cd git-2.38.1/
    ./configure
    make
    sudo make install
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    $ git --version          # 输出 git 版本号,说明安装成功
    git version 2.38.1
    
    • 1
    • 2

    4. 配置 jenkins 源

    echo "192.168.10.90 jenkins_master " | sudo tee -a /etc/hosts
    
    • 1

    dnf 命令软件更新

    sudo dnf update -y
    sudo dnf repolist
    
    • 1
    • 2

    下载 jenkins 源

    sudo dnf install wget -y
    sudo wget http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo -O /etc/yum.repos.d/jenkins.repo
    
    • 1
    • 2

    运行以下 rpm 命令以导入 Jenkins 包的 GPG 密钥

    sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
    
    • 1

    5. 安装 java-17

    查询 java 版本

    dnf list java* --showduplicates  | sort -r
    Last metadata expiration check: 0:00:38 ago on Tue 06 Dec 2022 12:00:53 PM CST.
    javapackages-tools.noarch                    5.3.0-1.module_el8.0.0+11+5b8c10bd appstream
    javapackages-filesystem.noarch               5.3.0-1.module_el8.0.0+11+5b8c10bd appstream
    java-atk-wrapper.x86_64                      0.33.2-6.el8                       appstream
    java-1.8.0-openjdk.x86_64                    1:1.8.0.312.b07-2.el8_5            appstream
    java-1.8.0-openjdk.x86_64                    1:1.8.0.312.b07-1.el8_4            appstream
    java-1.8.0-openjdk-src.x86_64                1:1.8.0.312.b07-2.el8_5            appstream
    java-1.8.0-openjdk-src.x86_64                1:1.8.0.312.b07-1.el8_4            appstream
    java-1.8.0-openjdk-slowdebug.x86_64          1:1.8.0.312.b07-2.el8_5            appstream
    java-1.8.0-openjdk-slowdebug.x86_64          1:1.8.0.312.b07-1.el8_4            appstream
    java-1.8.0-openjdk-javadoc-zip.noarch        1:1.8.0.312.b07-2.el8_5            appstream
    java-1.8.0-openjdk-javadoc-zip.noarch        1:1.8.0.312.b07-1.el8_4            appstream
    java-1.8.0-openjdk-javadoc.noarch            1:1.8.0.312.b07-2.el8_5            appstream
    java-1.8.0-openjdk-javadoc.noarch            1:1.8.0.312.b07-1.el8_4            appstream
    java-1.8.0-openjdk-headless.x86_64           1:1.8.0.312.b07-2.el8_5            appstream
    java-1.8.0-openjdk-headless.x86_64           1:1.8.0.312.b07-1.el8_4            appstream
    java-1.8.0-openjdk-headless-slowdebug.x86_64 1:1.8.0.312.b07-2.el8_5            appstream
    java-1.8.0-openjdk-headless-slowdebug.x86_64 1:1.8.0.312.b07-1.el8_4            appstream
    java-1.8.0-openjdk-devel.x86_64              1:1.8.0.312.b07-2.el8_5            appstream
    java-1.8.0-openjdk-devel.x86_64              1:1.8.0.312.b07-1.el8_4            appstream
    java-1.8.0-openjdk-demo.x86_64               1:1.8.0.312.b07-2.el8_5            appstream
    java-1.8.0-openjdk-demo.x86_64               1:1.8.0.312.b07-1.el8_4            appstream
    java-1.8.0-openjdk-accessibility.x86_64      1:1.8.0.312.b07-2.el8_5            appstream
    java-1.8.0-openjdk-accessibility.x86_64      1:1.8.0.312.b07-1.el8_4            appstream
    java-17-openjdk.x86_64                       1:17.0.1.0.12-2.el8_5              appstream
    java-17-openjdk.x86_64                       1:17.0.0.0.35-4.el8                appstream
    java-17-openjdk-static-libs.x86_64           1:17.0.1.0.12-2.el8_5              appstream
    java-17-openjdk-static-libs.x86_64           1:17.0.0.0.35-4.el8                appstream
    java-17-openjdk-src.x86_64                   1:17.0.1.0.12-2.el8_5              appstream
    java-17-openjdk-src.x86_64                   1:17.0.0.0.35-4.el8                appstream
    java-17-openjdk-jmods.x86_64                 1:17.0.1.0.12-2.el8_5              appstream
    java-17-openjdk-jmods.x86_64                 1:17.0.0.0.35-4.el8                appstream
    java-17-openjdk-javadoc-zip.x86_64           1:17.0.1.0.12-2.el8_5              appstream
    java-17-openjdk-javadoc-zip.x86_64           1:17.0.0.0.35-4.el8                appstream
    java-17-openjdk-javadoc.x86_64               1:17.0.1.0.12-2.el8_5              appstream
    java-17-openjdk-javadoc.x86_64               1:17.0.0.0.35-4.el8                appstream
    java-17-openjdk-headless.x86_64              1:17.0.1.0.12-2.el8_5              appstream
    java-17-openjdk-headless.x86_64              1:17.0.0.0.35-4.el8                appstream
    java-17-openjdk-devel.x86_64                 1:17.0.1.0.12-2.el8_5              appstream
    java-17-openjdk-devel.x86_64                 1:17.0.0.0.35-4.el8                appstream
    java-17-openjdk-demo.x86_64                  1:17.0.1.0.12-2.el8_5              appstream
    java-17-openjdk-demo.x86_64                  1:17.0.0.0.35-4.el8                appstream
    java-11-openjdk.x86_64                       1:11.0.13.0.8-4.el8_5              appstream
    java-11-openjdk.x86_64                       1:11.0.13.0.8-3.el8_5              appstream
    java-11-openjdk.x86_64                       1:11.0.13.0.8-1.el8_4              appstream
    java-11-openjdk-static-libs.x86_64           1:11.0.13.0.8-4.el8_5              appstream
    java-11-openjdk-static-libs.x86_64           1:11.0.13.0.8-3.el8_5              appstream
    java-11-openjdk-static-libs.x86_64           1:11.0.13.0.8-1.el8_4              appstream
    java-11-openjdk-src.x86_64                   1:11.0.13.0.8-4.el8_5              appstream
    java-11-openjdk-src.x86_64                   1:11.0.13.0.8-3.el8_5              appstream
    java-11-openjdk-src.x86_64                   1:11.0.13.0.8-1.el8_4              appstream
    java-11-openjdk-jmods.x86_64                 1:11.0.13.0.8-4.el8_5              appstream
    java-11-openjdk-jmods.x86_64                 1:11.0.13.0.8-3.el8_5              appstream
    java-11-openjdk-jmods.x86_64                 1:11.0.13.0.8-1.el8_4              appstream
    java-11-openjdk-javadoc-zip.x86_64           1:11.0.13.0.8-4.el8_5              appstream
    java-11-openjdk-javadoc-zip.x86_64           1:11.0.13.0.8-3.el8_5              appstream
    java-11-openjdk-javadoc-zip.x86_64           1:11.0.13.0.8-1.el8_4              appstream
    java-11-openjdk-javadoc.x86_64               1:11.0.13.0.8-4.el8_5              appstream
    java-11-openjdk-javadoc.x86_64               1:11.0.13.0.8-3.el8_5              appstream
    java-11-openjdk-javadoc.x86_64               1:11.0.13.0.8-1.el8_4              appstream
    java-11-openjdk-headless.x86_64              1:11.0.13.0.8-4.el8_5              appstream
    java-11-openjdk-headless.x86_64              1:11.0.13.0.8-3.el8_5              appstream
    java-11-openjdk-headless.x86_64              1:11.0.13.0.8-1.el8_4              appstream
    java-11-openjdk-devel.x86_64                 1:11.0.13.0.8-4.el8_5              appstream
    java-11-openjdk-devel.x86_64                 1:11.0.13.0.8-3.el8_5              appstream
    java-11-openjdk-devel.x86_64                 1:11.0.13.0.8-1.el8_4              appstream
    java-11-openjdk-demo.x86_64                  1:11.0.13.0.8-4.el8_5              appstream
    java-11-openjdk-demo.x86_64                  1:11.0.13.0.8-3.el8_5              appstream
    java-11-openjdk-demo.x86_64                  1:11.0.13.0.8-1.el8_4              appstream
    Available Packages
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71

    自 Jenkins 2.357 和 LTS 2.361.1 以来,Jenkins 需要 Java 11 或 17。

    $ dnf list java-17-openjdk-devel --showduplicates  | sort -r
    Last metadata expiration check: 0:08:00 ago on Tue 06 Dec 2022 12:06:28 PM CST.
    java-17-openjdk-devel.x86_64           1:17.0.1.0.12-2.el8_5           appstream
    java-17-openjdk-devel.x86_64           1:17.0.0.0.35-4.el8             appstream
    
    • 1
    • 2
    • 3
    • 4

    Java 是 Jenkins 的必备条件之一,所以运行下面的 dnf 命令来安装 java

    sudo dnf install -y java-17-openjdk-devel
    
    • 1

    查看 java 版本

    $ java -version
    openjdk version "17.0.1" 2021-10-19 LTS
    OpenJDK Runtime Environment 21.9 (build 17.0.1+12-LTS)
    OpenJDK 64-Bit Server VM 21.9 (build 17.0.1+12-LTS, mixed mode, sharing)
    
    • 1
    • 2
    • 3
    • 4

    设置Java环境

    $ sudo vi /etc/profile.d/java.sh
    export JAVA_HOME=$(dirname $(dirname $(readlink $(readlink $(which javac)))))
    export PATH=$PATH:$JAVA_HOME/bin
    export JRE_HOME=/usr/lib/jvm/jre
    export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
    
    • 1
    • 2
    • 3
    • 4
    • 5

    生效

    source /etc/profile.d/java.sh
    
    • 1

    检查

    $ echo $JAVA_HOME
    /usr/lib/jvm/java-17-openjdk-17.0.1.0.12-2.el8_5.x86_64
    
    $ env |grep java
    JAVA_HOME=/usr/lib/jvm/java-17-openjdk-17.0.1.0.12-2.el8_5.x86_64
    CLASSPATH=.:/usr/lib/jvm/java-17-openjdk-17.0.1.0.12-2.el8_5.x86_64/jre/lib:/usr/lib/jvm/java-17-openjdk-17.0.1.0.12-2.el8_5.x86_64/lib:/usr/lib/jvm/java-17-openjdk-17.0.1.0.12-2.el8_5.x86_64/lib/tools.jar
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/lib/jvm/java-17-openjdk-17.0.1.0.12-2.el8_5.x86_64/bin
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    6. 安装 Jenkins

    查询最新 Jenkins 版本

    $ dnf list jenkins --showduplicates  | sort -r
    Last metadata expiration check: 0:16:30 ago on Tue 06 Dec 2022 12:06:28 PM CST.
    jenkins.noarch                        2.89.4-1.1                         jenkins
    jenkins.noarch                        2.89.3-1.1                         jenkins
    jenkins.noarch                        2.89.2-1.1                         jenkins
    jenkins.noarch                        2.89.1-1.1                         jenkins
    jenkins.noarch                        2.7.4-1.1                          jenkins
    jenkins.noarch                        2.73.3-1.1                         jenkins
    jenkins.noarch                        2.73.2-1.1                         jenkins
    ......
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    安装

    sudo dnf -y install jenkins
    
    
    • 1
    • 2

    查询默认安装版本

    $ rpm -q jenkins
    jenkins-2.375.1-1.1.noarch
    
    • 1
    • 2

    启动

    sudo systemctl enable jenkins && sudo systemctl start jenkins
    
    • 1
    $ sudo systemctl status jenkins
    ● jenkins.service - Jenkins Continuous Integration Server
       Loaded: loaded (/usr/lib/systemd/system/jenkins.service; enabled; vendor preset: disabled)
       Active: active (running) since Tue 2022-12-06 12:50:29 CST; 12s ago
     Main PID: 2516 (java)
        Tasks: 52 (limit: 49495)
       Memory: 1.7G
       CGroup: /system.slice/jenkins.service
               └─2516 /usr/bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=/var/cache/jenkins/>
    
    Dec 06 12:49:53 jenkins_master jenkins[2516]: Jenkins initial setup is required. An admin user has been created and a p>
    Dec 06 12:49:53 jenkins_master jenkins[2516]: Please use the following password to proceed to installation:
    Dec 06 12:49:53 jenkins_master jenkins[2516]: 92b6f311ba9b433e894b5242cd4ab23c
    Dec 06 12:49:53 jenkins_master jenkins[2516]: This may also be found at: /var/lib/jenkins/secrets/initialAdminPassword
    Dec 06 12:49:53 jenkins_master jenkins[2516]: *************************************************************
    Dec 06 12:50:29 jenkins_master jenkins[2516]: 2022-12-06 04:50:29.241+0000 [id=34]        INFO        jenkins.InitReact>
    Dec 06 12:50:29 jenkins_master jenkins[2516]: 2022-12-06 04:50:29.269+0000 [id=25]        INFO        hudson.lifecycle.>
    Dec 06 12:50:29 jenkins_master systemd[1]: Started Jenkins Continuous Integration Server.
    Dec 06 12:50:30 jenkins_master jenkins[2516]: 2022-12-06 04:50:30.302+0000 [id=53]        INFO        h.m.DownloadServi>
    Dec 06 12:50:30 jenkins_master jenkins[2516]: 2022-12-06 04:50:30.303+0000 [id=53]        INFO        hudson.util.Retri>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    如果 firewalld 启动:

    YOURPORT=8080
    PERM="--permanent"
    SERV="$PERM --service=jenkins"
    
    firewall-cmd $PERM --new-service=jenkins
    firewall-cmd $SERV --set-short="Jenkins ports"
    firewall-cmd $SERV --set-description="Jenkins port exceptions"
    firewall-cmd $SERV --add-port=$YOURPORT/tcp
    firewall-cmd $PERM --add-service=jenkins
    firewall-cmd --zone=public --add-service=http --permanent
    firewall-cmd --reload
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    或者关闭firewalld

    systemctl stop firewalld.service
    systemctl disable firewalld.service
    
    • 1
    • 2

    7. 登陆

    访问:`http://192.168.10.90:8080


    获取自动生成的密码

    $ sudo  cat /var/lib/jenkins/secrets/initialAdminPassword
    92b6f311ba9b433e894b5242cd4ab23c
    
    • 1
    • 2

    安装插件


    设置管理用户



    界面

    用户状态

    8. 安装插件

    8.1 kubernets 插件

    8.2 git 插件

    8.3 docker 插件


    安装完插件进行重启

    9. 创建 pipeline job


    9.1 加载本地 Jenkinsfile 构建

    如果SCM 选择 None

    保存后,点击build构建。
    console output:

    /var/lib/jenkins/workspace/hello@script/bc6d5aaac091397cf6a4e48610337eabdad7c6ec8ff38cf8699e4e8a0aaaa1c8/Jenkinsfile
    
    • 1

    我们尝试创建该文件,并编写一个jenkinsfile

    $ vim /var/lib/jenkins/workspace/hello@script/bc6d5aaac091397cf6a4e48610337eabdad7c6ec8ff38cf8699e4e8a0aaaa1c8/Jenkinsfile
    
    • 1
    pipeline {
      agent any
      stages {
        stage('hello') {
          steps {
            sh 'echo "Hello World"'
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    保存后再次构建

    9.2 git 构建

    仓库地址:https://github.com/Ghostwritten/jenkins-example-private-repo.git


    获取 github Token

    加载Credentials,选择好分支,确认 Script path,保存。

    点击构建,查看console output

    Started by user ghostwritten
    Obtained Jenkinsfile from git https://github.com/Ghostwritten/jenkins-example-private-repo.git
    [Pipeline] Start of Pipeline
    [Pipeline] node
    Running on Jenkins in /var/lib/jenkins/workspace/hello
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (Declarative: Checkout SCM)
    [Pipeline] checkout
    Selected Git installation does not exist. Using Default
    The recommended git tool is: NONE
    using credential github-token
    Cloning the remote Git repository
    Cloning repository https://github.com/Ghostwritten/jenkins-example-private-repo.git
     > git init /var/lib/jenkins/workspace/hello # timeout=10
    Fetching upstream changes from https://github.com/Ghostwritten/jenkins-example-private-repo.git
     > git --version # timeout=10
     > git --version # 'git version 2.38.1'
    using GIT_ASKPASS to set credentials github-token
     > git fetch --tags --force --progress -- https://github.com/Ghostwritten/jenkins-example-private-repo.git +refs/heads/*:refs/remotes/origin/* # timeout=10
     > git config remote.origin.url https://github.com/Ghostwritten/jenkins-example-private-repo.git # timeout=10
     > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
    Avoid second fetch
     > git rev-parse refs/remotes/origin/main^{commit} # timeout=10
    Checking out Revision edd6932bd314bd2ad81d7e8bc6a312f18a93431b (refs/remotes/origin/main)
     > git config core.sparsecheckout # timeout=10
     > git checkout -f edd6932bd314bd2ad81d7e8bc6a312f18a93431b # timeout=10
    Commit message: "add link"
    First time build. Skipping changelog.
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] withEnv
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (hello)
    [Pipeline] sh
    + echo 'Hello World'
    Hello World
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // withEnv
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    Finished: SUCCESS
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    10. 问题

    加载下载插件失败:

    尝试 systemctl restart jenkins

    参考:

  • 相关阅读:
    聚观早报 | 美团打车接入腾讯出行;腾讯音乐将在港交所上市
    手机端实现触摸拖拽效果
    成绩定级脚本(Python)
    【C++】自旋锁
    模型优化整体方向概述
    使用React和ResizeObserver实现自适应ECharts图表
    LeetCode Cookbook 字符串习题 上篇
    【STL常用容器】:string 容器
    java计算机毕业设计高校网上教材征订系统源程序+mysql+系统+lw文档+远程调试
    java:代理模式
  • 原文地址:https://blog.csdn.net/xixihahalelehehe/article/details/128200498