Jenkins

The leading open source automation server, Jenkins provides hundreds of plugins to support building, deploying and automating any project.

jenkins

jenkins zh_CN

blog系列教程 zh_CN

download->install: install->start:

Download

LTS (Long-Term Support) releases are chosen every 12 weeks from the stream of regular releases as the stable release for that time period.

Download the war, renamed ROOT.war and put it into

/Users/houbinbin/it/tools/tomcat/tomcat8/webapps
  • start tomcat
houbinbindeMacBook-Pro:bin houbinbin$ pwd
/Users/houbinbin/it/tools/tomcat/tomcat8/bin
houbinbindeMacBook-Pro:bin houbinbin$ ./startup.sh
  • visit
localhost:8080
  • uninstall

Use ./ to the command shell.

$   /Library/Application Support/Jenkins/Uninstall.command

result

houbinbindeMacBook-Pro:Jenkins houbinbin$ ./Uninstall.command


Jenkins uninstallation script

The following commands are executed using sudo, so you need to be logged
in as an administrator. Please provide your password when prompted.

+ sudo launchctl unload /Library/LaunchDaemons/org.jenkins-ci.plist
+ sudo rm /Library/LaunchDaemons/org.jenkins-ci.plist
+ sudo rm -rf /Applications/Jenkins '/Library/Application Support/Jenkins' /Library/Documentation/Jenkins
+ sudo rm -rf /Users/Shared/Jenkins
sudo: cannot get working directory
+ sudo rm -rf /var/log/jenkins
sudo: cannot get working directory
+ sudo rm -f /etc/newsyslog.d/jenkins.conf
sudo: cannot get working directory
+ sudo dscl . -delete /Users/jenkins
sudo: cannot get working directory
+ sudo dscl . -delete /Groups/jenkins
sudo: cannot get working directory
+ pkgutil --pkgs
+ grep 'org\.jenkins-ci\.'
+ xargs -n 1 sudo pkgutil --forget
sudo: cannot get working directory
Forgot package 'org.jenkins-ci.documentation.pkg' on '/'.
sudo: cannot get working directory
Forgot package 'org.jenkins-ci.jenkins.osx.pkg' on '/'.
sudo: cannot get working directory
Forgot package 'org.jenkins-ci.jenkins2191.postflight.pkg' on '/'.
sudo: cannot get working directory
Forgot package 'org.jenkins-ci.launchd-jenkins.pkg' on '/'.
sudo: cannot get working directory
Forgot package 'org.jenkins-ci.support.pkg' on '/'.
+ set +x

Jenkins has been uninstalled.

Get Start

  • Verify password

copy /Users/houbinbin/.jenkins/secrets/initialAdminPassword content into password input.

  • Install plugins

  • Create user

after this you can see:

jenkines

  • Default user config place:
/Users/houbinbin/.jenkins/users/${username}/config.xml

Config Jenkins

Config java,git,maven

  • Get need first

1、which can get the execute path

2、If you had config the path before, like $M3_HOME in ~/.bash_profile, you can use echo $M3_HOME to get the home path

3、For Java, we can use /usr/libexec/java_home to get it’s path

houbinbindeMacBook-Pro:bin houbinbin$ which mvn
/usr/local/maven/maven3.3.9/bin/mvn
houbinbindeMacBook-Pro:~ houbinbin$ echo $M3_HOME
/usr/local/maven/maven3.3.9
houbinbindeMacBook-Pro:bin houbinbin$ which git
/usr/local/bin/git
houbinbindeMacBook-Pro:bin houbinbin$ which java
/usr/bin/java
houbinbindeMacBook-Pro:~ houbinbin$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home
  • Set path

系统管理-》Global Tool Configuration or http://localhost:8080/configureTools/

1、add jdk

In mac:

JDK别名:      JDK8
JAVA_HOME:    /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home


In ubuntu:

$ which java
/home/hbb/tool/jdk/jdk1.8.0_112/bin/java

设置

JAVA_HOME:  /home/hbb/tool/jdk/jdk1.8.0_112

in this way, add git and mvn

2、add git

Name: git
Path to Git executable: /usr/local/bin/git

3、add mvn

name: maven3
MAVEN_HOME: /usr/local/maven/maven3.3.9

tools

Config Mail

mail

config mail

Create Job

  • Create

Enter an item name blog for example, select 构建一个自由风格的软件项目

  • Config

1、源码管理

Git

Repository URL: https://github.com/houbb/blog
Credentials:    usr/pwd

2、构建触发器 Choose Build when a change is pushed to GitHub and Poll SCM

