看了一下网络上大多是使用自由构建做的教程,使用pipeline的有点少,今天自己做了一下,感觉有些小坑,做个教程让大家规避一下。
先上原PIPELINE:
pipeline {
agent any
parameters{
choice(
choices: 'deploy\nrollback',
description: '',
name: 'status'
)
string(name: 'version', defaultValue: '0', description: '')
string(name: 'tag', defaultValue: '0', description: '')
}
stages {
stage('GitPull') {
steps {
git branch: 'test',credentialsId: 'ea217bd6-f4f6-495f-8b1f-f8f7324ac5f7', url: 'http://XXX.XXX.XXX.XXX/back-end/XXX.git'
}
}
stage('Choice'){
when{
expression {params.status == "rollback"}
}
steps{
sh 'git checkout $version'
}
}
stage('Package'){
steps{
... ...
}
}
stage('dockerbuild'){
steps{
... ...
}
}
}
post {
always {
echo 'One way or another, I have finished'
// deleteDir() /* clean up our workspace */
}
success {
echo 'I succeeeded!'
script {
def gitUrl = 'http://XXX.XXX.XXX.XXX/back-end/XXX.git'
def currentDate = sh(script: "date +'%Y%m%d%H%M%S'", returnStdout: true).trim()
def tag = "${params.tag}-${currentDate}"
withCredentials([usernamePassword(credentialsId: 'ea217bd6-f4f6-495f-8b1f-f8f7324ac5f7', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
sh 'git config user.name "Jenkins"'
sh 'git config user.email "jenkins@example.com"'
sh """
git config credential.helper store
echo "http://${GIT_USERNAME}:${GIT_PASSWORD}@192.168.0.129" > ~/.git-credentials
"""
// 创建 Tag 并推送到远程仓库
sh """
git tag -a ${tag} -m 'Jenkins Build #${env.BUILD_NUMBER}'
git push origin ${tag}
"""
}
}
}
unstable {
echo 'I am unstable :/'
}
failure {
echo 'I failed :('
}
changed {
echo 'Things were different before...'
}
}
}
主要部分
post {
always {
echo 'One way or another, I have finished'
// deleteDir() /* clean up our workspace */
}
success {
echo 'I succeeeded!'
script {
def gitUrl = 'http://xxx.xxx.xxx.xxx/back-end/xxx.git'
def currentDate = sh(script: "date +'%Y%m%d%H%M%S'", returnStdout: true).trim()
def tag = "${params.tag}-${currentDate}"
withCredentials([usernamePassword(credentialsId: 'abcdefg', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
sh 'git config user.name "Jenkins"'
sh 'git config user.email "jenkins@example.com"'
sh """
git config credential.helper store
echo "http://${GIT_USERNAME}:${GIT_PASSWORD}@xxx.xxx.xxx.xxx" > ~/.git-credentials
"""
// 创建 Tag 并推送到远程仓库
sh """
git tag -a ${tag} -m 'Jenkins Build #${env.BUILD_NUMBER}'
git push origin ${tag}
"""
// 清理凭据
sh 'rm ~/.git-credentials'
}
}
}
unstable {
echo 'I am unstable :/'
}
failure {
echo 'I failed :('
}
changed {
echo 'Things were different before...'
}
}
POST 构建结束后:
success
成功后执行:{ }
一些基础参数:
- giturl //仓库地址。
- currentDate //获取当时时间,格式化20240108141805(到秒)
- tag //标签名 这里通过上面
string(name: 'tag', defaultValue: '0', description: '')
输入版本号加上当前时间作为标签名。
重要配置:withCredentials
这个是jenkins自带的凭据工具:
- usernamePassword 解析凭据格式为用户名/密码格式。
- credentialsId 凭据在jenkins中的ID。
- usernameVariable解析用户名并存到变量GIT_USERNAME中。
- passwordVariable解析密码并存到变量GIT_PASSWORD中。
- 接下来是GIT配置用户名称和邮件。这个可以随意配置。如果不配置会提示:`Committer identity unknown *** Please tell me who you are.`
- 重点、如果直接使用凭据用户名和密码并不能正确使用git push 分支 :
- git config credential.helper store 使用GIT凭据管理工具生成凭据文件。
echo "http://${GIT_USERNAME}:${GIT_PASSWORD}@xxx.xxx.xxx.xxx" > ~/.git-credentials
//将解析出来的用户密码写入到凭据文件。
- 使用本地配置凭据进行打TAG及PUSH到远程仓库。
如果不使用git凭据管理工具会出现:
fatal: could not read Username for 'http://XXX.XXX.XXX.XXX': No such device or address
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // script
Error when executing success post condition:
Also: org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: c653bd26-ca3b-4788-a0f1-d7cd75aa72df
hudson.AbortException: script returned exit code 128
at org.jenkinsci.plugins.workflow.steps.durable_task.DurableTaskStep$Execution.handleExit(DurableTaskStep.java:668)
at org.jenkinsci.plugins.workflow.steps.durable_task.DurableTaskStep$Execution.check(DurableTaskStep.java:614)
at org.jenkinsci.plugins.workflow.steps.durable_task.DurableTaskStep$Execution.run(DurableTaskStep.java:555)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:842)
这样问题得到完美解决。
评论区