文章目錄
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 配置
#bin/bash
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
echo "hello"
# print env variable
echo "[INFO] print env variable"
echo "Current deployment envrionment is $deploy_env" >> test.properties
echo "THe build is $version" >> test.properties
echo "[INFO] Done..."
#check test.properties
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
#!groovy
pipeline{
agent {node {label 'master'}} #pipeline 在Jenkins 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 #進入deploy虛擬環境
ssh [email protected] #查看是否已做好祕鑰認證
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 ##在虛擬環境下加載ansible2.5版本
ansibel --version #驗證ansible加載效果
- 新建一個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 #驗證ansible加載效果
ansibel-playbook --version #驗證ansible加載效果
cat testservers
ansible -i testservers testserver -m command -a "ip addr" #測試遠程主機命名
set -x