Windows和Linux上安装Jenkins
Table of Contents

Jenkins系统学习,首先就是如何在你的服务器上安装Jenkins,网上列举很很多种方式,我这里主要介绍两种常用方式。第一种是在Linux服务器上,通过命令行的方式启动Jenkins.war包,第二种方式是在Windows上,把Jenkins提升为服务模式。

linux上安装Jenkins

下载Jenkins

进入Jenkins官方下载站点 https://jenkins.io/download/
$ wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war

启动Jenkins

通过Apache Tomcat的方式启动Jenkins。将Jenkins.war包移到$TOMCAT_HOME/webapps目录下,重启Tomcat,然后访问 http://localhost:8080/jenkins/

$ mkdir -p /var/log/jenkins/ /var/cache/jenkins/ /root/jenkins/
$ sudo nohup /etc/alternatives/java -Djava.awt.headless=true -DJENKINS_HOME=/root/jenkins -jar /root/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --httpPort=8082 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20 &

上面通过java命令来启动的好处:Jenkins运行和Tomcat服务器进行分离,另外可以在启动的时候自定义端口号和日志输出路径等。

解锁jenkins
cat /root/jenkins/secrets/initialAdminPassword

注意通过root用户进行创建Jenkins环境。
在SSH Publishers插件中,设置source files的时候,参考的主目录是该jobs创建的主目录。一般的格式是:$JENKINS_HOME/workspace/$view_name/


Windows上安装Jenkins

本地localhost jenkins启动

$ nohup java -Djava.awt.headless=true -DJENKINS_HOME=/jenkins -jar  /jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log  --webroot=/var/cache/jenkins/war --httpPort=8088 --debug=5  --handlerCountMax=100 --handlerCountMaxIdle=20 &

将Jenkins提升为windows服务

只要你成功运行了Jenkins,就可以连接到服务器,然后进入Jenkins的管理界面。在这里你会发现有一个按钮是Install as Windows Service。这将在服务器上创建一个Jenkins服务,它会自动且有条理地开始和停止Jenkins。

在JENKINS_HOME目录下,jenkins.xml文件配置在提升Jenkins为系统服务后,会自动生成。

<service>
  <id>jenkins</id>
  <name>Jenkins</name>
  <description>This service runs Jenkins automation server.</description>
  <env name="JENKINS_HOME" value="%BASE%"/>
  <!--
    if you'd like to run Jenkins with a specific version of Java, specify a full path to java.exe.
    The following value assumes that you have java in your PATH.
  -->
  <executable>java</executable>
  <arguments>-Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -jar "%BASE%\jenkins.war" --httpPort=8088 --webroot="%BASE%\war"</arguments>
  <!--
    interactive flag causes the empty black Java window to be displayed.
    I'm still debugging this.
  <interactive />
  -->
  <logmode>rotate</logmode>

  <onfailure action="restart" />

  <!--
    In the case WinSW gets terminated and leaks the process, we want to abort
    these runaway JAR processes on startup to prevent corruption of JENKINS_HOME.
    So this extension is enabled by default.
  -->
  <extensions>
    <!-- This is a sample configuration for the RunawayProcessKiller extension. -->
    <extension enabled="true"
               className="winsw.Plugins.RunawayProcessKiller.RunawayProcessKillerExtension"
               id="killOnStartup">
      <pidfile>%BASE%\jenkins.pid</pidfile>
      <stopTimeout>10000</stopTimeout>
      <stopParentFirst>false</stopParentFirst>
    </extension>
  </extensions>

  <!-- See the referenced examples for more options -->

</service>

解锁jenkins
cat /root/jenkins/secrets/initialAdminPassword

注意通过root用户进行创建Jenkins环境。
在SSH Publishers插件中,设置source files的时候,参考的主目录是该jobs创建的主目录。一般的格式是:$JENKINS_HOME/workspace/$view_name/


附加

JDK准备,与本地安装。

$ yum install java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64
$ yum install java-1.8.0-openjdk-devel-1.8.0.191.b12-0.el6_10.x86_64

查看命令

[root@longsl jvm]# rpm -qa | grep java
java-1.8.0-openjdk-1.8.0.191.b12-0.el6_10.x86_64
java-1.8.0-openjdk-headless-1.8.0.191.b12-0.el6_10.x86_64
java-1.8.0-openjdk-devel-1.8.0.191.b12-0.el6_10.x86_64

Maven准备

$ wget http://mirrors.sonic.net/apache/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz
# Ensure JAVA_HOME environment variable is set and points to your JDK installation
$ vim /etc/profile
export JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el6_10.x86_64"
export PATH="$JAVA_HOME/bin:$PATH"

export MAVEN_HOME="/opt/maven-3.6"
export PATH="$MAVEN_HOME/bin:$PATH"

$ tar xzvf apache-maven-3.6.0-bin.tar.gz

查看命令

[root@longsl maven-3.6]# mvn --version
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T02:41:47+08:00)
Maven home: /opt/maven-3.6
Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el6_10.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.16.12-1.el6.elrepo.x86_64", arch: "amd64", family: "unix"

常见问题

一、修改Jenkins登录用户的密码
解决方法:进入Jenkins的用户目录,在用户主目录的$USER_HOME/.jenkins/users/目录下,每个文件夹代表一个用户,其中存在config.xml配置文件,在其中修改加密,代表111111

<hudson.security.HudsonPrivateSecurityRealm_-Details>
<passwordHash>#jbcrypt:$2a$10$DdaWzN64JgUtLdvxWIflcuQu2fgrrMSAMabF5TSrGK5nXitqK9ZMS</passwordHash>
</hudson.security.HudsonPrivateSecurityRealm_-Details>

二、在通过POST请求的方式调用Jenkins请求时,出现:错误403请求中未包含有效的crumb(Error 403 No valid crumb was included in the request)
解决方法:Jenkins --> 全局安全配置 --> CSRF Protection-->关闭 防止跨站点请求伪造(默认是开启状态)