Fork me on GitHub
Suzf  Blog

Tag Jenkins

How-to use Jenkins and GitLab to build Continuous Integration

环境: Jenkins + GitLab + Maven + Tomcat

主要实现自动构建部署 Java Web应用。

工作流程:

提交代码到 GitLab --> Jenkins触发构建任务 --> Maven编译打包 --> Jenkins将war包部署到Tomcat。 安装 GitLab 安装 Jenkins 安装  Maven & JDK 安装 Plugins Publish over SSH

How-to enable Maven on jenkins

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.

Jenkins ver. 2.32.1

Step 1: Install JDK

官方站点 http://openjdk.java.net/install/

-- The openjdk-8-jre package contains just the Java Runtime Environment.

-- If you want to develop Java programs then please install the openjdk-8-jdk package.

# install jdk
sudo apt-get install openjdk-8-jdk -y
 
# make sure the jdk install success
java -version
openjdk version "1.8.0_111"
OpenJDK Runtime Environment (build 1.8.0_111-8u111-b14-2ubuntu0.16.04.2-b14)
OpenJDK 64-Bit Server VM (build 25.111-b14, mixed mode)
 
# now jdk installed, Let's add JAVA_HOME Env variable
cat > /etc/profile.d/java-8-openjdk.sh << EOF
JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
PATH=\$JAVA_HOME/bin:\$PATH
export JAVA_HOME PATH
EOF
 
source /etc/profile.d/java-8-openjdk.sh

Step 2: Downloading and Setting Up Maven

maven 的官方下载站点 Apache Maven Download. 在我写这篇文章的时候,Maven 的最新版本是 3.3.9.

cd /usr/local/src
wget http://mirror.its.dal.ca/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz
tar xf apache-maven-3.3.9-bin.tar.gz
mv apache-maven-3.3.9 /usr/local/


cat > /etc/profile.d/apache-maven.sh << EOF
MAVEN_HOME=/usr/local/apache-maven-3.3.9
export MAVEN_HOME
export PATH=\${PATH}:\${MAVEN_HOME}/bin
EOF
 
source /etc/profile.d/apache-maven.sh

# 查看 maven 版本
# mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T05:41:47-11:00)
Maven home: /usr/local/apache-maven-3.3.9
Java version: 1.8.0_111, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-57-generic", arch: "amd64", family: "unix"

Step 3: Setting up Jenkins  JDK and Maven

登陆 Jenkins dashboard,点击 系统管理  -->  Global Tool Configuration -- > JDK 安装 jenkins_jdk --> Maven安装 jenkins_maven 最后保存配置

Step 4: Install Maven Plugin

参见 How-to install jenkins plugin via manual installation

cd /var/lib/jenkins/plugins
wget http://updates.jenkins-ci.org/download/plugins/javadoc/1.4/javadoc.hpi
wget http://updates.jenkins-ci.org/download/plugins/maven-plugin/2.14/maven-plugin.hpi
/etc/init.d/jenkins  restart

Now You can create Maven Project

hello_jenkins_maven ~~~ EOF ~~~

How To Install Jenkins on Ubuntu 16.04

介绍

Jenkins是一个用Java编写的开源持续集成工具。在与Oracle发生争执后,项目从Hudson项目复刻。 Jenkins提供了软件开发的持续集成服务。它运行在Servlet容器中(例如Apache Tomcat)。它支持软件配置管理(SCM)工具(包括AccuRev SCMCVSSubversionGitPerforceClearcase和和RTC),可以执行基于Apache AntApache Maven的项目,以及任意的Shell脚本和Windows批处理命令。Jenkins的主要开发者是川口耕介。[2]Jenkins是在MIT许可证下发布的自由软件[3] 可以通过各种手段触发构建。例如提交给版本控制系统时被触发,也可以通过类似Cron的机制调度,也可以在其他的构建已经完成时,还可以通过一个特定的URL进行请求。

系统要求

Minimum Recommended Configuration: Java 7 256MB free memory 1GB+ free disk space Recommended Configuration for Small Team: Java 8 1GB+ free memory 50GB+ free disk space

安装

Debian/Ubuntu

On Debian-based distributions, such as Ubuntu, you can install Jenkins through apt.

Recent versions are available in an apt repository. Older but stable LTS versions are in this apt repository.

# Install Jenkins
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins

# Install OpenJDK
# -- http://openjdk.java.net/install/
# -- The openjdk-8-jre package contains just the Java Runtime Environment. 
# -- If you want to develop Java programs then please install the openjdk-8-jdk package.
sudo apt-get install openjdk-8-jre openjdk-8-jdk

# make sure the jdk install success
java -version
openjdk version "1.8.0_111"
OpenJDK Runtime Environment (build 1.8.0_111-8u111-b14-2ubuntu0.16.04.2-b14)
OpenJDK 64-Bit Server VM (build 25.111-b14, mixed mode)

# now jdk installed, Let's add JAVA_HOME Env variable
cat > /etc/profile.d/java-8-openjdk.sh < < EOF
JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
PATH=\$JAVA_HOME/bin:\$PATH
export JAVA_HOME PATH
EOF

source /etc/profile.d/java-8-openjdk.sh

