环境: centos7,vm ip 192.168.116.6,docker-ce 18.09.6,Jenkins 2.263.1
1、部署 jenkins
mkdir jenkins_home
docker run -d -v jenkins_home:/var/jenkins_home -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts
如果出现日志报错:
touch: cannot touch ‘/var/jenkins_home/copy_reference_file.log’: Permission denied
Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
请修改 jenkins_home 目录权限,参考
artible2
chown -R 1000:1000 jenkins_home
2、登录 jenkins,127.0.0.1:8080(本文是在本地实现的,所以用 127.0.0.1)。按照提示输入登录密码,安装推荐插件就行。
3、添加 vm 到节点。“系统管理”–“节点管理”–“新建节点”,选择固定节点
连接报错
No entry currently exists in the Known Hosts file for this host. Connections will be denied until this new host and its associated key is added to the Known Hosts file. Key exchange was not finished, connection is closed. SSH Connection failed with IOException: "Key exchange was not finished, connection is closed.",
解决方法:Host Key Verification Strategy 选择 None verifying Verification Strategy。参考
报错:
[12/17/20 00:39:39] [SSH] Checking java version of /home/jenkins/jdk/bin/java
Couldn't figure out the Java version of /home/jenkins/jdk/bin/java
bash: /home/jenkins/jdk/bin/java: No such file or directory
解决方法:在节点 vm ip 192.168.116.6 上安装 java,如果不使用 java jdk 的情况下,可以直接使用 openjdk
yum install -y java-1.8.0-openjdk
4、对 node2 节点进行测试
新建一个流水线 aaa(我的已经创建好了,这里显示已存在)
pipeline scritpt
node('node2') {
stage('Clone') {
echo "1.Clone Stage"
}
stage('Test') {
echo "2.Test Stage"
echo "Skip it this time"
}
stage('Build') {
echo "3.Build Docker Image Stage"
}
stage('Push') {
echo "4.Push Docker Image Stage"
}
stage('Deploy') {
echo "5. Deploy Stage"
def userInput = input(
id: 'userInput',
message: 'Choose a deploy environment',
parameters: [
[
$class: 'ChoiceParameterDefinition',
choices: "Dev\nQA\nProd",
name: 'Env'
]
]
)
echo "=========heheda==========="
echo "This is a deploy step to ${userInput}"
if (userInput == "Dev") {
// deploy dev stuff
} else if (userInput == "QA"){
// deploy qa stuff
} else {
// deploy prod stuff
}
echo "pipeline script end"
}
}
立即构建
查看结果日志
5、补充。一个完整的部署 java 项目到 k8s 可以是这样的 pipeline script,参考
node('haimaxy-jnlp') {
stage('Clone') {
echo "1.Clone Stage"
git url: "
http://192.168.1.13/joker/jenkins-demo.git"
;
script {
build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
}
echo "$build_tag"
echo "$env.BRANCH_NAME"
}
stage('Test') {
echo "2.Test Stage"
echo "Skip it this time"
}
stage('Build') {
echo "3.Build Docker Image Stage"
sh "docker build -t cnych/jenkins-demo:${build_tag} ."
}
stage('Push') {
echo "4.Push Docker Image Stage"
withCredentials([usernamePassword(credentialsId: 'harbor', passwordVariable: 'harborPassword', usernameVariable: 'harborUser')]) {
sh "docker tag cnych/jenkins-demo:${build_tag} 192.168.1.200:32023/cnych/jenkins-demo:${build_tag}"
sh "docker login
http://192.168.1.200:32023
-u ${harborUser} -p ${harborPassword}"
sh "docker push 192.168.1.200:32023/cnych/jenkins-demo:${build_tag}"
}
}
stage('Deploy') {
echo "5. Deploy Stage"
def userInput = input(
id: 'userInput',
message: 'Choose a deploy environment',
parameters: [
[
$class: 'ChoiceParameterDefinition',
choices: "Dev\nQA\nProd",
name: 'Env'
]
]
)
echo "=========heheda==========="
echo "This is a deploy step to ${userInput}"
sh "sed -i 's/<BUILD_TAG>/${build_tag}/' k8s.yaml"
if (userInput == "Dev") {
// deploy dev stuff
} else if (userInput == "QA"){
// deploy qa stuff
} else {
// deploy prod stuff
}
sh "kubectl apply -f k8s.yaml"
}
}