我是Jenkinsfile
的新手,正在尝试将我必须拥有的工作传递给Jenkinsfile。我已经成功创建了所需的所有步骤,但无法像以前使用该界面和“ git Publisher”插件那样向我的git存储库添加标签。
这是我现在得到的:
stage('Tag') {
steps {
script {
env.POM_VERSION = readMavenPom().getVersion()
}
// creating the name of the tag
sh '''#!/bin/bash -xe
currentDate=$(date +"%Y-%m-%d_%Hh%Mm%Ss")
customTagName="${CUSTOMER_NAME}--${POM_VERSION}--${currentDate}"
echo CUSTOM_TAG_NAME=${customTagName} >> ${PROPERTIES_FILE_NAME}
'''
script {
def PROPERTIES = readProperties file: "${PROPERTIES_FILE_NAME}"
env.CUSTOM_TAG_NAME = PROPERTIES.CUSTOM_TAG_NAME
}
withCredentials([usernamePassword(credentialsId: "${GIT_CREDENTIALS_ID}", passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
sh "git tag -a ${CUSTOM_TAG_NAME} -m 'Jenkinsfile push tag'"
sh "git push https://${GIT_USERNAME}:${GIT_PASSWORD}@git.repohere.com/scm/reponamehere.git ${CUSTOM_TAG_NAME}"
}
}
}
但是后来我在日志中遇到了这个问题:
[Pipeline] sh+ git push'https:// ****:****@git.repohere.com/scm/reponamehere.git'名称--4.36.0--2020-04-10_22h00m50s
致命:无法访问'https:// ****:****@git.repohere.com/scm/reponamehere.git/':无法解析主机:****;未知错误[管道]}[管道] // withCredentials
我已经坚持了几个小时,如何在jenkins配置中添加和推送带有凭据的标签。谢谢!
首先在管道的环境指令中已经声明了此变量${GIT_CREDENTIALS_ID}
吗?如果不首先声明它,或者直接使用变量的值,如下所示:
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'MyID', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {
sh("git tag -a some_tag -m 'Jenkins'")
sh("git push https://${env.GIT_USERNAME}:${env.GIT_PASSWORD}@<REPO> --tags")
}
如果您使用sshagent创建标签并推送,则无需在jenkinsfile中的GIT存储库URL中传递密码。
pipeline {
agent any
stages {
stage("Tag and Push") {
when { branch 'master' }
environment {
GIT_TAG = "jenkins-$BUILD_NUMBER"
}
steps {
sh('''
git config user.name 'my-ci-user'
git config user.email '[email protected]'
git tag -a \$GIT_TAG -m "[Jenkins CI] New Tag"
''')
sshagent(['my-ssh-credentials-id']) {
sh("""
#!/usr/bin/env bash
set +x
export GIT_SSH_COMMAND="ssh -oStrictHostKeyChecking=no"
git push origin \$GIT_TAG
""")
}
}
}
}
}