This package installation will:

  • Setup Jenkins as a daemon launched on start. See /etc/init.d/jenkins for more details.
  • Create a jenkins user to run this service.
  • Direct console log output to the file /var/log/jenkins/jenkins.log. Check this file if you are troubleshooting Jenkins.
  • Populate /etc/default/jenkins with configuration parameters for the launch, e.g JENKINS_HOME
  • Set Jenkins to listen on port 8080. Access this port with your browser to start configuration.
  • INFO
  • If your /etc/init.d/jenkins file fails to start Jenkins, edit the /etc/default/jenkins to replace the line ----HTTP_PORT=8080---- with ----HTTP_PORT=8081---- Here, "8081" was chosen but you can put another port available.
安装后(安装向导)

Create Admin User and Password for Jenkins

Jenkins最初配置为在首次启动时是安全的。 如果没有用户名和密码,Jenkins将无法再访问,并且开放端口有限。 在初始运行Jenkins期间,将生成一个安全令牌并将其打印在控制台日志中:

*************************************************************

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

ae09a2a86xxx4b10952c70894d63xxxx

This may also be found at: /var/lib/jenkins/secrets/initialAdminPassword

*************************************************************

上面每个平台的安装说明包括您可以找到此日志输出的默认位置。 首次打开Jenkins UI时,必须在“安装向导”中输入此令牌。 如果您跳过安装向导中的用户创建步骤,则此令牌也将用作用户admin的默认密码。

初始插件安装

安装向导还将安装此Jenkins服务器的初始插件。 推荐的可用插件集是基于最常见的用例。 您可以在安装向导期间添加更多内容,或者根据需要稍后安装。 [0] https://jenkins.io/doc/book/getting-started/installing/ [1] https://zh.wikipedia.org/wiki/Jenkins_(%E8%BD%AF%E4%BB%B6)

Jenkins ver. 2.32.1

How-to configure a proxy for maven

前言

在国内使用Maven下载远程仓库jar包,速度真是不太理想,慢的真心无法忍受。所以,最好的解决办法是通过Maven设置代理(使用XX-Net翻墙)或者Maven使用代理

Maven设置代理

  1. 编辑 ${user.home}/.m2/settings.xml 文件,如果该目录下没有该文件,复制 $MAVEN_HOME/conf/setting.xml
  2. 找到<proxies>节点,设置代理信息
     <proxy>
       <id>xx-net</id>
       <active>true</active>
       <protocol>http</protocol>
       <host>127.0.0.1</host>
       <port>8087</port>
     </proxy>
  3. 完成上述配置,仍然还是会有问题
    [ERROR] Failed to execute goal on project xiaov: Could not resolve dependencies for project org.b3log:xiaov:war:2.2.0: Failed to collect dependencies at org.b3log:latke:jar:2.3.5 -> com.alibaba:druid:jar:1.0.16: Failed to read artifact descriptor for com.alibaba:druid:jar:1.0.16: Could not transfer artifact com.alibaba:druid:pom:1.0.16 from/to central (https://repo.maven.apache.org/maven2): sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target -> [Help 1]
    原因就是Maven走的是https,使用代理连接才会出现上述SSL认证问题。 更多解决办法:点击查看推荐使用下面的解决办法
  4. 最简洁的Maven配置请将下述配置信息覆盖settings.xml
    <?xml version="1.0" encoding="UTF-8"?>
     <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
       <proxies>
         <proxy>
           <id>xx-net</id>
           <active>true</active>
           <protocol>http</protocol>
           <host>127.0.0.1</host>
           <port>8087</port>
         </proxy>
       </proxies>
       <profiles>
         <profile>
           <id>securecentral</id>
           <repositories>
             <repository>
               <id>central</id>
               <url>http://repo1.maven.org/maven2</url>
               <releases>
                 <enabled>true</enabled>
               </releases>
             </repository>
           </repositories>
           <pluginRepositories>
             <pluginRepository>
               <id>central</id>
               <url>http://repo1.maven.org/maven2</url>
               <releases>
                 <enabled>true</enabled>
               </releases>
             </pluginRepository>
           </pluginRepositories>
         </profile>
       </profiles>
       <activeProfiles>
         <activeProfile>securecentral</activeProfile>
       </activeProfiles>
     </settings>

Maven使用镜像

这里推荐使用阿里Maven仓库镜像(Nexus仓库),希望能稳定存活。 修改Maven的settings.xml文件,配置mirrors的子节点,添加如下mirror:

<mirror>
    <id>nexus-aliyun</id>
    <mirrorOf>*</mirrorOf>
    <name>Nexus aliyun</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
上文来源 Abinnz's blog

以下是我个人 Jenkins Maven proxy 设置

:/usr/local/apache-maven-3.3.9# diff  -ruN conf/settings.xml.old  conf/settings.xml 
--- conf/settings.xml.old	2017-02-21 16:59:07.996000000 +0800
+++ conf/settings.xml	2017-02-21 17:01:15.496000000 +0800
@@ -88,6 +88,10 @@
    | specification in this list marked as active will be used.
    |-->
   <proxies>
+    <proxy>
+      <host>172.16.9.11</host>
+      <port>3128</port>
+    </proxy>
     <!-- proxy
      | Specification for one proxy, to be used in connecting to the network.
      |

[0] http://maven.apache.org/guides/mini/guide-proxies.html