配置jenkins打包Android程序的环境-CentOS篇

字数 702阅读 2081

系统版本为CentOS版本为6.8,使用Windows的应该比较简单,如果使用Mac的可以看我的这篇文章:配置jenkins打包Android程序的环境-Mac篇

1.添加Jenkins源

$ sudo wget -O /etc/yum.repos.d/jenkins.repo http://jenkins-ci.org/redhat/jenkins.repo
$ sudo rpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key

2.检查安装JDK(JDK 7 以上版本才支持Jenkins)

$ java --version
$ sudo yum install java-1.8.0 

如果以前的java版本低或者想用新的jdk,可以把他们卸载

yum remove java-1.6.0-openjdk
yum remove java-1.7.0-openjdk

设置jdk全局环境变量

我们可以用如下命令来轻松设置你安装JDK的环境变量:

export JAVA_HOME=/usr/java/jdk1.8.0_25/
export PATH=$PATH:$JAVA_HOME

现在,让我们用下面的命令来查看JDK的环境变量:
echo $JAVA_HOME
输出结果如下:

/usr/java/jdk1.8.0_25/

或者

echo $PATH

输出结果如下:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/java/jdk1.8.0_25/

3.安装Jenkins

$ sudo yum install jenkins

如果发现找不到jenkins包,需要更新一下yum源,

$ yum -y update

还是没有,则在yum源中搜索一下:

$ yum search jenkins     
$ sudo yum install jenkins

如果还是没有搜索到Jenkins,则添加Jenkins源

$ sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
$ sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
$ sudo yum install jenkins

如果出现没有授权key的提示导致无法安装,可以使用如下语句:

$ yum install jenkins --nogpgcheck

4.配置Jenkins目录

安装完成后,会有一下目录

  1. /usr/lib/jenkins/:jenkins安装目录,WAR包会放在这里。
$ cd /usr/lib/jenkins
$ ll -rw-r--r-- 1 root root 52891723 Feb 27 00:13 jenkins.war
  1. /etc/sysconfig/jenkins:

jenkins配置文件,“端口”,“JENKINS_HOME”等都可以在这里配置。内容如下,其中Jenkins的端口号和tomcat是一致的都是8080,有冲突的话可以自行更改端口号:

## Path:        Development/Jenkins
## Description: Configuration for the Jenkins continuous build server
## Type:        string
## Default:     "/var/lib/jenkins"
## ServiceRestart: jenkins
#
# Directory where Jenkins store its configuration and working
# files (checkouts, build reports, artifacts, ...).
#
JENKINS_HOME="/var/lib/jenkins"

## Type:        string
## Default:     ""
## ServiceRestart: jenkins
#
# Java executable to run Jenkins
# When left empty, we'll try to find the suitable Java.
#
JENKINS_JAVA_CMD=""

## Type:        string
## Default:     "jenkins"
## ServiceRestart: jenkins
#
# Unix user account that runs the Jenkins daemon
# Be careful when you change this, as you need to update
# permissions of $JENKINS_HOME and /var/log/jenkins.
#
JENKINS_USER="jenkins"

## Type:        string
## Default:     "-Djava.awt.headless=true"
## ServiceRestart: jenkins
#
# Options to pass to java when running Jenkins.
#
JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true"


## Type:        integer(0:65535)
## Default:     8080
## ServiceRestart: jenkins
#
# Port Jenkins is listening on.
# Set to -1 to disable
#
JENKINS_PORT="8080"

## Type:        integer(0:65535)
## Default:     8009
## ServiceRestart: jenkins
#
# Ajp13 Port Jenkins is listening on.
# Set to -1 to disable
#
JENKINS_AJP_PORT="8009"

## Type:        integer(1:9)
## Default:     5
## ServiceRestart: jenkins
#
# Debug level for logs -- the higher the value, the more verbose.
# 5 is INFO.
#
JENKINS_DEBUG_LEVEL="5"

## Type:        yesno
## Default:     no
## ServiceRestart: jenkins
#
# Whether to enable access logging or not.
#
JENKINS_ENABLE_ACCESS_LOG="no"

## Type:        integer
## Default:     100
## ServiceRestart: jenkins
#
# Maximum number of HTTP worker threads.
#
JENKINS_HANDLER_MAX="100"

## Type:        integer
## Default:     20
## ServiceRestart: jenkins
#
# Maximum number of idle HTTP worker threads.
#
JENKINS_HANDLER_IDLE="20"

## Type:        string
## Default:     ""
## ServiceRestart: jenkins
#
# Pass arbitrary arguments to Jenkins.
# Full option list: java -jar jenkins.war --help
#
JENKINS_ARGS=""
  1. /var/lib/jenkins/:默认的JENKINS_HOME。目录结构如下:
