Jenkins 构建工具
一、集成 Maven
1.1 下载软件包
Maven 官方发布页下载 maven 包,国内下载推荐选择清华的镜像源
$ wget --no-check-certificate https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.8.3/binaries/apache-maven-3.8.3-bin.tar.gz
1.2 解压软件包
$ tar xf apache-maven-3.8.3-bin.tar.gz -C /usr/local/
1.3 配置环境变量
配置环境变量
$ vim /etc/profile
export M2_HOME=/usr/local/apache-maven-3.8.3
export PATH=$PATH:$M2_HOME/bin
刷新环境变量
$ . /etc/profile
1.4 确认安装情况
获取 maven 版本号,检测安装是否正常
$ mvn -v
Apache Maven 3.8.3 (ff8e977a158738155dc465c6a97ffaf31982d739)
Maven home: /usr/local/apache-maven-3.8.3
Java version: 11.0.13, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-11-openjdk-11.0.13.0.8-1.el7_9.x86_64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-957.21.3.el7.x86_64", arch: "amd64", family: "unix"
OK~
1.5 设置镜像仓库
修改配置,注释 maven-default-http-blocker
, <mirrors>
块中添加 <mirror>
块
$ vim /usr/local/apache-maven-3.8.3/conf/settings.xml
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
1.6 全局工具配置
路径:配置中心 → Global Tool Configuration → Maven

1.7 流水线使用
示例如下:
pipeline {
agent {
node { label "built-in" }
}
stages {
stage("maven-ci") {
steps {
script {
// 获取 全局工具配置中 Maven 变量 M2 所定义的路径,赋值给 mvnHome
mvnHome = tool "M2"
sh "${mvnHome}/bin/mvn -v"
}
}
}
}
}
执行效果

二、集成 Ant
2.1 下载软件包
国内下载推荐选择清华的镜像源
$ wget --no-check-certificate https://mirrors.tuna.tsinghua.edu.cn/apache/ant/binaries/apache-ant-1.10.12-bin.tar.gz
2.2 解压软件包
$ tar xf apache-ant-1.10.12-bin.tar.gz -C /usr/local
$ ls /usr/local/apache-ant-1.10.12
bin CONTRIBUTORS contributors.xml etc fetch.xml get-m2.xml INSTALL KEYS lib LICENSE manual NOTICE patch.xml README WHATSNEW
2.3 配置环境变量
配置 ANT_HOME
环境变量
$ vim "ANT" /etc/profile
export ANT_HOME=/usr/local/apache-ant-1.10.12
export PATH=$PATH:$ANT_HOME/bin
刷新环境变量
$ . /etc/profile
2.4 确认安装情况
获取 ant 版本号
$ ant -version
Apache Ant(TM) version 1.10.12 compiled on October 13 2021
2.5 全局工具配置
路径:配置中心 → Global Tool Configuration → Ant
如果 全局工具配置中没有 ant 配置项,则说明没有安装 Ant 插件

2.6 流水线使用
示例如下:
pipeline {
agent {
node { label "built-in" }
}
stages {
stage("ant-ci") {
steps {
script {
// 获取 全局工具配置中 Ant 变量 ANT 所定义的路径,赋值给 antHome
antHome = tool "ANT"
sh "${antHome}/bin/ant -version"
}
}
}
}
}
执行效果

三、集成 Gradle
3.1 下载软件包
$ wget https://services.gradle.org/distributions/gradle-7.3-bin.zip
3.2 解压软件包
$ unzip gradle-7.3-bin.zip -d /usr/local
$ ls /usr/local/gradle-7.3
bin init.d lib LICENSE NOTICE README
3.3 配置环境变量
配置 GRADLE_HOME
环境变量
$ vim /etc/profile
export GRADLE_HOME=/usr/local/gradle-7.3
export PATH=$PATH:$GRADLE_HOME/bin
刷新环境变量
$ . /etc/profile
3.4 确认安装情况
获取 gradle 版本
$ gradle -v
Welcome to Gradle 7.3!
Here are the highlights of this release:
- Easily declare new test suites in Java projects
- Support for Java 17
- Support for Scala 3
For more details see https://docs.gradle.org/7.3/release-notes.html
------------------------------------------------------------
Gradle 7.3
------------------------------------------------------------
Build time: 2021-11-09 20:40:36 UTC
Revision: 96754b8c44399658178a768ac764d727c2addb37
Kotlin: 1.5.31
Groovy: 3.0.9
Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM: 11.0.13 (Red Hat, Inc. 11.0.13+8-LTS)
OS: Linux 3.10.0-957.21.3.el7.x86_64 amd64
3.5 全局工具配置
配置中心 → Global Tool Configuration → Gradle
如果 全局工具配置中没有 Gradle 配置项,则说明没有安装 Gradle 插件

