使用yum安装Jenkins

一、安装之前

  1. 1.安装Jenkins所依赖的jdk
  2. yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel

二、安装

  1. 2.安装方法1
  2. sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
  3. sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
  4. yum install -y jenkins
  5. #安装方法2
  6. wget https://pkg.jenkins.io/redhat/jenkins-2.156-1.1.noarch.rpm
  7. rpm -ivh jenkins-2.156-1.1.noarch.rpm

三、创建文件与授权

  1. mkdir -p /var/lib/jenkins
  2. mkdir -p /var/cache/jenkins
  3. mkdir -p /var/log/jenkins
  4. chown -R root:root /var/lib/jenkins
  5. chown -R root:root /var/cache/jenkins
  6. chown -R root:root /var/log/jenkins

四、设置调整防火墙

FirewallD是Centos 8上的默认防火墙解决方案
在安装过程中,Nginx使用预定义的规则创建防火墙服务文件,以允许访问HTTP(80)和 HTTPS(443) 端口

  1. 3.使用以下命令永久打开必要的端口
  2. systemctl start firewalld
  3. sudo firewall-cmd --permanent --zone=public --add-service=http
  4. sudo firewall-cmd --permanent --zone=public --add-service=https
  5. firewall-cmd --zone=public --add-port=7001/tcp --permanent
  6. firewall-cmd --zone=public --add-port=8080/tcp --permanent
  7. firewall-cmd --zone=public --add-port=8888/tcp --permanent
  8. firewall-cmd --zone=public --add-port=27017/tcp --permanent
  9. sudo firewall-cmd --reload
  10. systemctl restart firewalld

五、设置jenkins配置