$ cd /var/lib/jenkins/
$ ls -lst
total 216
4 -rw-r--r--   1 jenkins jenkins    0 Mar 18 23:59 Fingerprint cleanup.log
4 -rw-r--r--   1 jenkins jenkins    0 Mar 18 18:54 Workspace clean-up.log
8 drwxr-xr-x 180 jenkins jenkins 4096 Mar 18 17:04 fingerprints
8 -rw-r--r--   1 jenkins jenkins 1636 Mar 18 16:41 config.xml
8 -rw-r--r--   1 jenkins jenkins  903 Mar 18 16:41 hudson.maven.MavenModuleSet.xml
8 -rw-r--r--   1 jenkins jenkins 1122 Mar 18 16:41 hudson.plugins.sonar.SonarPublisher.xml
8 -rw-r--r--   1 jenkins jenkins  205 Mar 18 16:41 hudson.plugins.sonar.SonarRunnerInstallation.xml
8 -rw-r--r--   1 jenkins jenkins  196 Mar 18 16:41 hudson.scm.CVSSCM.xml
8 -rw-r--r--   1 jenkins jenkins  692 Mar 18 16:41 hudson.scm.SubversionSCM.xml
8 -rw-r--r--   1 jenkins jenkins  145 Mar 18 16:41 hudson.tasks.Ant.xml
8 -rw-r--r--   1 jenkins jenkins  187 Mar 18 16:41 hudson.tasks.Mailer.xml
8 -rw-r--r--   1 jenkins jenkins  330 Mar 18 16:41 hudson.tasks.Maven.xml
8 -rw-r--r--   1 jenkins jenkins   76 Mar 18 16:41 hudson.tasks.Shell.xml
8 -rw-r--r--   1 jenkins jenkins  215 Mar 18 16:41 hudson.triggers.SCMTrigger.xml
8 -rw-r--r--   1 jenkins jenkins  264 Mar 18 16:41 jenkins.model.JenkinsLocationConfiguration.xml
8 -rw-r--r--   1 jenkins jenkins  907 Mar 18 16:29 nodeMonitors.xml
8 -rw-r--r--   1 jenkins jenkins  159 Mar 18 16:29 hudson.model.UpdateCenter.xml
8 -rw-r--r--   1 jenkins jenkins   46 Mar 18 16:29 queue.xml.bak
8 drwxr-xr-x   2 jenkins jenkins 4096 Mar 18 16:24 updates
4 -rw-r--r--   1 jenkins jenkins    0 Mar 16 10:27 Connection Activity monitoring to slaves.log
8 drwxr-xr-x   6 jenkins jenkins 4096 Mar 13 18:24 workspace
8 drwxr-xr-x  13 jenkins jenkins 4096 Mar 13 17:59 jobs
8 drwx------   2 jenkins jenkins 4096 Feb 28 11:16 secrets
8 -rw-r--r--   1 jenkins jenkins  165 Feb 27 13:29 sonar.xml
8 drwxr-xr-x  14 jenkins jenkins 4096 Feb 27 13:29 plugins
8 -rw-------   1 jenkins jenkins 1679 Feb 27 12:12 identity.key
8 drwxr-xr-x   2 jenkins jenkins 4096 Feb 27 12:12 userContent
8 -rw-r--r--   1 jenkins jenkins   64 Feb 27 12:12 secret.key
4 -rw-r--r--   1 jenkins jenkins    0 Feb 27 12:12 secret.key.not-so-secret
  1. /var/log/jenkins/jenkins.log:Jenkins日志文件。
    启动,重启,关闭Jenkins:
$ sudo service jenkins start
$ sudo service jenkins restart
$ sudo service jenkins shutdown

此时,有可能出现有可能出现错误:“Starting Jenkins -bash: /usr/bin/java: No such file or directory”。表示找不到Jenkins找不到java。
这时就需要“vi /etc/init.d/jenkins”,把java路径加上:

$ Search usable Java. We do this because various reports indicated
$ that /usr/bin/java may not always point to Java 1.5
$ see http://www.nabble.com/guinea-pigs-wanted-----Hudson-RPM-for-RedHat-Linux-td25673707.html
 for candidate in /etc/alternatives/java /usr/lib/jvm/java-1.6.0/bin/java /usr/lib/jvm/jre-1.6.0/bin/java /usr/lib/jvm/java-1 .5.0/bin/java /usr/lib/jvm/jre-1.5.0/bin/java /usr/bin/java /usr/j2sdk/bin/java
 do
 [ -x "$JENKINS_JAVA_CMD" ] && break
 JENKINS_JAVA_CMD="$candidate"
 done

此时,可在浏览器中打开Jenkins页面(默认端口8080):localhost:8080

  1. 修改Jenkins密码
    Jenkins密码可能忘记的话,用一下语句可以重置
$ sudo passwd jenkins

5.配置Jenkins打包所需的其他软件环境

1.安装Gradle

  1. http://www.gradle.org/downloads页面,下载最新版的gradle软件包
  2. 解压gradle
$ cd /usr.local
$ sudo unzip ~/gradle-2.2.1-all.zip
  1. 设置运行时环境
$ sudo vi /etc/profile

在文件中增加GRADLE_HOME输出路径,并且修订PATH设置如下

export GRADLE_HOME=/usr/local/gradle-2.2.1  
export PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin:${GRADLE_HOME}/bin:${JAVA_HOME}:${PATH}

在PATH语句后面追加${GRADLE_HOME}/bin,让系统能够找到可执行文件的路径。

如果你不希望所有用户都使用这个配置,那么可以单独为自己的账号配置gradle环境,在自己账号的HOME目录下:

$ vi .bash_profile

进行类似的编辑即可。

  1. 保存退出后,为了让修改即时生效,执行:
$source /etc/profile
  1. 查看gradle的版本信息
view plaincopy to clipboardprint?
$ gradle -version------------------------------------------------------------Gradle 2.2.1-----------------------------------------------------------
Build time:   2014-11-24 09:45:35 UTCBuild number: noneRevision:   6fcb59c06f43a4e6b1bcb401f7686a8601a1fb4aGroovy:       
2.3.6Ant:          Apache Ant(TM) version 1.9.3 compiled on 
December 23 2013JVM:          1.8.0_25 (Oracle Corporation
25.25-b02)OS:           Linux 2.6.32-431.11.2.el6.i686 i386  

安装git

yum install git

至此,Jenkins为Android打包环境已经配置完成了。

推荐阅读更多精彩内容