3.6 流水线使用
示例代码:
pipeline {
agent {
node { label "built-in" }
}
stages {
stage("gradle-ci") {
steps {
script {
// 获取 全局工具配置中 Gradle 变量 GRADLE 所定义的路径,赋值给 gradleHome
gradleHome = tool "GRADLE"
sh "${gradleHome}/bin/gradle -v"
}
}
}
}
}
执行效果

四、集成 NPM
4.1 下载软件包
用的是国内的清华源,可能会存在版本落后
$ wget --no-check-certificate https://mirrors.tuna.tsinghua.edu.cn/nodejs-release/v16.9.1/node-v16.9.1-linux-x64.tar.gz
4.2 解压软件包
$ tar xf node-v16.9.1-linux-x64.tar.gz -C /usr/local
$ mv /usr/local/node-v16.9.1-linux-x64 /usr/local/node-v16.9.1
$ ls /usr/local/node-v16.9.1
bin CHANGELOG.md include lib LICENSE README.md share
4.3 配置环境变量
配置 NODE_HOME
环境变量
$ vim /etc/profile
export NODE_HOME=/usr/local/node-v16.9.1
export PATH=$PATH:$NODE_HOME/bin
刷新环境变量
$ . /etc/profile
4.4 确认安装情况
获取 node 版本号
$ node -v
v16.9.1
4.5 配置镜像加速
为了避免默认通过国外站点下载依赖包,这里配置使用阿里的镜像源
$ npm config set registry=http://registry.npm.taobao.org # 默认 https://registry.npmjs.org/
$ npm config list
; "user" config from /root/.npmrc
registry = "http://registry.npm.taobao.org/"
; node bin location = /usr/local/node-v16.9.1/bin/node
; cwd = /tmp
; HOME = /root
; Run `npm config ls -l` to show all defaults.
4.5 全局工具配置
配置中心 → Global Tool Configuration → NodeJS
如果 全局工具配置中没有 NodeJS 配置项,则说明没有安装 NodeJS 插件

4.6 流水线使用
示例代码:
pipeline {
agent {
node { label "built-in" }
}
stages {
stage("nodejs-ci") {
steps {
script {
// 获取 全局工具配置中 NodeJS 变量 NODEJS 所定义的路径,赋值给 nodejsHome
nodejsHome = tool "NODEJS"
// 默认 pipeline 中 PATH 变量仅包含最基本的 bin path,我们在全局工具配置中添加的配置项不会注入 PATH 中
// 而 npm 命令依赖 node 命令,如果找不到则会抛出 /usr/bin/env: node: No such file or directory
// 所以,我们这里手动注入 nodejs path 至 环境变量 PATH
sh """
${nodejsHome}/bin/node -v
export PATH=${PATH}:${nodejsHome}/bin
npm -v
"""
}
}
}
}
}
执行效果

不过还有一种更简单的方法
pipeline {
agent {
node { label "built-in" }
}
tools {
// 关键字 "全局工具配置名称"
// tools 会自动将 "全局工具配置名称" 其 "安装目录" 下的 /bin/ 注入到环境变量 PATH 中
nodejs "NODEJS"
}
stages {
stage("nodejs-ci") {
steps {
script {
sh """
node -v
npm -v
echo $PATH
"""
}
}
}
}
}
执行效果