Set Schedule for Poll SCM

*/5 * * * *

means re-flush each five minutes.

对于所有的任务日程表,jenkins采用cron-style语法,包含5个字段,字段之间使用空格隔开,格式如下

MINUTE HOUR DOM MANTH DOW
MINUTE:0-59    HOUR:0-23    DOM:1-31   MONTH:1-12   DOW:0-7 ,其中0,7都是表示周日 ,以下有几个简化字符

1、“*” 表示在该字段所有可能出现的值 2、也可以使用连字符“-”定义范围,如在DOW字段上定义1-5,表示周一至周五 3、使用正斜杠符号定义一个跳过一个范围,如在MINUTE字段上定义 */5 表示每五分钟的意思 4、使用逗号分隔一个字段表示一系列有效值,如HOUR 字段定义 12,18 表示12点与18点

3、选择【丢弃旧的构建】,这里主要是为节省磁盘空间而考虑的,因为每次构建后,jenkins都会在jobs目录下为每个job下载,和创建相关数据,如打包后的jar等

  • 保持构建的天数,这里我设置5,表示每个每次构建后的记录只有5天有效期
  • 保持构建的最大个数,每个job只会保留最新的5次构建记录,这里需要提醒的是,如果之前有一次构建成功,那么jenkins会永远保留,当然也可以手动删除

4、构建触发器

勾选一下选项:

  • Build whenever a SNAPSHOT dependency is built

  • Build periodically

H 7 * * 1-7
  • Build when a change is pushed to GitHub

5、构建

Invoke-top-level Maven Targets

Maven Version: maven3

Goals: clean site

参数化构建

blog

  1. Install Dynamic Parameter Plug-in in Jenkins

  2. 参数化构建过程->Dynamic Choice Parameter

  • Name values release_branch
  • Choices Script values:
def ver_keys = [ 'bash', '-c', 'cd /Users/houbinbin/.jenkins/workspace/framework; git pull>/dev/null; git branch -a|grep remotes|cut -d "/" -f3|sort -r |head -10 ' ]
ver_keys.execute().text.tokenize('\n')

命令简单解释:

到项目路径下,截取获取分支名称。

houbinbindeMacBook-Pro:framework houbinbin$ pwd
/Users/houbinbin/.jenkins/workspace/framework
houbinbindeMacBook-Pro:framework houbinbin$ git branch -a
* (HEAD detached at 00177f4)
  remotes/origin/master
  remotes/origin/release_1.0.1

可优化点:

(1) /Users/houbinbin/.jenkins/workspace/framework 打开的路径,就是jenkins自带的 WORKSPACE, 直接使用自测无效。愚蠢。。

(2) workspace 下项目必须在 jenkins 部署一次之后, 路径下才会保存。所以可以调整为本地存放代码的路径。

直接指定选项

参数化构建过程->Choice

选项指定,一行为一个选项。

  1. Branches to build -> Branch Specifier (blank for ‘any’) 内容指定为 $release_branch

Pipeline

Jenkins Pipeline is a suite of plugins which supports implementing and integrating continuous delivery pipelines into Jenkins.

To use Jenkins Pipeline, you will need:

  • Jenkins 2.x or later (older versions back to 1.642.3 may work but are not recommended)
  • Pipeline plugin

Defining a Pipeline in the Web UI

  1. Click New Item in Home Page

  2. Enter an item name like pipeline-example, choose Pipeline, and save.

  3. In the Script define field of Pipeline, add this and save

node {
    echo "hello world"
}
  1. Click Build Now to run.

  2. Click the #1 under the Build History, and Click the Console Output, you may see:

Started by user houbinbin
[Pipeline] node
Running on master in /Users/houbinbin/.jenkins/workspace/pipeline-example
[Pipeline] {
[Pipeline] echo
hello world
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Defining a Pipeline in SCM

Doc can find in localhost:8888/pipeline-syntax/

The Jenkins file

Create->Build: Build->Test:

Creating a Jenkinsfile

Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline and is checked into source control.

node {
    checkout scm

    /* .. snip .. */
    stage('Build') {
        sh 'make' // <1>
        archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true // <2>
    }

    /* .. snip .. */
    stage('Test') {
        /* `make check` returns non-zero on test failures,
         * using `true` to allow the Pipeline to continue nonetheless
         */
        sh 'make check || true' // <1>
        junit '**/target/*.xml' // <2>
    }

    /* .. snip .. */
    stage('Deploy') {
        if (currentBuild.result == 'SUCCESS') { // <1>
            sh 'make publish'
        }
    }

}

Examples