输入命令 vim /etc/sysconfig/jenkins 进入编辑模式 ggdG 全删 ggyG 全部复制 ggvG ggVG 高亮显示

  1. ## Path: Development/Jenkins
  2. ## Description: Jenkins Automation Server
  3. ## Type: string
  4. ## Default: "/var/lib/jenkins"
  5. ## ServiceRestart: jenkins
  6. #
  7. # Directory where Jenkins store its configuration and working
  8. # files (checkouts, build reports, artifacts, ...).
  9. #
  10. JENKINS_HOME="/var/lib/jenkins"
  11. ## Type: string
  12. ## Default: "jenkins"
  13. ## ServiceRestart: jenkins
  14. ## 修改用户
  15. # Unix user account that runs the Jenkins daemon
  16. # Be careful when you change this, as you need to update
  17. # permissions of $JENKINS_HOME and /var/log/jenkins.
  18. #
  19. JENKINS_USER="root"
  20. ## Type: integer(0:65535)
  21. ## Default: 8080
  22. ## ServiceRestart: jenkins
  23. ## 监听端口
  24. # Port Jenkins is listening on.
  25. # Set to -1 to disable
  26. #
  27. JENKINS_PORT="8888"
  28. ## Type: string
  29. ## Default: ""
  30. ## ServiceRestart: jenkins
  31. #
  32. # Java executable to run Jenkins
  33. # When left empty, we'll try to find the suitable Java.
  34. #
  35. JENKINS_JAVA_CMD=""
  36. ## Type: string
  37. ## Default: "false"
  38. ## ServiceRestart: jenkins
  39. #
  40. # Whether to skip potentially long-running chown at the
  41. # $JENKINS_HOME location. Do not enable this, "true", unless
  42. # you know what you're doing. See JENKINS-23273.
  43. #
  44. #JENKINS_INSTALL_SKIP_CHOWN="false"
  45. ## Type: string
  46. ## Default: "-Djava.awt.headless=true"
  47. ## ServiceRestart: jenkins
  48. #
  49. # Options to pass to java when running Jenkins.
  50. #
  51. JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true"
  52. ## Type: string
  53. ## Default: ""
  54. ## ServiceRestart: jenkins
  55. #
  56. # IP address Jenkins listens on for HTTP requests.
  57. # Default is all interfaces (0.0.0.0).
  58. #
  59. JENKINS_LISTEN_ADDRESS=""
  60. ## Type: integer(0:65535)
  61. ## Default: ""
  62. ## ServiceRestart: jenkins
  63. #
  64. # HTTPS port Jenkins is listening on.
  65. # Default is disabled.
  66. #
  67. JENKINS_HTTPS_PORT=""
  68. ## Type: string
  69. ## Default: ""
  70. ## ServiceRestart: jenkins
  71. #
  72. # Path to the keystore in JKS format (as created by the JDK 'keytool').
  73. # Default is disabled.
  74. #
  75. JENKINS_HTTPS_KEYSTORE=""
  76. ## Type: string
  77. ## Default: ""
  78. ## ServiceRestart: jenkins
  79. #
  80. # Password to access the keystore defined in JENKINS_HTTPS_KEYSTORE.
  81. # Default is disabled.
  82. #
  83. JENKINS_HTTPS_KEYSTORE_PASSWORD=""
  84. ## Type: string
  85. ## Default: ""
  86. ## ServiceRestart: jenkins
  87. #
  88. # IP address Jenkins listens on for HTTPS requests.
  89. # Default is disabled.
  90. #
  91. JENKINS_HTTPS_LISTEN_ADDRESS=""
  92. ## Type: integer(0:65535)
  93. ## Default: ""
  94. ## ServiceRestart: jenkins
  95. #
  96. # HTTP2 port Jenkins is listening on.
  97. # Default is disabled.
  98. #
  99. # Notice: HTTP2 support may require additional configuration, see Winstone
  100. # documentation for more information.
  101. #
  102. JENKINS_HTTP2_PORT=""
  103. ## Type: string
  104. ## Default: ""
  105. ## ServiceRestart: jenkins
  106. #
  107. # IP address Jenkins listens on for HTTP2 requests.
  108. # Default is disabled.
  109. #
  110. # Notice: HTTP2 support may require additional configuration, see Winstone
  111. # documentation for more information.
  112. #
  113. JENKINS_HTTP2_LISTEN_ADDRESS=""
  114. ## Type: integer(1:9)
  115. ## Default: 5
  116. ## ServiceRestart: jenkins
  117. #
  118. # Debug level for logs -- the higher the value, the more verbose.
  119. # 5 is INFO.
  120. #
  121. JENKINS_DEBUG_LEVEL="5"
  122. ## Type: yesno
  123. ## Default: no
  124. ## ServiceRestart: jenkins
  125. #
  126. # Whether to enable access logging or not.
  127. #
  128. JENKINS_ENABLE_ACCESS_LOG="no"
  129. ## Type: integer
  130. ## Default: 100
  131. ## ServiceRestart: jenkins
  132. #
  133. # Maximum number of HTTP worker threads.
  134. #
  135. JENKINS_HANDLER_MAX="100"
  136. ## Type: integer
  137. ## Default: 20
  138. ## ServiceRestart: jenkins
  139. #
  140. # Maximum number of idle HTTP worker threads.
  141. #
  142. JENKINS_HANDLER_IDLE="20"
  143. ## Type: string
  144. ## Default: ""
  145. ## ServiceRestart: jenkins
  146. #
  147. # Folder for additional jar files to add to the Jetty class loader.
  148. # See Winstone documentation for more information.
  149. # Default is disabled.
  150. #
  151. JENKINS_EXTRA_LIB_FOLDER=""
  152. ## Type: string
  153. ## Default: ""
  154. ## ServiceRestart: jenkins
  155. #
  156. # Pass arbitrary arguments to Jenkins.
  157. # Full option list: java -jar jenkins.war --help
  158. #
  159. JENKINS_ARGS=""

六、启动

  1. 6.设置启动与开机启动
  2. sudo systemctl start jenkins && sudo systemctl enable jenkins
  3. #或 sudo service jenkins start && chkconfig jenkins on
  4. #启动
  5. sudo systemctl start jenkins
  6. #或 service jenkins start
  7. #重启
  8. sudo systemctl restart jenkins
  9. #或 service jenkins restart
  10. #开机启动
  11. sudo systemctl enable jenkins
  12. #或 sudo service jenkins star
  13. #关闭开机启动
  14. sudo systemctl disable jenkins
  15. #或 sudo chkconfig jenkins off

