程式碼塊:
stage('Clone') {
echo "1.Clone Stage"
git url: "https://gitee.com/xujk-27400861/springboot-dubbo.git"
script {
build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
}
echo "${build_tag}"
}
stage('執行構建') {
container('maven')
{
sh "mvn --version"
sh 'pwd'
sh '''cd provider/
mvn clean package -DskipTests'''
//sh 'mvn package'
echo '構建完成'
}
}
stage('Build') {
echo "3.Build Stage"
container('docker') {
sh "docker -v"
sh 'pwd'
sh """cd /home/jenkins/agent/workspace/linetest/provider/
ls
docker -H tcp://192.168.231.132:2375 build -t xjk27400861/springbootapp:${build_tag} ."""
}
}
stage('Push') {
echo "4.Push Docker Image Stage"
container('docker') {
sh "docker -H tcp://192.168.231.132:2375 login --username=xjk27400861 -p xujingkun@123"
sh "docker -H tcp://192.168.231.132:2375 push xjk27400861/springbootapp:${build_tag}"
}
}
輸入命令:
sudo gedit /lib/systemd/system/docker.service
找到ExecStart段,修改為
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock -H fd:// --containerd=/run/containerd/containerd.sock
儲存並退出編輯後,過載守護行程以及重啟Docker
sudo systemctl daemon-reload
sudo service docker restart
可通過執行命令檢視是否開放了遠端存取埠
sudo systemctl status docker.service
stage('Deploy') {
echo "5. Deploy Stage"
container('kubectl') {
sh "kubectl version"
def namespace="jenkinsdemo"//springboot2node
sh """cd /home/jenkins/agent/workspace/linetest/provider/
ls
sed -i 's/<BUILD_TAG>/${build_tag}/' k8s.yaml
sed -i 's/<BUILD_NAMESPACE>/${namespace}/' k8s.yaml
kubectl delete -f .
kubectl apply -f k8s.yaml --record
"""
}
}
def label = "jnlp"
podTemplate (label: label,cloud: 'kubernetes',containers: [
//image: 'datorresf/jenkins-agent-mvn',
//image: 'maven:latest',
containerTemplate(
name: 'maven',
image: 'maven:3.8.1-ibmjava-8',
alwaysPullImage: false,
ttyEnabled: true,
command:'cat'
),//docker:19.03.8
containerTemplate(
name: 'docker',
image: "docker:stable",
ttyEnabled: true,
command: 'cat',
),
containerTemplate(name: 'kubectl', image: 'bibinwilson/docker-kubectl:latest', command: 'cat', ttyEnabled: true)
],serviceAccount: 'jenkinsbuild',//jenkinsbuild
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
hostPathVolume(mountPath: '/root/.m2', hostPath: '/root/.m2'),
hostPathVolume(mountPath: '/home/jenkins/.kube', hostPath: '/root/.kube'),
hostPathVolume(mountPath: '/etc/kubernetes/pki', hostPath: '/etc/kubernetes/pki'),
])
{
node('jnlp') {
stage('Clone') {
echo "1.Clone Stage"
git credentialsId: '56cc9ad2-aafa-41f3-bc6b-2d7d66c52f32', url: "https://gitee.com/xujk-27400861/springboot-dubbo.git"
script {
build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
}
echo "${build_tag}"
}
stage('Test') {
echo "2.Test Stage"
}
stage('執行構建') {
container('maven')
{
sh "mvn --version"
sh 'pwd'
sh '''cd provider/
mvn clean package -DskipTests'''
//sh 'mvn package'
echo '構建完成'
}
}
stage('Build') {
echo "3.Build Stage"
container('docker') {
sh "docker -v"
sh 'pwd'
sh """cd /home/jenkins/agent/workspace/linetest/provider/
ls
docker -H tcp://192.168.231.132:2375 build -t xjk27400861/springbootapp:${build_tag} ."""
}
}
stage('Push') {
echo "4.Push Docker Image Stage"
container('docker') {
sh "docker -H tcp://192.168.231.132:2375 login --username=xjk27400861 -p xujingkun@123"
sh "docker -H tcp://192.168.231.132:2375 push xjk27400861/springbootapp:${build_tag}"
}
}
stage('Deploy') {
echo "5. Deploy Stage"
container('kubectl') {
sh "kubectl version"
def namespace="jenkinsdemo"//springboot2node
sh """cd /home/jenkins/agent/workspace/linetest/provider/
ls
sed -i 's/<BUILD_TAG>/${build_tag}/' k8s.yaml
sed -i 's/<BUILD_NAMESPACE>/${namespace}/' k8s.yaml
kubectl delete -f .
kubectl apply -f k8s.yaml --record
"""
}
}
}
}
Roles設定
編輯設定
RoleBindings無需修改,後面如果有需要其他使用者操作k8s的話,可以通過建立Role與RoleBindings,賦予其他賬戶對應的許可權來操作k8s環境
Manage Jenkins -> Manage nodes and clouds 依次點選,進入設定頁面
點選:Configure Clouds;名稱預設:kubernetes,我們的指令碼裡用cloud屬性指定的k8s叢集
由於我使用的是rancher通過應用市場安裝的,此處的kubernetes設定的都是預設值,無需修改
修改k8s叢集的地址(https://192.168.231.133:6443)
連線k8s叢集的認證方式,我這裡設定了3種,使用者名稱密碼&祕鑰文字&kube.config檔案
使用者名稱密碼方式,就直接輸入我們k8s叢集的存取使用者名稱和密碼,在jenkins建立全域性憑證,設定時選中即可
祕鑰文字方式,我們通過rancher,下載k8s的組態檔
開啟組態檔,把下圖圈起來的資料,在jenkins建立憑據,secret text型別
使用k8s的組態檔,建立secret file型別的憑證,上傳我們的組態檔,測試;由於檔案裡設定的是k8s叢集的存取域名,所以提示失敗,如果域名可通的話,也是可以的
好的,本篇到此為止