不过,美中不足的是它只支持特定的工具,如果想要更灵活方式,可以考虑这篇讨论中的两个方案 environment
或 withEnv
,如下所示:
// environment
environment {
GOPATH = "$WORKSPACE/gopath/bin"
PATH = "$GOPATH/bin:$PATH"
}
// withEnv 方式
stage ('STAGE NAME') {
withEnv(['PATH+EXTRA=/usr/sbin:/usr/bin:/sbin:/bin']) {
sh '//code block'
}
}
五、集成 Ansible
5.1 安装软件包
CentOS 7
安装 epel 源
$ yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
安装 ansible
$ yum -y install ansible
Ubuntu
安装 PPA 源
$ apt-get install software-properties-common
$ apt-add-repository ppa:ansible/ansible
$ apt-get update
安装 ansible
$ apt-get install ansible
5.2 确认安装情况
$ ansible --version
ansible 2.9.25
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Nov 16 2020, 22:23:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
5.3 公钥互信配置
首先,Ansible 是集中式的远程管理软件,它的一切远程操作都需要基于 SSH 协议,为了方便后续的操作,这里需要配置所有节点信任中控节点
中控节点:生成公私钥
$ ssh-keygen
Generating public/private rsa key pair.
# ...
拷贝 ~/.ssh/id_rsa.pub 公钥文件中的内容到主/被控节点(主控节点也纳入远程管理)的 ~/.ssh/authorized_keys 文件中
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAJqGFDjb6K9TmokM7qFnA2/y04Vj26h2w7z9T/lZN2k18h9i8xNZNTS59H0xMLhfV5YtOovMuFSwuGQRvD5eZn1qIm1yQOrAHjjtjRJrv/H93BgtRIwWTLpgcTBy4bbAGniGciHw4KJj20opNx/sKbE/f+XJgeNEmXfzM0+vuUkZ6j0xK7WjE/rBGRf9eeL/rJgi/FolVb1xnQLMLJ9a3PTKTcm1YSHeDRBnybZp4R2v2MInSX1gOzP5rL60sxH7z3UjIdGUK9SIuqZg65yIvDc7G7QD24WzNhLfyOq5DLRCtDqgec/i6tFBEL+TFjGvmyWCzkLsPx9XsaJJRculb root@sz-aliyun-ecs-1
被控节点:添加中控节点 SSH 公钥
$ cat ~/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAJqGFDjb6K9TmokM7qFnA2/y04Vj26h2w7z9T/lZN2k18h9i8xNZNTS59H0xMLhfV5YtOovMuFSwuGQRvD5eZn1qIm1yQOrAHjjtjRJrv/H93BgtRIwWTLpgcTBy4bbAGniGciHw4KJj20opNx/sKbE/f+XJgeNEmXfzM0+vuUkZ6j0xK7WjE/rBGRf9eeL/rJgi/FolVb1xnQLMLJ9a3PTKTcm1YSHeDRBnybZp4R2v2MInSX1gOzP5rL60sxH7z3UjIdGUK9SIuqZg65yIvDc7G7QD24WzNhLfyOq5DLRCtDqgec/i6tFBEL+TFjGvmyWCzkLsPx9XsaJJRculb root@sz-aliyun-ecs-1
5.4 配置节点清单
Ansible 可同时操作属于一个组的多台主机,组和主机之间的关系通过 inventory 文件配置,默认的文件路径为 /etc/ansible/hosts
主控节点
$ egrep -v "#|^$" /etc/ansible/hosts
# [<group_name>]
[ecs]
# server list
sz-aliyun-ecs-1
bj-huawei-hecs-1
# 这里定义多个组是为了后续做 Patterns 规则的匹配实验
[bj_server]
bj-huawei-hecs-1
[sz_server]
sz-aliyun-ecs-1
[prod]
bj-huawei-hecs-1
[dev]
sz-aliyun-ecs-1
[web_server]
bj-huawei-hecs-1
[db_server]
sz-aliyun-ecs-1
5.5 测试节点配置
执行命令:ansible all -m ping
$ ansible all -m ping
sz-aliyun-ecs-1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
bj-huawei-hecs-1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
OK,配置基本完成,测试也已经通过~
5.6 创建远程仓库
创建远程仓库跳过,网页点点就完事了
这里贴下用到的几个文件
Jenkinsfile
pipeline {
agent any
stages {
stage('ansible-demo') {
steps {
ansiblePlaybook(
playbook: "${env.WORKSPACE}/playbook.yml",
inventory: "${env.WORKSPACE}/hosts",
// 需要添加 SSH 用户名、私钥 凭证
// PS:Username 字段为 Ansible 执行时远程连接用户,这里需要注意下,配置不当会出现如下错误
// Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password)
credentialsId: "sz-aliyun-root-pk"
)
}
}
}
}
playbook.yml
- hosts: example
tasks:
- debug: msg="这是来自于 {{ lookup('env', 'BUILD_TAG') }} 的 Jenkins 构建!"
hosts
[example]
172.25.163.48
远程创建好以后,WebUI 上传文件,本地 git push,推荐后者
$ mkdir jenkins-ansible-demo
$ cd jenkins-ansible-demo
$ git init
添加本地文件
$ git add *
提交、推送到远程仓库
$ git commit -m "first commit"
$ git remote add origin git@gitee.com:l0tusch1ng/jenkins-ansible-demo.git
$ git push -u origin master
5.7 流水线使用
创建多分支流水线,配置好 远程仓库地址、Jenkinsfile、及访问凭证即可

执行效果