快速部署Jenkins

  1. yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
  2. sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
  3. sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
  4. yum install -y jenkins
  5. mkdir -p /var/lib/jenkins
  6. mkdir -p /var/cache/jenkins
  7. mkdir -p /var/log/jenkins
  8. chown -R root:root /var/lib/jenkins
  9. chown -R root:root /var/cache/jenkins
  10. chown -R root:root /var/log/jenkins
  11. systemctl start firewalld
  12. sudo firewall-cmd --permanent --zone=public --add-service=http
  13. sudo firewall-cmd --permanent --zone=public --add-service=https
  14. firewall-cmd --zone=public --add-port=7001/tcp --permanent
  15. firewall-cmd --zone=public --add-port=8080/tcp --permanent
  16. firewall-cmd --zone=public --add-port=8888/tcp --permanent
  17. firewall-cmd --zone=public --add-port=27017/tcp --permanent
  18. sudo firewall-cmd --reload
  19. systemctl restart firewalld

点击定位配置jenkins文件的位置

查看

  1. #执行命令查看密码
  2. cat /var/lib/jenkins/secrets/initialAdminPassword
  3. #检查是否已安装JDK,以下命令二选一,中括号选一即可
  4. yum list installed | grep [java][jdk]
  5. rpm -qa | grep [java][jdk][gcj]
  6. #查看centos系统版本
  7. cat /etc/redhat-release
  8. #查看部署的库主目录
  9. ls /var/lib/jenkins/

修改密码

1. 查找jenkins的路径

  1. #方法1:
  2. [root@iZedbi0f3jvy9lZ ~]# whereis jenkins
  3. jenkins: /usr/lib/jenkins
  4. #方法2:
  5. [root@iZedbi0f3jvy9lZ ~]# find / -name config.xml
  6. /var/lib/jenkins/jobs/gitee test/config.xml
  7. /var/lib/jenkins/users/kim_1794366939214431569/config.xml
  8. /var/lib/jenkins/config.xml

2. 修改目录

  1. #方法1:
  2. 修改config.xml文件
  3. 找到关键词 useSecurity, 把后面的值 true 改为false
  4. 可以无账号和密码登录Jenkins
  5. #方法2:
  6. 打开该配置文件找到<passwordHash>节点
  7. 将#jbcrypt:后面的内容替换成
  8. $2a$10$d6oDwJRKYlWQMOKhW/Mi0eNq4xs/utxvfHLeWBUeeXbQLN9vpNhuK
  9. 即可

上面那串如何生成的呢?
Jenkins采用Java加密工具jBCrypt,所以我们在生成密码的时候需要添加相关的jar包。

  1. <dependency>
  2. <groupId>org.mindrot</groupId>
  3. <artifactId>jbcrypt</artifactId>
  4. <version>0.4</version>
  5. </dependency>
  1. public class Test {
  2. public static void main(String[] args) {
  3. // 加密
  4. String admin = BCrypt.hashpw("admin", BCrypt.gensalt());
  5. System.out.println("加密:" + admin);
  6. // 解密
  7. if (BCrypt.checkpw("admin", admin)) {
  8. System.out.println("is match");
  9. } else {
  10. System.out.println("is not match");
  11. }
  12. }
  13. }

3. 重启 Jenkins

  1. /etc/init.d/jenkins restart
  2. sudo systemctl restart jenkins

插件

Git Parameter

Extended Choice Parameter

  1. #覆盖内容
  2. echo "abc==master,develop,test" > /opt/jenkins.properties
  3. #追加内容
  4. echo "abc=master,develop,test" >> /opt/jenkins.properties
  5. #cat 用户保存下的路径/jenkins.property > /opt/jenkins.property

错误解决

错误信息为Starting Jenkins bash: /usr/bin/java: No such file or directory是java环境配置的问题

  1. yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
  2. #或者如下命令,安装jdk1.8.0的所有文件
  3. #yum install -y java-1.8.0-openjdk*
  4. #ln -s /usr/local/java/jdk1.8.0_171/bin/java /usr/bin/java