文章目录
Jenkins job构建配置构建freestyle jobjenkins pipeline job构建配置pipeline基础架构
Jenkins应用Jenkins Linux shell集成Jenkins参数集成Jenkins git集成Jenkins maven集成Jenkins ansible集成
Jenkins job构建配置
环境准备
配置Jenkins server本地gitlab DNS安装git client,curl工具依赖关闭系统git http.sslVerify安全认证添加Jenkins后台git client user与email添加Jenkins后台 git credential凭据
系统管理–》系统设置–》注册 gitlab client的user和email
凭据—》Stores scoped to Jenkins—》全局凭据—》添加凭据—》gitlab的用户和密码
构建freestyle job
创建一个freestyle project
编辑描述信息
参数配置,向任务传送参数的接口
源代码管理
build 配置
export PATH
="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
echo "hello"
echo "[INFO] print env variable"
echo "Current deployment envrionment is $deploy_env" >> test.properties
echo "THe build is $version" >> test.properties
echo "[INFO] Done..."
echo "[INFO] check test properties"
if [-s test.properties
]
then
cat test.properties
echo "[INFO] Done..."
else
echo "test.properties is empty"
fi
echo "[INFO] build finished..."
$version 、$deploy_env :是之前的参数化构建过程填写的参数
jenkins pipeline job构建配置
pipeline基础架构
所有代码包裹在pipeline{}层内,声明job为 pipeline语法格式stages{}层用来包含该pipeline所有stage子层,将pipeline管道分为若干个管道块,每个管道块都可以做一件事情,且彼此不受影响stage{}层用来包含具体我们需要编写任务的steps{}子层,stage后跟管道块名steps{}层用来添加我们具体需要调用的模块语句,例如ssh、echo等parameters配置参数,定义Jenkins选项参数 建议使用any
创建一个pipeline project
pipeline
{
agent
{node
{label
'master'}}
environment
{
PATH
="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
}
parameters
{
choice
(
choices:
'dev\nprod',
description:
'choose deploy environment',
name:
'deploy_env'
)
string
(name:
'version',defaultvale:
'1.0.0', description:
'build version')
}
stages
{
stage
("checkout test repo"){
steps
{
sh
'git config --global http.sslVerify false'
dir ("${env.WORKSPACE}"){
git branch:
'master',credentiasID:
"ac34fe61-e627-47b2-a396-32db431e91e7",url:
""
}
}
}
}
}
未完待续。。。
Jenkins应用
Jenkins Linux shell集成
Jenkins Linux shell集成,可以将Linux下的脚本迁移到这里进行日常维护管理
构建一个freestyle风格的shell任务,直接新建一个freestyle任务,然后如下
#!/bin/bash
user
=`whoami`
if [$user == 'deploy']
then
echo "hello,my name is $user"
else
echo "sorry, I am not $user"
fi
free -h
ip addr
cat /etc/system-release
df -h
py_cmd
=`which python`
$py_cmd --version
Jenkins参数集成
Jenkins参数集成,所有需要与Jenkins任务交互的数据,都会从这个接口中进入,通过传入不通的参数,实现Jenkins在不同环境,不同任务,不同版本,不同逻辑的执行效果
选项参数
文本参数
布尔值参数,默认值为true
密码参数
#!/bin/bash
echo "current deploy envrionment is $deploy_env"
echo "The build is $version"
echo "The password is $pass"
if $bool
then
echo "request id approved"
else
echo "request is rejected"
fi
Jenkins git集成
Jenkins git集成,将gitlab GitHub代码克隆到本地这是系统凭据不对造成的
Jenkins maven集成
使用Jenkins maven插件去将代码仓库克隆下来的java,Python源代码编译成事先配置好的war包或者jar包到本地
wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.2/binaries/apache-maven-3.6.2-bin.tar.gz
atr -zxvf apache-maven-3.6.2-bin.tar.gz -C /opt
cd apache-maven-3.6.2/
cd bin/
./mvn --version
填写好git克隆地址
添加java_home
Jenkins ansible集成
内嵌shell模块调用本地ansible命令工具从而实现Jenkins能够集成ansible工具进行远程服务器的部署管理功能
登录ansible
su - deploy
ssh root@test.example.com
exit
cd /home/deploy/
cat testservers
[testserver
]
test.example.com ansible_user
=root
source /home/deploy/.py3-a2.5-env/bin/activate
source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q
ansibel --version
新建一个ansible的自由风格任务构建选择执行shell
#!/bin/sh
set +x
source /home/deploy/.py3-a2.5-env/bin/activate
source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q
cd /home/deploy
ansibel --version
ansibel-playbook --version
cat testservers
ansible -i testservers testserver -m
command -a
"ip addr"
set -x